You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2006/03/09 15:41:10 UTC
svn commit: r384516 - in /jakarta/commons/proper/dbcp/trunk: project.xml
src/java/org/apache/commons/dbcp/PoolableConnection.java
src/test/org/apache/commons/dbcp/TestAll.java
src/test/org/apache/commons/dbcp/TestPoolableConnection.java
Author: psteitz
Date: Thu Mar 9 06:41:07 2006
New Revision: 384516
URL: http://svn.apache.org/viewcvs?rev=384516&view=rev
Log:
Modified PoolableConnection close method to invalidate instance
when invoked on an already closed connection.
Addresses BZ #33591
Contributed by Huw Lewis, Bill Ring
Added:
jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolableConnection.java (with props)
Modified:
jakarta/commons/proper/dbcp/trunk/project.xml
jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolableConnection.java
jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java
Modified: jakarta/commons/proper/dbcp/trunk/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/dbcp/trunk/project.xml?rev=384516&r1=384515&r2=384516&view=diff
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/project.xml (original)
+++ jakarta/commons/proper/dbcp/trunk/project.xml Thu Mar 9 06:41:07 2006
@@ -149,6 +149,12 @@
<email>yoavs@apache.org</email>
<organization>Apache Software Foundation</organization>
</developer>
+ <developer>
+ <name>Phil Steitz</name>
+ <id>psteitz</id>
+ <email></email>
+ <organization></organization>
+ </developer>
</developers>
<contributors>
<contributor>
@@ -176,7 +182,8 @@
downloaded to your Maven repository, and it's not even
required on JDK 1.4. Maybe we should remove it from
this dependency list so Maven doesn't choke?
- -->
+ -->
+
<dependency>
<groupId>javax.sql</groupId>
<artifactId>jdbc-stdext</artifactId>
@@ -217,6 +224,7 @@
<include>org/apache/commons/dbcp/TestAbandonedObjectPool.java</include>
<include>org/apache/commons/jocl/TestJOCLContentHandler.java</include>
<include>org/apache/commons/dbcp/TestDelegatingConnection.java</include>
+ <include>org/apache/commons/dbcp/TestPoolableConnection.java</include>
<include>org/apache/commons/dbcp/TestDelegatingPreparedStatement.java</include>
<include>org/apache/commons/dbcp/TestDelegatingStatement.java</include>
<include>org/apache/commons/dbcp/TestJOCLed.java</include>
Modified: jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolableConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolableConnection.java?rev=384516&r1=384515&r2=384516&view=diff
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolableConnection.java (original)
+++ jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolableConnection.java Thu Mar 9 06:41:07 2006
@@ -74,6 +74,11 @@
throw new SQLNestedException("Cannot close connection (isClosed check failed)", e);
}
if (isClosed) {
+ try {
+ _pool.invalidateObject(this);
+ } catch (Exception ie) {
+ // DO NOTHING, "Already closed" exception thrown below
+ }
throw new SQLException("Already closed.");
} else {
try {
Modified: jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java?rev=384516&r1=384515&r2=384516&view=diff
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java (original)
+++ jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java Thu Mar 9 06:41:07 2006
@@ -42,6 +42,7 @@
suite.addTest(TestDelegatingConnection.suite());
suite.addTest(TestDelegatingStatement.suite());
suite.addTest(TestDelegatingPreparedStatement.suite());
+ suite.addTest(TestPoolableConnection.suite());
suite.addTest(TestSharedPoolDataSource.suite());
suite.addTest(TestPerUserPoolDataSource.suite());
suite.addTest(TestFactory.suite());
Added: jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolableConnection.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolableConnection.java?rev=384516&view=auto
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolableConnection.java (added)
+++ jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolableConnection.java Thu Mar 9 06:41:07 2006
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.dbcp;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.pool.ObjectPool;
+import org.apache.commons.pool.impl.GenericObjectPool;
+
+/**
+ * @author James Ring
+ * @version $Revision$ $Date$
+ */
+public class TestPoolableConnection extends TestCase {
+ public TestPoolableConnection(String testName) {
+ super(testName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(TestPoolableConnection.class);
+ }
+
+ private ObjectPool pool = null;
+
+ public void setUp() throws Exception {
+ pool = new GenericObjectPool();
+ PoolableConnectionFactory factory =
+ new PoolableConnectionFactory(
+ new DriverConnectionFactory(new TesterDriver(),"jdbc:apache:commons:testdriver", null),
+ pool, null, null, true, true);
+ pool.setFactory(factory);
+ }
+
+ public void testConnectionPool() {
+ // Grab a new connection from the pool
+ Connection c = null;
+ try {
+ c = (Connection)pool.borrowObject();
+ } catch (Exception e) {
+ fail("Could not fetch Connection from pool: " + e.getMessage());
+ }
+
+ assertTrue("Connection should be created and should not be null", c != null);
+ assertEquals("There should be exactly one active object in the pool", 1, pool.getNumActive());
+
+ // Now return the connection by closing it
+ try {
+ c.close();
+ } catch (SQLException e) {
+ fail("Could not close connection: " + e.getMessage());
+ }
+
+ assertEquals("There should now be zero active objects in the pool", 0, pool.getNumActive());
+ }
+
+ // Bugzilla Bug 33591: PoolableConnection leaks connections if the
+ // delegated connection closes itself.
+ public void testPoolableConnectionLeak() {
+ Connection conn = null;
+ try {
+ // 'Borrow' a connection from the pool
+ conn = (Connection)pool.borrowObject();
+
+ // Now close our innermost delegate, simulating the case where the
+ // underlying connection closes itself
+ ((PoolableConnection)conn).getInnermostDelegate().close();
+
+ // At this point, we can close the pooled connection. The
+ // PoolableConnection *should* realise that its underlying
+ // connection is gone and invalidate itself. The pool should have no
+ // active connections.
+ } catch (Exception e) {
+ fail("Exception occured while testing connection leak: " + e.getMessage());
+ }
+
+ try {
+ conn.close();
+ } catch (Exception e) {
+ // Here we expect 'connection already closed', but the connection
+ // should *NOT* be returned to the pool
+ }
+
+ assertEquals("The pool should have no active connections",
+ 0, pool.getNumActive());
+ }
+}
Propchange: jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolableConnection.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org