Author: psteitz Date: Sun Feb 3 19:58:08 2008 New Revision: 618163 URL: http://svn.apache.org/viewvc?rev=618163&view=rev Log: Added exception handler to ensure that PooledConnections are not orphaned when an exception occurs in setUpDefaults or clearWarnings in IntanceKeyDataSource getConnection.
JIRA: DBCP-237 Reported and patched by Oliver Matz Added: commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestInstanceKeyDataSource.java Modified: commons/proper/dbcp/trunk/pom.xml commons/proper/dbcp/trunk/project.xml commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.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=618163&r1=618162&r2=618163&view=diff ============================================================================== --- commons/proper/dbcp/trunk/pom.xml (original) +++ commons/proper/dbcp/trunk/pom.xml Sun Feb 3 19:58:08 2008 @@ -255,6 +255,7 @@ <include>org/apache/commons/dbcp/datasources/TestKeyedCPDSConnectionFactory.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/datasources/TestInstanceKeyDataSource.java</include> <include>org/apache/commons/dbcp/managed/TestBasicManagedDataSource.java</include> <include>org/apache/commons/dbcp/managed/TestManagedDataSource.java</include> Modified: commons/proper/dbcp/trunk/project.xml URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/project.xml?rev=618163&r1=618162&r2=618163&view=diff ============================================================================== --- commons/proper/dbcp/trunk/project.xml (original) +++ commons/proper/dbcp/trunk/project.xml Sun Feb 3 19:58:08 2008 @@ -366,7 +366,8 @@ <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/datasources/TestInstanceKeyDataSource.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> Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.java URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.java?rev=618163&r1=618162&r2=618163&view=diff ============================================================================== --- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.java (original) +++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.java Sun Feb 3 19:58:08 2008 @@ -688,10 +688,20 @@ + " to create the PooledConnection."); } - Connection con = info.getPooledConnection().getConnection(); - setupDefaults(con, username); - con.clearWarnings(); - return con; + Connection con = info.getPooledConnection().getConnection(); + try { + setupDefaults(con, username); + con.clearWarnings(); + return con; + } catch (SQLException ex) { + try { + con.close(); + } catch (Exception exc) { + getLogWriter().println( + "ignoring exception during close: " + exc); + } + throw ex; + } } protected abstract PooledConnectionAndInfo Added: commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestInstanceKeyDataSource.java URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestInstanceKeyDataSource.java?rev=618163&view=auto ============================================================================== --- commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestInstanceKeyDataSource.java (added) +++ commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/datasources/TestInstanceKeyDataSource.java Sun Feb 3 19:58:08 2008 @@ -0,0 +1,81 @@ +/* + * 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.datasources; + +import java.sql.Connection; +import java.sql.SQLException; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS; + +/** + * @version $Revision$ $Date$ + */ +public class TestInstanceKeyDataSource extends TestCase { + public TestInstanceKeyDataSource(String testName) { + super(testName); + } + + public static Test suite() { + return new TestSuite(TestInstanceKeyDataSource.class); + } + + + public void setUp() throws Exception { + } + + /** + * Verify that exception on setupDefaults does not leak PooledConnection + * + * JIRA: DBCP-237 + */ + public void testExceptionOnSetupDefaults() throws Exception { + DriverAdapterCPDS pcds; + pcds = new DriverAdapterCPDS(); + pcds.setDriver("org.apache.commons.dbcp.TesterDriver"); + pcds.setUrl("jdbc:apache:commons:testdriver"); + pcds.setUser("foo"); + pcds.setPassword("bar"); + pcds.setPoolPreparedStatements(false); + ThrowOnSetupDefaultsDataSource tds = new ThrowOnSetupDefaultsDataSource(); + tds.setConnectionPoolDataSource(pcds); + int numConnections = tds.getNumActive(); + try { + Connection conn = tds.getConnection("foo", "bar"); + fail("Expecting SQLException"); + } catch (SQLException ex) { + //Expected + } + assertEquals(numConnections,tds.getNumActive()); + } + + private static class ThrowOnSetupDefaultsDataSource + extends SharedPoolDataSource { + ThrowOnSetupDefaultsDataSource() { + super(); + } + protected void setupDefaults(Connection con, String username) + throws SQLException { + throw new SQLException("bang!"); + } + } + +} Modified: commons/proper/dbcp/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/xdocs/changes.xml?rev=618163&r1=618162&r2=618163&view=diff ============================================================================== --- commons/proper/dbcp/trunk/xdocs/changes.xml (original) +++ commons/proper/dbcp/trunk/xdocs/changes.xml Sun Feb 3 19:58:08 2008 @@ -104,7 +104,12 @@ <action dev="psteitz" type="fix" issue="DBCP-245" due-to="Michael Drechsel"> Fixed error in SharedPoolDataSource causing incorrect passwords to be stored under certain conditions. - </action> + </action> + <action dev="psteitz" type="fix" issue="DBCP-237" due-to="Oliver Matz"> + Added exception handler to ensure that PooledConnections are not + orphaned when an exception occurs in setUpDefaults or clearWarnings in + IntanceKeyDataSource.getConnection. + </action> </release> <release version="1.2.2" date="2007-04-04" description="This is a maintenance release containing bug fixes