DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9073>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9073

BasicDataSource - invalid connections are not checked

           Summary: BasicDataSource - invalid connections are not checked
           Product: Commons
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Dbcp
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


Hi all!

I recently moved from Struts 1.0 to Struts 1.1 b1 running on Tomcat 3.3.1 
final. I use the standard implementation for javax.sql.DataSource provided with 
Struts, org.apache.struts.util.GenericDataSource, to handle my database 
connections.
I found that, if the database server went down and came up again afterwards 
while my application was running, I was not able to get a valid 
java.sql.Connection instance from GenericDataSource after that. Executing any 
SQL query on such a Connection failed.

After looking through the source code I found the following:

In Struts 1.1 GenericDataSource delegates most of its functionality to 
org.apache.commons.dbcp.BasicDataSource. This class provides a String 
property "validationQuery" representing, as the javadoc comment says, the SQL 
query that will be used to validate connections from this pool before returning 
them to the caller.
This field "validationQuery" is passed to an instance of 
org.apache.commons.dbcp.PoolableConnectionFactory while initializing the 
underlying DataSource implementation for BasicDataSource.

The SQL query contained in "validationQuery" is executed when the 
method "validateObject()" of PoolableConnectionFactory is called. This call 
should be done by org.apache.commons.pool.impl.GenericObjectPool which is 
internally used for pooling the database connections by BasicDataSource, at 
least if a connection from the pool is requested by calling the "borrowObject
()" method.
But, if we want GenericObjectPool to validate an object from the pool before it 
is returned to the caller, we have to explicitly set its 
property "testOnBorrow" to true, as it defaults to false. This is not done 
while initializing the underlying DataSource implementation for BasicDataSource.

Therefore I propose the following patch to solve the encountered problem:

diff -u -r1.6 BasicDataSource.java
--- src/java/org/apache/commons/dbcp/BasicDataSource.java       1 May 2002 
06:27:52 -0000       1.6
+++ src/java/org/apache/commons/dbcp/BasicDataSource.java       14 May 2002 
15:22:35 -0000
@@ -476,6 +476,7 @@
         connectionPool.setMaxActive(maxActive);
         connectionPool.setMaxIdle(maxIdle);
         connectionPool.setMaxWait(maxWait);
+        connectionPool.setTestOnBorrow(true);

         // Set up the driver connection factory we will use
         connectionProperties.put("user", username);

Thanks for considering this proposal. I hope I found the right place for this 
change in the impressively structured code mass built by you.

Alex.

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to