?
Solved

How to specify JDBC inside web.xml for struts application

Posted on 2003-03-03
10
Medium Priority
?
402 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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

A Case Study of using the Windows API to provide RS232 communications capability in Access without the use of Active-X controls.
The deadly train derailment that occurred recently in DuPont, Washington, raises a lot of questions. It was a new route, the first trip tested with passengers, and the train was travelling at 50 mph over the zone’s speed limit. Could IoT play a role…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

580 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