You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ps...@apache.org on 2007/09/23 21:50:09 UTC

svn commit: r578597 - in /commons/proper/dbcp/trunk: ./ src/java/org/apache/commons/dbcp/ src/test/org/apache/commons/dbcp/ xdocs/

Author: psteitz
Date: Sun Sep 23 12:50:08 2007
New Revision: 578597

URL: http://svn.apache.org/viewvc?rev=578597&view=rev
Log:
Eliminated potential sources of NullPointerExceptions in PoolingConnection.
JIRA: DBCP-241

Modified:
    commons/proper/dbcp/trunk/pom.xml
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/DelegatingConnection.java
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingConnection.java
    commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestDelegatingConnection.java
    commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPStmtPooling.java
    commons/proper/dbcp/trunk/xdocs/changes.xml

Modified: commons/proper/dbcp/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/pom.xml?rev=578597&r1=578596&r2=578597&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/pom.xml (original)
+++ commons/proper/dbcp/trunk/pom.xml Sun Sep 23 12:50:08 2007
@@ -241,6 +241,7 @@
                 <include>org/apache/commons/dbcp/TestBasicDataSourceFactory.java</include>
                 <include>org/apache/commons/dbcp/TestBasicDataSource.java</include>
                 <include>org/apache/commons/dbcp/TestAbandonedBasicDataSource.java</include>
+                <include>org/apache/commons/dbcp/TestPStmtPooling.java</include>
                 <include>org/apache/commons/dbcp/TestPStmtPoolingBasicDataSource.java</include>
                 <include>org/apache/commons/dbcp/TestPoolingDataSource.java</include>
                 <include>org/apache/commons/dbcp/TestJndi.java</include>

Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/DelegatingConnection.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/DelegatingConnection.java?rev=578597&r1=578596&r2=578597&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/DelegatingConnection.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/DelegatingConnection.java Sun Sep 23 12:50:08 2007
@@ -358,8 +358,13 @@
 
     protected void checkOpen() throws SQLException {
         if(_closed) {
-            throw new SQLException
-                ("Connection " + _conn + " is closed.");
+            if (null != _conn) {
+                throw new SQLException
+                    ("Connection " + _conn + " is closed.");
+            } else {
+                throw new SQLException
+                    ("Connection is null.");
+            }      
         }
     }
 

Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingConnection.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingConnection.java?rev=578597&r1=578596&r2=578597&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingConnection.java (original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingConnection.java Sun Sep 23 12:50:08 2007
@@ -88,6 +88,10 @@
      * @return a {@link PoolablePreparedStatement}
      */
     public PreparedStatement prepareStatement(String sql) throws SQLException {
+        if (null == _pstmtPool) {
+            throw new SQLException(
+                    "Statement pool is null - closed or invalid PoolingConnection.");
+        }
         try {
             return(PreparedStatement)(_pstmtPool.borrowObject(createKey(sql)));
         } catch(NoSuchElementException e) {
@@ -104,6 +108,10 @@
      * @return a {@link PoolablePreparedStatement}
      */
     public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
+        if (null == _pstmtPool) {
+            throw new SQLException(
+                    "Statement pool is null - closed or invalid PoolingConnection.");
+        }
         try {
             return(PreparedStatement)(_pstmtPool.borrowObject(createKey(sql,resultSetType,resultSetConcurrency)));
         } catch(NoSuchElementException e) {
@@ -245,7 +253,11 @@
     }
 
     public String toString() {
-        return "PoolingConnection: " + _pstmtPool.toString();
+        if (_pstmtPool != null ) {
+            return "PoolingConnection: " + _pstmtPool.toString();
+        } else {
+            return "PoolingConnection: null";
+        }
     }
 
     /**

Modified: commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestDelegatingConnection.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestDelegatingConnection.java?rev=578597&r1=578596&r2=578597&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestDelegatingConnection.java (original)
+++ commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestDelegatingConnection.java Sun Sep 23 12:50:08 2007
@@ -24,6 +24,8 @@
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
+import org.apache.commons.pool.impl.GenericKeyedObjectPool;
+
 /**
  * @author Dirk Verbeeck
  * @version $Revision$ $Date$
@@ -90,5 +92,39 @@
         } catch (SQLException ex) {
             // expected
         }      
+    }
+    
+    /**
+     * Verify fix for DBCP-241
+     */
+    public void testCheckOpenNull() throws Exception {
+        try {
+            conn.close();
+            conn.checkOpen();
+            fail("Expecting SQLException");
+        } catch (SQLException ex) {
+            assertTrue(ex.getMessage().endsWith("is closed."));
+        }
+
+        try {
+            conn = new DelegatingConnection(null);
+            conn._closed = true;  
+            conn.checkOpen();
+            fail("Expecting SQLException");
+        } catch (SQLException ex) {
+            assertTrue(ex.getMessage().endsWith("is null."));
+        }
+
+        try {
+            PoolingConnection pc = new PoolingConnection
+                (delegateConn2, new GenericKeyedObjectPool());
+            conn = new DelegatingConnection(pc);
+            pc.close();
+            conn.close();
+            conn.prepareStatement("");
+            fail("Expecting SQLException");
+        } catch (SQLException ex) {
+            assertTrue(ex.getMessage().endsWith("invalid PoolingConnection."));
+        }   
     }
 }

Modified: commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPStmtPooling.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPStmtPooling.java?rev=578597&r1=578596&r2=578597&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPStmtPooling.java (original)
+++ commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPStmtPooling.java Sun Sep 23 12:50:08 2007
@@ -19,6 +19,7 @@
 
 import java.sql.Connection;
 import java.sql.Statement;
+import java.sql.SQLException;
 
 import javax.sql.DataSource;
 
@@ -68,5 +69,35 @@
         Statement ustmt2 = ((DelegatingStatement) stmt2).getInnermostDelegate();
         stmt2.close();
         assertSame(ustmt1, ustmt2);
+    }
+    
+    public void testClosePool() throws Exception {
+        new TesterDriver();
+        ConnectionFactory connFactory = new DriverManagerConnectionFactory(
+                "jdbc:apache:commons:testdriver","u1","p1");
+
+        ObjectPool connPool = new GenericObjectPool();
+        KeyedObjectPoolFactory stmtPoolFactory = new GenericKeyedObjectPoolFactory(null);
+
+        PoolableConnectionFactory x = new PoolableConnectionFactory(
+                connFactory, connPool, stmtPoolFactory,
+                null, false, true);
+
+        DataSource ds = new PoolingDataSource(connPool);
+        ((PoolingDataSource) ds).setAccessToUnderlyingConnectionAllowed(true);
+
+        Connection conn = ds.getConnection();
+        Statement stmt = conn.prepareStatement("select 1 from dual");
+        
+        Connection poolableConnection = ((DelegatingConnection) conn).getDelegate();
+        Connection poolingConnection = 
+            ((DelegatingConnection) poolableConnection).getDelegate();
+        poolingConnection.close();
+        try {
+            stmt = conn.prepareStatement("select 1 from dual");
+            fail("Expecting SQLException");
+        } catch (SQLException ex) {
+            assertTrue(ex.getMessage().endsWith("invalid PoolingConnection."));
+        }     
     }
 }

Modified: commons/proper/dbcp/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/xdocs/changes.xml?rev=578597&r1=578596&r2=578597&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/xdocs/changes.xml (original)
+++ commons/proper/dbcp/trunk/xdocs/changes.xml Sun Sep 23 12:50:08 2007
@@ -88,6 +88,10 @@
         idle connections are destroyed and the method returns.  As the remaining
         active connections are closed, they are destroyed.
       </action>
+      <action dev="psteitz" type="fix" issue="DBCP-241">
+        Eliminated potential sources of NullPointerExceptions in 
+        PoolingConnection.
+      </action>
     </release>
     <release version="1.2.2" date="2007-04-04"
       description="This is a maintenance release containing bug fixes