Author: dain Date: Mon Jul 23 15:28:37 2007 New Revision: 558884 URL: http://svn.apache.org/viewvc?view=rev&rev=558884 Log: DBCP-221 Changed BasicDataSource.close() to permanently mark the data source as closed. At close all idle connections are destroyed and the method returns. As existing active connections are closed, they are destroyed.
Added: jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/managed/TestBasicManagedDataSource.java Modified: jakarta/commons/proper/dbcp/trunk/pom.xml jakarta/commons/proper/dbcp/trunk/project.xml jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolableConnection.java jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/ManagedConnection.java jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestBasicDataSource.java Modified: jakarta/commons/proper/dbcp/trunk/pom.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/pom.xml?view=diff&rev=558884&r1=558883&r2=558884 ============================================================================== --- jakarta/commons/proper/dbcp/trunk/pom.xml (original) +++ jakarta/commons/proper/dbcp/trunk/pom.xml Mon Jul 23 15:28:37 2007 @@ -236,6 +236,7 @@ <include>org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java</include> <include>org/apache/commons/dbcp/datasources/TestSharedPoolDataSource.java</include> + <include>org/apache/commons/dbcp/managed/TestBasicManagedDataSource.java</include> <include>org/apache/commons/dbcp/managed/TestManagedDataSource.java</include> <include>org/apache/commons/dbcp/managed/TestManagedDataSourceInTx.java</include> </includes> Modified: jakarta/commons/proper/dbcp/trunk/project.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/project.xml?view=diff&rev=558884&r1=558883&r2=558884 ============================================================================== --- jakarta/commons/proper/dbcp/trunk/project.xml (original) +++ jakarta/commons/proper/dbcp/trunk/project.xml Mon Jul 23 15:28:37 2007 @@ -354,6 +354,10 @@ <include>org/apache/commons/dbcp/datasources/TestFactory.java</include> <include>org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java</include> <include>org/apache/commons/dbcp/datasources/TestSharedPoolDataSource.java</include> + + <include>org/apache/commons/dbcp/managed/TestBasicManagedDataSource.java</include> + <include>org/apache/commons/dbcp/managed/TestManagedDataSource.java</include> + <include>org/apache/commons/dbcp/managed/TestManagedDataSourceInTx.java</include> </includes> <resources> <resource> Modified: jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java?view=diff&rev=558884&r1=558883&r2=558884 ============================================================================== --- jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java (original) +++ jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/BasicDataSource.java Mon Jul 23 15:28:37 2007 @@ -1093,7 +1093,7 @@ } /** - * Sets the connection properties passed to driver.connect(...). + * Sets the connection properties passed to driver.connect(...). * * Format of the string must be [propertyName=property;]* * @@ -1126,13 +1126,18 @@ this.restartNeeded = true; } + protected boolean closed; + /** * Close and release all connections that are currently stored in the - * connection pool associated with our data source. + * connection pool associated with our data source. All open (active) + * connection remain open until closed. Once the data source has + * been closed, no more connections can be obtained. * * @throws SQLException if a database error occurs */ public synchronized void close() throws SQLException { + closed = true; GenericObjectPool oldpool = connectionPool; connectionPool = null; dataSource = null; @@ -1149,6 +1154,13 @@ } } + /** + * If true, this data source is closed and no more connections can be retrieved from this datasource. + * @return true, if the data source is closed; false otherwise + */ + public synchronized boolean isClosed() { + return closed; + } // ------------------------------------------------------ Protected Methods @@ -1167,6 +1179,9 @@ */ protected synchronized DataSource createDataSource() throws SQLException { + if (closed) { + throw new SQLException("Data source is closed"); + } // Return the pool if we have already created it if (dataSource != null) { Modified: jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolableConnection.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolableConnection.java?view=diff&rev=558884&r1=558883&r2=558884 ============================================================================== --- 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 Mon Jul 23 15:28:37 2007 @@ -70,6 +70,10 @@ } catch (SQLException e) { try { _pool.invalidateObject(this); // XXX should be guarded to happen at most once + } catch(IllegalStateException ise) { + // pool is closed, so close the connection + passivate(); + getInnermostDelegate().close(); } catch (Exception ie) { // DO NOTHING the original exception will be rethrown } @@ -78,6 +82,10 @@ if (isClosed) { try { _pool.invalidateObject(this); // XXX should be guarded to happen at most once + } catch(IllegalStateException e) { + // pool is closed, so close the connection + passivate(); + getInnermostDelegate().close(); } catch (Exception ie) { // DO NOTHING, "Already closed" exception thrown below } @@ -85,6 +93,10 @@ } else { try { _pool.returnObject(this); // XXX should be guarded to happen at most once + } catch(IllegalStateException e) { + // pool is closed, so close the connection + passivate(); + getInnermostDelegate().close(); } catch(SQLException e) { throw e; } catch(RuntimeException e) { Modified: jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/ManagedConnection.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/ManagedConnection.java?view=diff&rev=558884&r1=558883&r2=558884 ============================================================================== --- jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/ManagedConnection.java (original) +++ jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/ManagedConnection.java Mon Jul 23 15:28:37 2007 @@ -139,13 +139,7 @@ } public void close() throws SQLException { - // close can be called multiple times, but PoolableConnection improperly - // throws an exception when a connection is closed twice, so before calling - // close we aren't alreayd closed - if (!isClosed()) { - - // don't use super.close() because it calls passivate() which marks the - // the connection as cloased without returning it to the pool + if (!_closed) { try { // don't actually close the connection if in a transaction // the connection will be closed by the transactionComplete method Modified: jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestAll.java?view=diff&rev=558884&r1=558883&r2=558884 ============================================================================== --- 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 Mon Jul 23 15:28:37 2007 @@ -17,10 +17,15 @@ package org.apache.commons.dbcp; -import junit.framework.*; -import org.apache.commons.dbcp.datasources.TestSharedPoolDataSource; -import org.apache.commons.dbcp.datasources.TestPerUserPoolDataSource; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; import org.apache.commons.dbcp.datasources.TestFactory; +import org.apache.commons.dbcp.datasources.TestPerUserPoolDataSource; +import org.apache.commons.dbcp.datasources.TestSharedPoolDataSource; +import org.apache.commons.dbcp.managed.TestBasicManagedDataSource; +import org.apache.commons.dbcp.managed.TestManagedDataSource; +import org.apache.commons.dbcp.managed.TestManagedDataSourceInTx; import org.apache.commons.jocl.TestJOCLContentHandler; /** @@ -51,6 +56,9 @@ suite.addTest(TestJOCLContentHandler.suite()); suite.addTest(TestPoolingDataSource.suite()); suite.addTest(TestJndi.suite()); + suite.addTest(TestBasicManagedDataSource.suite()); + suite.addTest(TestManagedDataSource.suite()); + suite.addTest(TestManagedDataSourceInTx.suite()); return suite; } Modified: jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestBasicDataSource.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestBasicDataSource.java?view=diff&rev=558884&r1=558883&r2=558884 ============================================================================== --- jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestBasicDataSource.java (original) +++ jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestBasicDataSource.java Mon Jul 23 15:28:37 2007 @@ -48,7 +48,7 @@ public void setUp() throws Exception { super.setUp(); - ds = new BasicDataSource(); + ds = createDataSource(); ds.setDriverClassName("org.apache.commons.dbcp.TesterDriver"); ds.setUrl("jdbc:apache:commons:testdriver"); ds.setMaxActive(getMaxActive()); @@ -62,12 +62,53 @@ ds.setValidationQuery("SELECT DUMMY FROM DUAL"); } + protected BasicDataSource createDataSource() throws Exception { + return new BasicDataSource(); + } + public void tearDown() throws Exception { super.tearDown(); ds.close(); ds = null; } + public void testClose() throws Exception { + ds.setAccessToUnderlyingConnectionAllowed(true); + + // active conneciton is held open when ds is closed + Connection activeConnection = getConnection(); + Connection rawActiveConnection = ((DelegatingConnection) activeConnection).getInnermostDelegate(); + assertFalse(activeConnection.isClosed()); + assertFalse(rawActiveConnection.isClosed()); + + // idle connection is in pool but closed + Connection idleConnection = getConnection(); + Connection rawIdleConnection = ((DelegatingConnection) idleConnection).getInnermostDelegate(); + assertFalse(idleConnection.isClosed()); + assertFalse(rawIdleConnection.isClosed()); + + // idle wrapper should be closed but raw conneciton should be open + idleConnection.close(); + assertTrue(idleConnection.isClosed()); + assertFalse(rawIdleConnection.isClosed()); + + ds.close(); + + // raw idle connection should now be closed + assertFalse(rawIdleConnection.isClosed()); + + // active connection should still be open + assertFalse(activeConnection.isClosed()); + assertFalse(rawActiveConnection.isClosed()); + + // now close the active connection + activeConnection.close(); + + // both wrapper and raw active connection should be closed + assertTrue(activeConnection.isClosed()); + assertTrue(rawActiveConnection.isClosed()); + } + public void testSetProperties() throws Exception { // normal ds.setConnectionProperties("name1=value1;name2=value2;name3=value3"); @@ -359,7 +400,7 @@ public void testCreateDataSourceCleanupThreads() throws Exception { ds.close(); ds = null; - ds = new BasicDataSource(); + ds = createDataSource(); ds.setDriverClassName("org.apache.commons.dbcp.TesterDriver"); ds.setUrl("jdbc:apache:commons:testdriver"); ds.setMaxActive(getMaxActive()); Added: jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/managed/TestBasicManagedDataSource.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/managed/TestBasicManagedDataSource.java?view=auto&rev=558884 ============================================================================== --- jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/managed/TestBasicManagedDataSource.java (added) +++ jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/managed/TestBasicManagedDataSource.java Mon Jul 23 15:28:37 2007 @@ -0,0 +1,47 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.managed; + +import org.apache.commons.dbcp.BasicDataSource; +import org.apache.commons.dbcp.TestBasicDataSource; +import org.apache.geronimo.transaction.manager.TransactionManagerImpl; +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * TestSuite for BasicManagedDataSource + */ +public class TestBasicManagedDataSource extends TestBasicDataSource { + public TestBasicManagedDataSource(String testName) { + super(testName); + } + + public static Test suite() { + return new TestSuite(TestBasicManagedDataSource.class); + } + + protected BasicDataSource createDataSource() throws Exception { + BasicManagedDataSource basicManagedDataSource = new BasicManagedDataSource(); + basicManagedDataSource.setTransactionManager(new TransactionManagerImpl()); + return basicManagedDataSource; + } + + public void testHashCode() throws Exception { + // TODO reenable... hashcode doesn't work when accessToUnderlyingConnectionAllowed is false + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]