?
Solved

How to specify JDBC inside web.xml for struts application

Posted on 2003-03-03
10
Medium Priority
?
399 Views
Last Modified: 2010-08-05
Hello -

I am using Struts for my new application. I have to access the Oracle database using JDBC. I want to specify the connection to Oracle DB in the web.xml or struts-config.xml file (not sure which one to use). I want to connect at the time the application starts and use the same connection object through all my pages. Is this feasible? If so, which xml file to mention and how? Also, how to pass the connection object to all the pages?

Thanks a lot
Pribu
0
Comment
Question by:pribu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 8058437
you can store the connection URL in the web.xml

<init-param>
   <param-name>dbconnectURL</param-name>
   <param-value>jdbc:oracle:thin.....</param-value>
</init-param>

But I don't believe you can use either xml file to actually store a connection object.

What you can do is create an application level connection object that is reused by each user, though you may run into race conditions with multiple users.

On the other hand you can create a Connection object in session for each user and they can reuse that connection for their page accesses.

CJ
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8059953
pages are accessed concurrently, even one connection per session can cause serious race condition, because conneciton object is not thread safe!

you should consider to use connection pool, if you'd like to use struts, put this right after <struts-config> in struts-config.xml file:

  <data-sources>
    <data-source>
      <set-property property="autoCommit"
                       value="false"/>
      <set-property property="description"
                       value="Example Data Source Configuration"/>
      <set-property property="driverClass"
                       value="org.postgresql.Driver"/>
      <set-property property="maxCount"
                       value="4"/>
      <set-property property="minCount"
                       value="2"/>
      <set-property property="password"
                       value="mypassword"/>
      <set-property property="url"
                       value="jdbc:postgresql://localhost/mydatabase"/>
      <set-property property="user"
                       value="myusername"/>
    </data-source>
  </data-sources>

let me know if you have further enquires.
0
 

Author Comment

by:pribu
ID: 8060115
Hello -

Actually I have done something very similar to the one menthioned by kennethxu. I even access it in my Action class as follows:

ServletContext context = servlet.getServletContext();

DataSource dataSource = (DataSource) context.getAttribute (Action.DATA_SOURCE_KEY);

 con = dataSource.getConnection();

The problem is, when the action class that contains the code above to get the connection object, is called many times by hitting the url that calls this action class, for some reason, after 15 or 20 times, the page takes a very long time to reload, I would say even 5  - 10 minutes.

I dont know why this happens?

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:kennethxu
ID: 8060265
you must do con.close() after you have done with the connection. con.close() allows the connection to go back to pool after use.

make sure you never store the connection object somewhere, because it is not thread safe and shouldn't be reused in most of case.

always make your code looks like this:

Connection con = null;
try {
  con = dataSource.getConnection();
  // use your con, create statemnt, resultset
  // .......
} finally {
  // if you have statment, close it here too.
  if( con != null ) try { con.close(); } catch(Exception e) {}
}
0
 

Author Comment

by:pribu
ID: 8060382
Thanks a lot kennethxu. I shall try the above and see if it still takes a long time after a number of reloads to the same page.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8077151
did you have a chance to try that out?
0
 

Expert Comment

by:richiegr
ID: 8078573
Hi

Struts have a simple connection pool implementation built-in so you can use it like this :
add in your struts-config.xml something like (adapt to your database):
<struts-config>
  <data-sources>
    <data-source
      autoCommit="false"
     description="Example Data Source Description"
     driverClass="org.postgresql.Driver"
        maxCount="4"
        minCount="2"
        password="mypassword"
             url="jdbc:postgresql://localhost/mydatabase"
            user="myusername"/>
  </data-sources>
</struts-config>

Then in your action you could make :
public ActionForward
       perform(ActionMapping mapping,
               ActionForm form,
               HttpServletRequest request,
               HttpServletResponse response)
{
 try {
   javax.sql.DataSource dataSource =
     servlet.findDataSource(null);
   java.sql.Connection myConnection =
     dataSource.getConnection();

   //do what you wish with myConnection
 } catch (SQLException sqle) {
   getServlet().log("Connection.process", sqle);
 } finally {

   //enclose this in a finally block to make
   //sure the connection is closed
   try {
     myConnection.close();
   } catch (SQLException e) {
     getServlet().log("Connection.close", e);
   }
 }
}

You don't need to configure nothing into web.xml it's work and the connection are pooled too

Bye

Richard

0
 

Author Comment

by:pribu
ID: 8080043
Yup. Its works as suggested by kennethxu and richiegr. Closing the connection after using the data source works. But I wll do more rigiorous stress testing to prove it.

Thanks a million for all your help.
0
 
LVL 14

Accepted Solution

by:
kennethxu earned 120 total points
ID: 8085387
if it works, can you accept the comment as answer to PAQ this question? thanks :)
0
 

Author Comment

by:pribu
ID: 8129267
It works as suggested by kennethxu and richiegr
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question