Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 97646 invoked from network); 23 Sep 2007 19:50:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 Sep 2007 19:50:37 -0000 Received: (qmail 87930 invoked by uid 500); 23 Sep 2007 19:50:27 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 87853 invoked by uid 500); 23 Sep 2007 19:50:26 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 87844 invoked by uid 99); 23 Sep 2007 19:50:26 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 23 Sep 2007 12:50:26 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 23 Sep 2007 19:52:41 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id EF5401A9832; Sun, 23 Sep 2007 12:50:09 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r578597 - in /commons/proper/dbcp/trunk: ./ src/java/org/apache/commons/dbcp/ src/test/org/apache/commons/dbcp/ xdocs/ Date: Sun, 23 Sep 2007 19:50:09 -0000 To: commits@commons.apache.org From: psteitz@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20070923195009.EF5401A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ org/apache/commons/dbcp/TestBasicDataSourceFactory.java org/apache/commons/dbcp/TestBasicDataSource.java org/apache/commons/dbcp/TestAbandonedBasicDataSource.java + org/apache/commons/dbcp/TestPStmtPooling.java org/apache/commons/dbcp/TestPStmtPoolingBasicDataSource.java org/apache/commons/dbcp/TestPoolingDataSource.java org/apache/commons/dbcp/TestJndi.java 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. + + Eliminated potential sources of NullPointerExceptions in + PoolingConnection. +