commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1568253 - 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 11:46:47 GMT
Author: markt
Date: Fri Feb 14 11:46:46 2014
New Revision: 1568253

URL: http://svn.apache.org/r1568253
Log:
Fix DBCP-351, DBCP-399
Add two new options:
enableAutoCommitOnReturn
rollbackOnReturn

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/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=1568253&r1=1568252&r2=1568253&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/changes/changes.xml (original)
+++ commons/proper/dbcp/trunk/src/changes/changes.xml Fri Feb 14 11:46:46 2014
@@ -157,6 +157,14 @@ The <action> type attribute can be add,u
         represents a significant refactoring of these classes and a number of
         property names have changed as a result. 
       </action>
+      <action dev="markt" issue="DBCP-351" type="add">
+        Provide an option to control if autoCommit is always set to true when a
+        connection is returned to the connection pool.
+      </action>
+      <action dev="markt" issue="DBCP-399" type="add">
+        Provide an option to control if rollback is called when a connection is
+        returned to the poll with autoCommit disabled.
+      </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=1568253&r1=1568252&r2=1568253&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 11:46:46 2014
@@ -1276,6 +1276,52 @@ public class BasicDataSource
     }
 
 
+    private boolean enableAutoCommitOnReturn = true;
+
+    /**
+     * Returns the value of the flag that controls whether or not connections
+     * being returned to the pool will checked and configured with
+     * {@link Connection#setAutoCommit(boolean) Connection.setAutoCommit(true)}
+     * if the auto commit setting is <code>false</false> when the connection
+     * is returned. It is <code>true</code> by default.
+     */
+    public boolean getEnableAutoCommitOnReturn() {
+        return enableAutoCommitOnReturn;
+    }
+
+    /**
+     * Sets the value of the flag that controls whether or not connections
+     * being returned to the pool will checked and configured with
+     * {@link Connection#setAutoCommit(boolean) Connection.setAutoCommit(true)}
+     * if the auto commit setting is <code>false</false> when the connection
+     * is returned. It is <code>true</code> by default.
+     */
+    public void setEnableAutoCommitOnReturn(boolean enableAutoCommitOnReturn) {
+        this.enableAutoCommitOnReturn = enableAutoCommitOnReturn;
+    }
+
+
+    private boolean rollbackOnReturn = true;
+
+    /**
+     * Gets the current value of the flag that controls if a connection will be
+     * rolled back when it is returned to the pool if auto commit is not enabled
+     * and the connection is not read only.
+     */
+    public boolean getRollbackOnReturn() {
+        return rollbackOnReturn;
+    }
+
+    /**
+     * Sets the flag that controls if a connection will be rolled back when it
+     * is returned to the pool if auto commit is not enabled and the connection
+     * is not read only.
+     */
+    public void setRollbackOnReturn(boolean rollbackOnReturn) {
+        this.rollbackOnReturn = rollbackOnReturn;
+    }
+
+
     // ----------------------------------------------------- Instance Variables
 
     /**
@@ -2073,6 +2119,8 @@ public class BasicDataSource
             connectionFactory.setMaxOpenPrepatedStatements(
                     maxOpenPreparedStatements);
             connectionFactory.setMaxConnLifetimeMillis(maxConnLifetimeMillis);
+            connectionFactory.setRollbackOnReturn(getRollbackOnReturn());
+            connectionFactory.setEnableAutoCommitOnReturn(getEnableAutoCommitOnReturn());
             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=1568253&r1=1568252&r2=1568253&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 11:46:46 2014
@@ -93,6 +93,8 @@ public class BasicDataSourceFactory impl
     private final static String PROP_MAXOPENPREPAREDSTATEMENTS = "maxOpenPreparedStatements";
     private final static String PROP_CONNECTIONPROPERTIES = "connectionProperties";
     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[] ALL_PROPERTIES = {
         PROP_DEFAULTAUTOCOMMIT,
@@ -129,7 +131,9 @@ public class BasicDataSourceFactory impl
         PROP_POOLPREPAREDSTATEMENTS,
         PROP_MAXOPENPREPAREDSTATEMENTS,
         PROP_CONNECTIONPROPERTIES,
-        PROP_MAXCONNLIFETIMEMILLIS
+        PROP_MAXCONNLIFETIMEMILLIS,
+        PROP_ROLLBACK_ON_RETURN,
+        PROP_ENABLE_AUTOCOMMIT_ON_RETURN
     };
 
     // -------------------------------------------------- ObjectFactory Methods
@@ -406,6 +410,16 @@ public class BasicDataSourceFactory impl
             dataSource.setJmxName(value);
         }
 
+        value = properties.getProperty(PROP_ENABLE_AUTOCOMMIT_ON_RETURN);
+        if (value != null) {
+            dataSource.setEnableAutoCommitOnReturn(Boolean.valueOf(value).booleanValue());
+        }
+
+        value = properties.getProperty(PROP_ROLLBACK_ON_RETURN);
+        if (value != null) {
+            dataSource.setRollbackOnReturn(Boolean.valueOf(value).booleanValue());
+        }
+
         // 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/PoolableConnectionFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/main/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java?rev=1568253&r1=1568252&r2=1568253&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 11:46:46 2014
@@ -173,6 +173,25 @@ public class PoolableConnectionFactory
         this.maxConnLifetimeMillis = maxConnLifetimeMillis;
     }
 
+
+    public boolean isEnableAutoCommitOnReturn() {
+        return enableAutoCommitOnReturn;
+    }
+
+    public void setEnableAutoCommitOnReturn(boolean enableAutoCommitOnReturn) {
+        this.enableAutoCommitOnReturn = enableAutoCommitOnReturn;
+    }
+
+
+    public boolean isRollbackOnReturn() {
+        return rollbackOnReturn;
+    }
+
+    public void setRollbackOnReturn(boolean rollbackOnReturn) {
+        this.rollbackOnReturn = rollbackOnReturn;
+    }
+
+
     @Override
     public PooledObject<PoolableConnection> makeObject() throws Exception {
         Connection conn = _connFactory.createConnection();
@@ -312,14 +331,25 @@ public class PoolableConnectionFactory
         validateLifetime(p);
 
         PoolableConnection conn = p.getObject();
-        boolean connAutoCommit = conn.getAutoCommit();
-        if(!connAutoCommit && !conn.isReadOnly()) {
-            conn.rollback();
+        Boolean connAutoCommit = null;
+        if (rollbackOnReturn) {
+            connAutoCommit = Boolean.valueOf(conn.getAutoCommit());
+            if(!connAutoCommit.booleanValue() && !conn.isReadOnly()) {
+                conn.rollback();
+            }
         }
+
         conn.clearWarnings();
-        // DBCP-97 Idle connections in the pool should have autoCommit enabled
-        if(!connAutoCommit) {
-            conn.setAutoCommit(true);
+
+        // DBCP-97 / DBCP-399 / DBCP-351 Idle connections in the pool should
+        // have autoCommit enabled
+        if (enableAutoCommitOnReturn) {
+            if (connAutoCommit == null) {
+                connAutoCommit = Boolean.valueOf(conn.getAutoCommit());
+            }
+            if(!connAutoCommit.booleanValue()) {
+                conn.setAutoCommit(true);
+            }
         }
 
         conn.passivate();
@@ -390,6 +420,8 @@ public class PoolableConnectionFactory
     private volatile ObjectPool<PoolableConnection> _pool = null;
     private Boolean _defaultReadOnly = null;
     private Boolean _defaultAutoCommit = null;
+    private boolean enableAutoCommitOnReturn = true;
+    private boolean rollbackOnReturn = true;
     private int _defaultTransactionIsolation = UNKNOWN_TRANSACTIONISOLATION;
     private String _defaultCatalog;
     private boolean _cacheState;

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=1568253&r1=1568252&r2=1568253&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 11:46:46 2014
@@ -192,10 +192,13 @@ public class BasicManagedDataSource exte
             connectionFactory.setDefaultAutoCommit(getDefaultAutoCommit());
             connectionFactory.setDefaultTransactionIsolation(getDefaultTransactionIsolation());
             connectionFactory.setDefaultCatalog(getDefaultCatalog());
+            connectionFactory.setCacheState(getCacheState());
             connectionFactory.setPoolStatements(isPoolPreparedStatements());
             connectionFactory.setMaxOpenPrepatedStatements(
                     getMaxOpenPreparedStatements());
             connectionFactory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis());
+            connectionFactory.setRollbackOnReturn(getRollbackOnReturn());
+            connectionFactory.setEnableAutoCommitOnReturn(getEnableAutoCommitOnReturn());
             validateConnectionFactory(connectionFactory);
         } catch (RuntimeException e) {
             throw e;



Mime
View raw message