commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1568264 - in /commons/proper/dbcp/trunk/src: changes/ main/java/org/apache/commons/dbcp2/ main/java/org/apache/commons/dbcp2/managed/
Date Fri, 14 Feb 2014 12:55:36 GMT
Author: markt
Date: Fri Feb 14 12:55:35 2014
New Revision: 1568264

URL: http://svn.apache.org/r1568264
Log:
DBCP-340
Provide an option to set the default query timeout.

Modified:
    commons/proper/dbcp/trunk/src/changes/changes.xml
    commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java
    commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
    commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java
    commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
    commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/managed/BasicManagedDataSource.java

Modified: commons/proper/dbcp/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/changes/changes.xml?rev=1568264&r1=1568263&r2=1568264&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/changes/changes.xml (original)
+++ commons/proper/dbcp/trunk/src/changes/changes.xml Fri Feb 14 12:55:35 2014
@@ -165,6 +165,9 @@ The <action> type attribute can be add,u
         Provide an option to control if rollback is called when a connection is
         returned to the poll with autoCommit disabled.
       </action>
+      <action dev="markt" issue="DBCP-340" type="add">
+        Provide an option to set the default query timeout.
+      </action>
     </release>
     <release version="1.5.1" date="TBD" description="TBD">
       <action dev="markt" issue="DBCP-400" type="fix">

Modified: commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java?rev=1568264&r1=1568263&r2=1568264&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java
(original)
+++ commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java
Fri Feb 14 12:55:35 2014
@@ -35,6 +35,7 @@ import java.sql.Driver;
 import java.sql.DriverManager;
 import java.sql.SQLException;
 import java.sql.SQLFeatureNotSupportedException;
+import java.sql.Statement;
 
 import javax.management.InstanceAlreadyExistsException;
 import javax.management.MBeanRegistration;
@@ -200,6 +201,28 @@ public class BasicDataSource
     }
 
 
+    private Integer defaultQueryTimeout = null;
+
+    /**
+     * Obtain the default query timeout that will be used for {@link Statement}s
+     * created from this connection. <code>null</code> means that the driver
+     * default will be used.
+     */
+    public Integer getDefaultQueryTimeout() {
+        return defaultQueryTimeout;
+    }
+
+
+    /**
+     * Set the default query timeout that will be used for {@link Statement}s
+     * created from this connection. <code>null</code> means that the driver
+     * default will be used.
+     */
+    public void setDefaultQueryTimeout(Integer defaultQueryTimeout) {
+        this.defaultQueryTimeout = defaultQueryTimeout;
+    }
+
+
     /**
      * The default "catalog" of connections created by this pool.
      */
@@ -2121,6 +2144,7 @@ public class BasicDataSource
             connectionFactory.setMaxConnLifetimeMillis(maxConnLifetimeMillis);
             connectionFactory.setRollbackOnReturn(getRollbackOnReturn());
             connectionFactory.setEnableAutoCommitOnReturn(getEnableAutoCommitOnReturn());
+            connectionFactory.setDefaultQueryTimeout(getDefaultQueryTimeout());
             validateConnectionFactory(connectionFactory);
         } catch (RuntimeException e) {
             throw e;

Modified: commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java?rev=1568264&r1=1568263&r2=1568264&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
(original)
+++ commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
Fri Feb 14 12:55:35 2014
@@ -95,6 +95,7 @@ public class BasicDataSourceFactory impl
     private final static String PROP_MAXCONNLIFETIMEMILLIS = "maxConnLifetimeMillis";
     private final static String PROP_ROLLBACK_ON_RETURN = "rollbackOnReturn";
     private final static String PROP_ENABLE_AUTOCOMMIT_ON_RETURN = "enableAutoCommitOnReturn";
+    private final static String PROP_DEFAULT_QUERYTIMEOUT = "defaultQueryTimeout";
 
     private final static String[] ALL_PROPERTIES = {
         PROP_DEFAULTAUTOCOMMIT,
@@ -133,7 +134,8 @@ public class BasicDataSourceFactory impl
         PROP_CONNECTIONPROPERTIES,
         PROP_MAXCONNLIFETIMEMILLIS,
         PROP_ROLLBACK_ON_RETURN,
-        PROP_ENABLE_AUTOCOMMIT_ON_RETURN
+        PROP_ENABLE_AUTOCOMMIT_ON_RETURN,
+        PROP_DEFAULT_QUERYTIMEOUT
     };
 
     // -------------------------------------------------- ObjectFactory Methods
@@ -420,6 +422,12 @@ public class BasicDataSourceFactory impl
             dataSource.setRollbackOnReturn(Boolean.valueOf(value).booleanValue());
         }
 
+        value = properties.getProperty(PROP_DEFAULT_QUERYTIMEOUT);
+        if (value != null) {
+            dataSource.setDefaultQueryTimeout(Integer.valueOf(value));
+        }
+
+
         // DBCP-215
         // Trick to make sure that initialSize connections are created
         if (dataSource.getInitialSize() > 0) {

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=1568264&r1=1568263&r2=1568264&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
Fri Feb 14 12:55:35 2014
@@ -80,6 +80,7 @@ public class DelegatingConnection<C exte
     private boolean _cacheState = true;
     private Boolean _autoCommitCached = null;
     private Boolean _readOnlyCached = null;
+    private Integer defaultQueryTimeout = null;
 
     /**
      * Create a wrapper for the Connection which traces this
@@ -247,11 +248,21 @@ public class DelegatingConnection<C exte
         throw e;
     }
 
+    private void initializeStatement(DelegatingStatement ds) throws SQLException {
+        if (defaultQueryTimeout != null &&
+                defaultQueryTimeout.intValue() != ds.getQueryTimeout()) {
+            ds.setQueryTimeout(defaultQueryTimeout.intValue());
+        }
+    }
+
     @Override
     public Statement createStatement() throws SQLException {
         checkOpen();
         try {
-            return new DelegatingStatement(this, _conn.createStatement());
+            DelegatingStatement ds =
+                    new DelegatingStatement(this, _conn.createStatement());
+            initializeStatement(ds);
+            return ds;
         }
         catch (SQLException e) {
             handleException(e);
@@ -264,8 +275,10 @@ public class DelegatingConnection<C exte
                                      int resultSetConcurrency) throws SQLException {
         checkOpen();
         try {
-            return new DelegatingStatement
-                (this, _conn.createStatement(resultSetType,resultSetConcurrency));
+            DelegatingStatement ds = new DelegatingStatement(
+                    this, _conn.createStatement(resultSetType,resultSetConcurrency));
+            initializeStatement(ds);
+            return ds;
         }
         catch (SQLException e) {
             handleException(e);
@@ -277,8 +290,10 @@ public class DelegatingConnection<C exte
     public PreparedStatement prepareStatement(String sql) throws SQLException {
         checkOpen();
         try {
-            return new DelegatingPreparedStatement
-                (this, _conn.prepareStatement(sql));
+            DelegatingPreparedStatement dps = new DelegatingPreparedStatement(
+                    this, _conn.prepareStatement(sql));
+            initializeStatement(dps);
+            return dps;
         }
         catch (SQLException e) {
             handleException(e);
@@ -292,9 +307,10 @@ public class DelegatingConnection<C exte
                                               int resultSetConcurrency) throws SQLException
{
         checkOpen();
         try {
-            return new DelegatingPreparedStatement
-                (this, _conn.prepareStatement
-                    (sql,resultSetType,resultSetConcurrency));
+            DelegatingPreparedStatement dps = new DelegatingPreparedStatement(
+                    this, _conn.prepareStatement(sql,resultSetType,resultSetConcurrency));
+            initializeStatement(dps);
+            return dps;
         }
         catch (SQLException e) {
             handleException(e);
@@ -306,7 +322,10 @@ public class DelegatingConnection<C exte
     public CallableStatement prepareCall(String sql) throws SQLException {
         checkOpen();
         try {
-            return new DelegatingCallableStatement(this, _conn.prepareCall(sql));
+            DelegatingCallableStatement dcs =
+                    new DelegatingCallableStatement(this, _conn.prepareCall(sql));
+            initializeStatement(dcs);
+            return dcs;
         }
         catch (SQLException e) {
             handleException(e);
@@ -320,8 +339,10 @@ public class DelegatingConnection<C exte
                                          int resultSetConcurrency) throws SQLException {
         checkOpen();
         try {
-            return new DelegatingCallableStatement
-                (this, _conn.prepareCall(sql, resultSetType,resultSetConcurrency));
+            DelegatingCallableStatement dcs = new DelegatingCallableStatement(
+                    this, _conn.prepareCall(sql, resultSetType,resultSetConcurrency));
+            initializeStatement(dcs);
+            return dcs;
         }
         catch (SQLException e) {
             handleException(e);
@@ -477,6 +498,26 @@ public class DelegatingConnection<C exte
 
 
     /**
+     * Obtain the default query timeout that will be used for {@link Statement}s
+     * created from this connection. <code>null</code> means that the driver
+     * default will be used.
+     */
+    public Integer getDefaultQueryTimeout() {
+        return defaultQueryTimeout;
+    }
+
+
+    /**
+     * Set the default query timeout that will be used for {@link Statement}s
+     * created from this connection. <code>null</code> means that the driver
+     * default will be used.
+     */
+    public void setDefaultQueryTimeout(Integer defaultQueryTimeout) {
+        this.defaultQueryTimeout = defaultQueryTimeout;
+    }
+
+
+    /**
      * Sets the state caching flag.
      *
      * @param cacheState    The new value for the state caching flag
@@ -682,8 +723,11 @@ public class DelegatingConnection<C exte
                                      int resultSetHoldability) throws SQLException {
         checkOpen();
         try {
-            return new DelegatingStatement(this, _conn.createStatement(
-                resultSetType, resultSetConcurrency, resultSetHoldability));
+            DelegatingStatement ds = new DelegatingStatement(this,
+                    _conn.createStatement(resultSetType, resultSetConcurrency,
+                            resultSetHoldability));
+            initializeStatement(ds);
+            return ds;
         }
         catch (SQLException e) {
             handleException(e);
@@ -697,8 +741,11 @@ public class DelegatingConnection<C exte
                                               int resultSetHoldability) throws SQLException
{
         checkOpen();
         try {
-            return new DelegatingPreparedStatement(this, _conn.prepareStatement(
-                sql, resultSetType, resultSetConcurrency, resultSetHoldability));
+            DelegatingPreparedStatement dps = new DelegatingPreparedStatement(
+                    this, _conn.prepareStatement(sql, resultSetType,
+                            resultSetConcurrency, resultSetHoldability));
+            initializeStatement(dps);
+            return dps;
         }
         catch (SQLException e) {
             handleException(e);
@@ -712,8 +759,11 @@ public class DelegatingConnection<C exte
                                          int resultSetHoldability) throws SQLException {
         checkOpen();
         try {
-            return new DelegatingCallableStatement(this, _conn.prepareCall(
-                sql, resultSetType, resultSetConcurrency, resultSetHoldability));
+            DelegatingCallableStatement dcs = new DelegatingCallableStatement(
+                    this, _conn.prepareCall(sql, resultSetType,
+                            resultSetConcurrency, resultSetHoldability));
+            initializeStatement(dcs);
+            return dcs;
         }
         catch (SQLException e) {
             handleException(e);
@@ -725,8 +775,10 @@ public class DelegatingConnection<C exte
     public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException
{
         checkOpen();
         try {
-            return new DelegatingPreparedStatement(this, _conn.prepareStatement(
-                sql, autoGeneratedKeys));
+            DelegatingPreparedStatement dps = new DelegatingPreparedStatement(
+                    this, _conn.prepareStatement(sql, autoGeneratedKeys));
+            initializeStatement(dps);
+            return dps;
         }
         catch (SQLException e) {
             handleException(e);
@@ -738,8 +790,10 @@ public class DelegatingConnection<C exte
     public PreparedStatement prepareStatement(String sql, int columnIndexes[]) throws SQLException
{
         checkOpen();
         try {
-            return new DelegatingPreparedStatement(this, _conn.prepareStatement(
-                sql, columnIndexes));
+            DelegatingPreparedStatement dps = new DelegatingPreparedStatement(
+                    this, _conn.prepareStatement(sql, columnIndexes));
+            initializeStatement(dps);
+            return dps;
         }
         catch (SQLException e) {
             handleException(e);
@@ -751,8 +805,10 @@ public class DelegatingConnection<C exte
     public PreparedStatement prepareStatement(String sql, String columnNames[]) throws SQLException
{
         checkOpen();
         try {
-            return new DelegatingPreparedStatement(this, _conn.prepareStatement(
-                sql, columnNames));
+            DelegatingPreparedStatement dps =  new DelegatingPreparedStatement(
+                    this, _conn.prepareStatement(sql, columnNames));
+            initializeStatement(dps);
+            return dps;
         }
         catch (SQLException e) {
             handleException(e);

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=1568264&r1=1568263&r2=1568264&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
Fri Feb 14 12:55:35 2014
@@ -192,6 +192,15 @@ public class PoolableConnectionFactory
     }
 
 
+    public Integer getDefaultQueryTimeout() {
+        return defaultQueryTimeout;
+    }
+
+    public void setDefaultQueryTimeout(Integer defaultQueryTimeout) {
+        this.defaultQueryTimeout = defaultQueryTimeout;
+    }
+
+
     @Override
     public PooledObject<PoolableConnection> makeObject() throws Exception {
         Connection conn = _connFactory.createConnection();
@@ -381,6 +390,7 @@ public class PoolableConnectionFactory
                 (!_defaultCatalog.equals(conn.getCatalog()))) {
             conn.setCatalog(_defaultCatalog);
         }
+        conn.setDefaultQueryTimeout(defaultQueryTimeout);
     }
 
     private void validateLifetime(PooledObject<PoolableConnection> p)
@@ -430,6 +440,7 @@ public class PoolableConnectionFactory
         GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL_PER_KEY;
     private long maxConnLifetimeMillis = -1;
     private AtomicLong connectionIndex = new AtomicLong(0);
+    private Integer defaultQueryTimeout = null;
 
     /**
      * Internal constant to indicate the level is not set.

Modified: commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/managed/BasicManagedDataSource.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/managed/BasicManagedDataSource.java?rev=1568264&r1=1568263&r2=1568264&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/managed/BasicManagedDataSource.java
(original)
+++ commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/managed/BasicManagedDataSource.java
Fri Feb 14 12:55:35 2014
@@ -199,6 +199,7 @@ public class BasicManagedDataSource exte
             connectionFactory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis());
             connectionFactory.setRollbackOnReturn(getRollbackOnReturn());
             connectionFactory.setEnableAutoCommitOnReturn(getEnableAutoCommitOnReturn());
+            connectionFactory.setDefaultQueryTimeout(getDefaultQueryTimeout());
             validateConnectionFactory(connectionFactory);
         } catch (RuntimeException e) {
             throw e;



Mime
View raw message