You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2011/05/11 13:39:54 UTC

svn commit: r1101837 - in /commons/proper/dbcp/trunk: ./ src/java/org/apache/commons/dbcp2/ src/site/xdoc/

Author: markt
Date: Wed May 11 11:39:54 2011
New Revision: 1101837

URL: http://svn.apache.org/viewvc?rev=1101837&view=rev
Log:
Make state cahcing optional

Modified:
    commons/proper/dbcp/trunk/RELEASE-NOTES.txt
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSource.java
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/DelegatingConnection.java
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
    commons/proper/dbcp/trunk/src/site/xdoc/configuration.xml

Modified: commons/proper/dbcp/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/RELEASE-NOTES.txt?rev=1101837&r1=1101836&r2=1101837&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/dbcp/trunk/RELEASE-NOTES.txt Wed May 11 11:39:54 2011
@@ -10,8 +10,9 @@ DBCP 2.0.x.
 Changes since DBCP 1.3/1.4 include:
 
 New features:
-o  Cache current values of autoCommit and readOnly so database queries are not
-   required for every call to the associated getters.
+o  Provide a new option (cacheState) to cache current values of autoCommit and
+   readOnly so database queries are not required for every call to the
+   associated getters. This option is enabled by default.
 
 Fixed Bugs:
 o  DBCP-300: Removed unnecessary synchronisation in

Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSource.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSource.java?rev=1101837&r1=1101836&r2=1101837&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSource.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSource.java Wed May 11 11:39:54 2011
@@ -192,7 +192,30 @@ public class BasicDataSource implements 
         this.restartNeeded = true;
     }
 
-  
+    /**
+     * The property that controls if the pooled connections cache some state
+     * rather than query the database for current state to improve performance.
+     */
+    private boolean cacheState = true;
+
+    /**
+     * Returns the state caching flag.
+     * 
+     * @return  the state caching flag
+     */
+    public boolean getCacheState() {
+        return cacheState;
+    }
+
+    /**
+     * Sets the state caching flag.
+     * 
+     * @param cacheState    The new value for the state caching flag
+     */
+    public void setCacheState(boolean cacheState) {
+        this.cacheState = cacheState;
+    }
+
     /**
      * The fully qualified Java class name of the JDBC driver to be used.
      */
@@ -1752,6 +1775,7 @@ public class BasicDataSource implements 
                                               defaultAutoCommit,
                                               defaultTransactionIsolation,
                                               defaultCatalog,
+                                              cacheState,
                                               configuration);
             validateConnectionFactory(connectionFactory);
         } catch (RuntimeException e) {

Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java?rev=1101837&r1=1101836&r2=1101837&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSourceFactory.java Wed May 11 11:39:54 2011
@@ -49,6 +49,7 @@ public class BasicDataSourceFactory impl
     private final static String PROP_DEFAULTREADONLY = "defaultReadOnly";
     private final static String PROP_DEFAULTTRANSACTIONISOLATION = "defaultTransactionIsolation";
     private final static String PROP_DEFAULTCATALOG = "defaultCatalog";
+    private final static String PROP_CACHESTATE ="cacheState";
     private final static String PROP_DRIVERCLASSNAME = "driverClassName";
     private final static String PROP_LIFO = "lifo";
     private final static String PROP_MAXACTIVE = "maxActive";
@@ -87,6 +88,7 @@ public class BasicDataSourceFactory impl
         PROP_DEFAULTREADONLY,
         PROP_DEFAULTTRANSACTIONISOLATION,
         PROP_DEFAULTCATALOG,
+        PROP_CACHESTATE,
         PROP_DRIVERCLASSNAME,
         PROP_LIFO,
         PROP_MAXACTIVE,
@@ -218,6 +220,11 @@ public class BasicDataSourceFactory impl
             dataSource.setDefaultCatalog(value);
         }
 
+        value = properties.getProperty(PROP_CACHESTATE);
+        if (value != null) {
+            dataSource.setCacheState(Boolean.valueOf(value).booleanValue());
+        }
+
         value = properties.getProperty(PROP_DRIVERCLASSNAME);
         if (value != null) {
             dataSource.setDriverClassName(value);

Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/DelegatingConnection.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/DelegatingConnection.java?rev=1101837&r1=1101836&r2=1101837&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/DelegatingConnection.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/DelegatingConnection.java Wed May 11 11:39:54 2011
@@ -75,7 +75,7 @@ public class DelegatingConnection extend
 
     protected boolean _closed = false;
     
-    
+    private boolean _cacheState = true;
     private Boolean _autoCommitCached = null;
     private Boolean _readOnlyCached = null;
 
@@ -347,10 +347,19 @@ public class DelegatingConnection extend
     public void commit() throws SQLException
     { checkOpen(); try { _conn.commit(); } catch (SQLException e) { handleException(e); } }
     
+    /**
+     * Returns the state caching flag.
+     * 
+     * @return  the state caching flag
+     */
+    public boolean getCacheState() {
+        return _cacheState;
+    }
+
     @Override
     public boolean getAutoCommit() throws SQLException {
         checkOpen();
-        if (_autoCommitCached != null) {
+        if (_cacheState && _autoCommitCached != null) {
             return _autoCommitCached.booleanValue();
         }
         try {
@@ -392,7 +401,7 @@ public class DelegatingConnection extend
     @Override
     public boolean isReadOnly() throws SQLException {
         checkOpen();
-        if (_readOnlyCached != null) {
+        if (_cacheState && _readOnlyCached != null) {
             return _readOnlyCached.booleanValue();
         }
         try {
@@ -412,12 +421,23 @@ public class DelegatingConnection extend
     public void rollback() throws SQLException
     { checkOpen(); try {  _conn.rollback(); } catch (SQLException e) { handleException(e); } }
     
+    /**
+     * Sets the state caching flag.
+     * 
+     * @param cacheState    The new value for the state caching flag
+     */
+    public void setCacheState(boolean cacheState) {
+        this._cacheState = cacheState;
+    }
+
     @Override
     public void setAutoCommit(boolean autoCommit) throws SQLException {
         checkOpen();
         try {
             _conn.setAutoCommit(autoCommit);
-            _autoCommitCached = Boolean.valueOf(autoCommit);
+            if (_cacheState) {
+                _autoCommitCached = Boolean.valueOf(autoCommit);
+            }
         } catch (SQLException e) {
             _autoCommitCached = null;
             handleException(e);
@@ -433,7 +453,9 @@ public class DelegatingConnection extend
         checkOpen();
         try {
             _conn.setReadOnly(readOnly);
-            _readOnlyCached = Boolean.valueOf(readOnly);
+            if (_cacheState) {
+                _readOnlyCached = Boolean.valueOf(readOnly);
+            }
         } catch (SQLException e) {
             _readOnlyCached = null;
             handleException(e);

Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java?rev=1101837&r1=1101836&r2=1101837&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java Wed May 11 11:39:54 2011
@@ -443,6 +443,7 @@ public class PoolableConnectionFactory i
      * @param defaultTransactionIsolation the default "Transaction Isolation" setting for returned {@link Connection}s
      * @param defaultCatalog the default "catalog" setting for returned {@link Connection}s
      * @param config the AbandonedConfig if tracing SQL objects
+     * @param cacheState the state caching flag for returned {@link Connection}s
      * @since 1.3
      */
     public PoolableConnectionFactory(
@@ -473,6 +474,51 @@ public class PoolableConnectionFactory i
     }
 
     /**
+     * Create a new <tt>PoolableConnectionFactory</tt>.
+     * @param connFactory the {@link ConnectionFactory} from which to obtain base {@link Connection}s
+     * @param pool the {@link ObjectPool} in which to pool those {@link Connection}s
+     * @param stmtPoolFactory the {@link KeyedObjectPoolFactory} to use to create {@link KeyedObjectPool}s for pooling {@link java.sql.PreparedStatement}s, or <tt>null</tt> to disable {@link java.sql.PreparedStatement} pooling
+     * @param validationQuery a query to use to {@link #validateObject validate} {@link Connection}s.  Should return at least one row. Using <tt>null</tt> turns off validation.
+     * @param validationQueryTimeout the number of seconds that validation queries will wait for database response before failing.  Use a value less than or equal to 0 for no timeout.
+     * @param connectionInitSqls a Collection of SQL statements to initialize {@link Connection}s. Using <tt>null</tt> turns off initialization.
+     * @param defaultReadOnly the default "read only" setting for borrowed {@link Connection}s
+     * @param defaultAutoCommit the default "auto commit" setting for returned {@link Connection}s
+     * @param defaultTransactionIsolation the default "Transaction Isolation" setting for returned {@link Connection}s
+     * @param defaultCatalog the default "catalog" setting for returned {@link Connection}s
+     * @param config the AbandonedConfig if tracing SQL objects
+     * @param cacheState the state caching flag for returned {@link Connection}s
+     * @since 2.0
+     */
+    public PoolableConnectionFactory(
+        ConnectionFactory connFactory,
+        ObjectPool pool,
+        KeyedObjectPoolFactory stmtPoolFactory,
+        String validationQuery,
+        int validationQueryTimeout,
+        Collection connectionInitSqls,
+        Boolean defaultReadOnly,
+        boolean defaultAutoCommit,
+        int defaultTransactionIsolation,
+        String defaultCatalog,
+        boolean cacheState,
+        AbandonedConfig config) {
+
+        _connFactory = connFactory;
+        _pool = pool;
+        _config = config;
+        _pool.setFactory(this);
+        _stmtPoolFactory = stmtPoolFactory;
+        _validationQuery = validationQuery;
+        _validationQueryTimeout = validationQueryTimeout;
+        _connectionInitSqls = connectionInitSqls;
+        _defaultReadOnly = defaultReadOnly;
+        _defaultAutoCommit = defaultAutoCommit;
+        _defaultTransactionIsolation = defaultTransactionIsolation;
+        _defaultCatalog = defaultCatalog;
+        _cacheState = cacheState;
+    }
+
+    /**
      * Sets the {@link ConnectionFactory} from which to obtain base {@link Connection}s.
      * @param connFactory the {@link ConnectionFactory} from which to obtain base {@link Connection}s
      */
@@ -734,6 +780,7 @@ public class PoolableConnectionFactory i
     protected boolean _defaultAutoCommit = true;
     protected int _defaultTransactionIsolation = UNKNOWN_TRANSACTIONISOLATION;
     protected String _defaultCatalog;
+    private boolean _cacheState;
 
     /**
      * Configuration for removing abandoned connections.

Modified: commons/proper/dbcp/trunk/src/site/xdoc/configuration.xml
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/site/xdoc/configuration.xml?rev=1101837&r1=1101836&r2=1101837&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/site/xdoc/configuration.xml (original)
+++ commons/proper/dbcp/trunk/src/site/xdoc/configuration.xml Wed May 11 11:39:54 2011
@@ -112,6 +112,17 @@ testWhileIdle
    <td></td>
    <td>The default catalog of connections created by this pool.</td>
 </tr>
+<tr>
+  <td>cacheState</td>
+  <td>true</td>
+  <td>If true, the pooled connection will cache the current readOnly and
+      autoCommit settings when first read or written and on all subsequent
+      writes. This removes the need for additional database queries for any
+      further calls to the getter. If the underlying connection is accessed
+      directly and the readOnly and/or autoCommit settings changed the cached
+      values will not reflect the current state. In this case, caching should be
+      disabled by setting this attribute to false.</td>
+</tr>
 </table>