commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1568643 - in /commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2: DelegatingConnection.java PoolableConnection.java PoolableConnectionFactory.java
Date Sat, 15 Feb 2014 16:33:00 GMT
Author: markt
Date: Sat Feb 15 16:33:00 2014
New Revision: 1568643

URL: http://svn.apache.org/r1568643
Log:
Use a PreparedStatement for the validation query to improve performance. Testing suggests
a ~15% improvement with a very basic validation query.

Modified:
    commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java
    commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnection.java
    commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java

Modified: commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java?rev=1568643&r1=1568642&r2=1568643&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java
(original)
+++ commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java
Sat Feb 15 16:33:00 2014
@@ -75,7 +75,7 @@ public class DelegatingConnection<C exte
     /** My delegate {@link Connection}. */
     private C _conn = null;
 
-    private boolean _closed = false;
+    private volatile boolean _closed = false;
 
     private boolean _cacheState = true;
     private Boolean _autoCommitCached = null;

Modified: commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnection.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnection.java?rev=1568643&r1=1568642&r2=1568643&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnection.java
(original)
+++ commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnection.java
Sat Feb 15 16:33:00 2014
@@ -18,6 +18,8 @@ package org.apache.commons.dbcp2;
 
 import java.lang.management.ManagementFactory;
 import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 
 import javax.management.InstanceAlreadyExistsException;
@@ -49,6 +51,11 @@ public class PoolableConnection extends 
 
     private final ObjectName _jmxName;
 
+    // Use a prepared statement for validation, retaining the last used SQL to
+    // check if the validation query has changed.
+    private PreparedStatement validationPreparedStatement = null;
+    private String lastValidationSql = null;
+
     /**
      *
      * @param conn my underlying connection
@@ -168,6 +175,16 @@ public class PoolableConnection extends 
                 // Ignore
             }
         }
+
+
+        if (validationPreparedStatement != null) {
+            try {
+                validationPreparedStatement.close();
+            } catch (SQLException sqle) {
+                // Ignore
+            }
+        }
+
         super.closeInternal();
     }
 
@@ -180,5 +197,32 @@ public class PoolableConnection extends 
     public String getToString() {
         return toString();
     }
+
+
+    public void validate(String sql, int timeout) throws SQLException {
+        if (sql == null) {
+            return;
+        }
+
+        if (!sql.equals(lastValidationSql)) {
+            lastValidationSql = sql;
+            // Has to be the innermost delegate else the prepared statement will
+            // be closed when the pooled connection is passivated.
+            validationPreparedStatement =
+                    getInnermostDelegateInternal().prepareStatement(sql);
+        }
+
+        if (timeout > 0) {
+            validationPreparedStatement.setQueryTimeout(timeout);
+        }
+
+        try (ResultSet rs = validationPreparedStatement.executeQuery()) {
+            if(!rs.next()) {
+                throw new SQLException("validationQuery didn't return a row");
+            }
+        } catch (SQLException sqle) {
+            throw sqle;
+        }
+    }
 }
 

Modified: commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java?rev=1568643&r1=1568642&r2=1568643&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
(original)
+++ commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
Sat Feb 15 16:33:00 2014
@@ -19,7 +19,6 @@ package org.apache.commons.dbcp2;
 
 import java.sql.Connection;
 import java.sql.Statement;
-import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.concurrent.atomic.AtomicLong;
@@ -297,40 +296,11 @@ public class PoolableConnectionFactory
         }
     }
 
-    public void validateConnection(Connection conn) throws SQLException {
-        String query = _validationQuery;
+    public void validateConnection(PoolableConnection conn) throws SQLException {
         if(conn.isClosed()) {
             throw new SQLException("validateConnection: connection closed");
         }
-        if(null != query) {
-            Statement stmt = null;
-            ResultSet rset = null;
-            try {
-                stmt = conn.createStatement();
-                if (_validationQueryTimeout > 0) {
-                    stmt.setQueryTimeout(_validationQueryTimeout);
-                }
-                rset = stmt.executeQuery(query);
-                if(!rset.next()) {
-                    throw new SQLException("validationQuery didn't return a row");
-                }
-            } finally {
-                if (rset != null) {
-                    try {
-                        rset.close();
-                    } catch(Exception t) {
-                        // ignored
-                    }
-                }
-                if (stmt != null) {
-                    try {
-                        stmt.close();
-                    } catch(Exception t) {
-                        // ignored
-                    }
-                }
-            }
-        }
+        conn.validate(_validationQuery, _validationQueryTimeout);
     }
 
     @Override



Mime
View raw message