commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1152977 - in /commons/proper/dbcp/trunk/src: java/org/apache/commons/dbcp2/ java/org/apache/commons/dbcp2/managed/ test/org/apache/commons/dbcp2/ test/org/apache/commons/dbcp2/managed/
Date Tue, 02 Aug 2011 00:08:15 GMT
Author: markt
Date: Tue Aug  2 00:08:14 2011
New Revision: 1152977

URL: http://svn.apache.org/viewvc?rev=1152977&view=rev
Log:
Generics
These changes triggered some test failures which in turn prompted some improved error handling
in PoolableConnectionFactory

Modified:
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/BasicDataSource.java
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/PoolableConnectionFactory.java
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java
    commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp2/TestPoolingDataSource.java
    commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp2/managed/TestManagedDataSource.java

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=1152977&r1=1152976&r2=1152977&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 Tue Aug
 2 00:08:14 2011
@@ -1785,7 +1785,7 @@ public class BasicDataSource implements 
     }
 
     protected static void validateConnectionFactory(PoolableConnectionFactory connectionFactory)
throws Exception {
-        Connection conn = null;
+        PoolableConnection conn = null;
         try {
             conn = connectionFactory.makeObject();
             connectionFactory.activateObject(conn);
@@ -1793,7 +1793,9 @@ public class BasicDataSource implements 
             connectionFactory.passivateObject(conn);
         }
         finally {
-            connectionFactory.destroyObject(conn);
+            if (conn != null) {
+                connectionFactory.destroyObject(conn);
+            }
         }
     }
 

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=1152977&r1=1152976&r2=1152977&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
Tue Aug  2 00:08:14 2011
@@ -40,7 +40,7 @@ import org.apache.commons.pool2.impl.Whe
  * @version $Revision$ $Date$
  */
 public class PoolableConnectionFactory
-        implements PoolableObjectFactory<Connection> {
+        implements PoolableObjectFactory<PoolableConnection> {
 
     /**
      * Create a new <tt>PoolableConnectionFactory</tt>.
@@ -165,12 +165,23 @@ public class PoolableConnectionFactory
     }
 
     @Override
-    public Connection makeObject() throws Exception {
+    public PoolableConnection makeObject() throws Exception {
         Connection conn = _connFactory.createConnection();
         if (conn == null) {
             throw new IllegalStateException("Connection factory returned null from createConnection");
         }
-        initializeConnection(conn);
+        try {
+            initializeConnection(conn);
+        } catch (SQLException sqle) {
+            // Make sure the connection is closed
+            try {
+                conn.close();
+            } catch (SQLException ignore) {
+                // ignore
+            }
+            // Rethrow original exception so it is visible to caller
+            throw sqle;
+        }
         if(poolStatements) {
             conn = new PoolingConnection(conn);
             GenericKeyedObjectPoolConfig<PStmtKey,DelegatingPreparedStatement> config
=
@@ -217,14 +228,12 @@ public class PoolableConnectionFactory
     }
 
     @Override
-    public void destroyObject(Connection obj) throws Exception {
-        if(obj instanceof PoolableConnection) {
-            ((PoolableConnection)obj).reallyClose();
-        }
+    public void destroyObject(PoolableConnection obj) throws Exception {
+        obj.reallyClose();
     }
 
     @Override
-    public boolean validateObject(Connection conn) {
+    public boolean validateObject(PoolableConnection conn) {
         try {
             validateConnection(conn);
             return true;
@@ -270,7 +279,7 @@ public class PoolableConnectionFactory
     }
 
     @Override
-    public void passivateObject(Connection conn) throws Exception {
+    public void passivateObject(PoolableConnection conn) throws Exception {
         if(!conn.getAutoCommit() && !conn.isReadOnly()) {
             conn.rollback();
         }
@@ -279,16 +288,12 @@ public class PoolableConnectionFactory
             conn.setAutoCommit(true);
         }
 
-        if(conn instanceof DelegatingConnection) {
-            ((DelegatingConnection)conn).passivate();
-        }
+        conn.passivate();
     }
 
     @Override
-    public void activateObject(Connection conn) throws Exception {
-        if(conn instanceof DelegatingConnection) {
-            ((DelegatingConnection)conn).activate();
-        }
+    public void activateObject(PoolableConnection conn) throws Exception {
+        conn.activate();
 
         if (conn.getAutoCommit() != _defaultAutoCommit) {
             conn.setAutoCommit(_defaultAutoCommit);

Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java?rev=1152977&r1=1152976&r2=1152977&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java
(original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp2/managed/PoolableManagedConnectionFactory.java
Tue Aug  2 00:08:14 2011
@@ -18,6 +18,7 @@
 package org.apache.commons.dbcp2.managed;
 import java.sql.Connection;
 
+import org.apache.commons.dbcp2.PoolableConnection;
 import org.apache.commons.dbcp2.PoolableConnectionFactory;
 import org.apache.commons.dbcp2.PoolingConnection;
 import org.apache.commons.pool2.KeyedObjectPool;
@@ -53,7 +54,7 @@ public class PoolableManagedConnectionFa
      * if statement pooling is enabled.
      */
     @Override
-    synchronized public Connection makeObject() throws Exception {
+    synchronized public PoolableConnection makeObject() throws Exception {
         Connection conn = _connFactory.createConnection();
         if (conn == null) {
             throw new IllegalStateException("Connection factory returned null from createConnection");

Modified: commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp2/TestPoolingDataSource.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp2/TestPoolingDataSource.java?rev=1152977&r1=1152976&r2=1152977&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp2/TestPoolingDataSource.java
(original)
+++ commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp2/TestPoolingDataSource.java
Tue Aug  2 00:08:14 2011
@@ -93,37 +93,16 @@ public class TestPoolingDataSource exten
         }
     }
     
-    private void checkPoolGuardConnectionWrapperEqualsReflexive() throws Exception {
-        Connection con = ds.getConnection();
-        Connection con2 = con;
-        assertTrue(con2.equals(con));
-        assertTrue(con.equals(con2));
-        con.close();
-    }
-    
     /*
      * JIRA: DBCP-198
      */
     public void testPoolGuardConnectionWrapperEqualsReflexive()
         throws Exception {
-        // Statndard setup - using DelegatingConnections
-        // returned from PoolableConnectionFactory
-        checkPoolGuardConnectionWrapperEqualsReflexive();
-        // Force PoolGuardConnectionWrappers to wrap non-Delegating connections
-        pool.close();
-        
-        Properties props = new Properties();
-        props.setProperty("user", "username");
-        props.setProperty("password", "password");
-        NonDelegatingPoolableConnectionFactory factory = 
-            new NonDelegatingPoolableConnectionFactory(
-                new DriverConnectionFactory(new TesterDriver(),
-                        "jdbc:apache:commons:testdriver", props));
-        pool = new GenericObjectPool(factory);
-        pool.setMaxTotal(getMaxTotal());
-        pool.setMaxWait(getMaxWait());
-        ds = new PoolingDataSource(pool);
-        checkPoolGuardConnectionWrapperEqualsReflexive();
+        Connection con = ds.getConnection();
+        Connection con2 = con;
+        assertTrue(con2.equals(con));
+        assertTrue(con.equals(con2));
+        con.close();
     }
     
     public void testPoolGuardConnectionWrapperEqualsFail() throws Exception {
@@ -159,19 +138,4 @@ public class TestPoolingDataSource exten
         assertTrue(con2.innermostDelegateEquals(inner));
         assertTrue(con.equals(con2));
     }
-    
-    /** Factory to return non-delegating connections for DBCP-198 test */
-    private static class NonDelegatingPoolableConnectionFactory
-            extends PoolableConnectionFactory {
-        public NonDelegatingPoolableConnectionFactory(ConnectionFactory connFactory) {
-            super(connFactory);
-            super.setDefaultAutoCommit(true);
-            super.setDefaultReadOnly(true);
-        }
-    
-        @Override
-        synchronized public Connection makeObject() throws Exception {
-            return _connFactory.createConnection();
-        }
-    }
 }

Modified: commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp2/managed/TestManagedDataSource.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp2/managed/TestManagedDataSource.java?rev=1152977&r1=1152976&r2=1152977&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp2/managed/TestManagedDataSource.java
(original)
+++ commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp2/managed/TestManagedDataSource.java
Tue Aug  2 00:08:14 2011
@@ -178,25 +178,6 @@ public class TestManagedDataSource exten
     * JIRA: DBCP-198
     */
     public void testManagedConnectionEqualsReflexive() throws Exception {
-        // Statndard setup - using DelegatingConnections
-        // returned from PoolableConnectionFactory
-        checkManagedConnectionEqualsReflexive();
-
-        // Force ManagedConnections to wrap non-Delegating connections
-        Properties props = new Properties();
-        props.setProperty("user", "username");
-        props.setProperty("password", "password");
-        NonDelegatingPoolableConnectionFactory factory = new NonDelegatingPoolableConnectionFactory(
-                new DriverConnectionFactory(new TesterDriver(), "jdbc:apache:commons:testdriver",
props));
-        pool.close();
-        pool = new GenericObjectPool(factory);
-        pool.setMaxTotal(getMaxTotal());
-        pool.setMaxWait(getMaxWait());
-        ds = new PoolingDataSource(pool);
-        checkManagedConnectionEqualsReflexive();
-    }
-
-    private void checkManagedConnectionEqualsReflexive() throws Exception {
         Connection con = ds.getConnection();
         Connection con2 = con;
         assertTrue(con2.equals(con));
@@ -237,21 +218,4 @@ public class TestManagedDataSource exten
         assertTrue(con2.innermostDelegateEquals(inner));
         assertTrue(con.equals(con2));
     }
-
-    /**
-     * Factory to return non-delegating connections for DBCP-198 test
-     */
-    private static class NonDelegatingPoolableConnectionFactory
-            extends PoolableConnectionFactory {
-        public NonDelegatingPoolableConnectionFactory(ConnectionFactory connFactory) {
-            super(connFactory);
-            super.setDefaultAutoCommit(true);
-            super.setDefaultReadOnly(true);
-        }
-
-        @Override
-        synchronized public Connection makeObject() throws Exception {
-            return _connFactory.createConnection();
-        }
-    }
 }



Mime
View raw message