Author: psteitz
Date: Sat Sep 9 13:20:00 2006
New Revision: 441852
URL: http://svn.apache.org/viewvc?view=rev&rev=441852
Log:
Changed implementation of equals in
PoolingDataSource.PoolGuardConnectionWrapper to ensure it is reflexive, even
when wrapped connections are not DelegatingConnections.
Also added tests for PoolingDataSource.
JIRA: DBCP-198
Reported by Kevin Ruland
Added:
jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolingDataSource.java
(with props)
Modified:
jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingDataSource.java
jakarta/commons/proper/dbcp/trunk/xdocs/changes.xml
Modified:
jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingDataSource.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingDataSource.java?view=diff&rev=441852&r1=441851&r2=441852
==============================================================================
---
jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingDataSource.java
(original)
+++
jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/PoolingDataSource.java
Sat Sep 9 13:20:00 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-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.
@@ -209,11 +209,23 @@
return delegate.createStatement(resultSetType,
resultSetConcurrency);
}
+
public boolean equals(Object obj) {
- if (delegate == null){
+ if (obj == null) {
return false;
}
- return delegate.equals(obj);
+ if (obj == this) {
+ return true;
+ }
+ if (delegate == null) {
+ return false;
+ }
+ if (obj instanceof PoolGuardConnectionWrapper) {
+ PoolGuardConnectionWrapper w = (PoolGuardConnectionWrapper)
obj;
+ return delegate.equals(w.delegate);
+ } else {
+ return delegate.equals(obj);
+ }
}
public boolean getAutoCommit() throws SQLException {
Added:
jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolingDataSource.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolingDataSource.java?view=auto&rev=441852
==============================================================================
---
jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolingDataSource.java
(added)
+++
jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolingDataSource.java
Sat Sep 9 13:20:00 2006
@@ -0,0 +1,157 @@
+/*
+ * 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.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.pool.ObjectPool;
+import org.apache.commons.pool.impl.GenericObjectPool;
+
+/**
+ * TestSuite for PoolingDataSource
+ *
+ * @version $Revision: 392677 $ $Date: 2006-04-08 21:42:24 -0700 (Sat, 08 Apr
2006) $
+ */
+public class TestPoolingDataSource extends TestConnectionPool {
+ public TestPoolingDataSource(String testName) {
+ super(testName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(TestPoolingDataSource.class);
+ }
+
+ protected Connection getConnection() throws Exception {
+ return ds.getConnection();
+ }
+
+ protected PoolingDataSource ds = null;
+ private GenericObjectPool pool = null;
+
+ public void setUp() throws Exception {
+ super.setUp();
+ pool = new GenericObjectPool();
+ pool.setMaxActive(getMaxActive());
+ pool.setMaxWait(getMaxWait());
+ Properties props = new Properties();
+ props.setProperty("user", "username");
+ props.setProperty("password", "password");
+ PoolableConnectionFactory factory =
+ new PoolableConnectionFactory(
+ new DriverConnectionFactory(new TesterDriver(),
+ "jdbc:apache:commons:testdriver", props),
+ pool, null, "SELECT DUMMY FROM DUAL", true, true);
+ pool.setFactory(factory);
+ ds = new PoolingDataSource(pool);
+ ds.setAccessToUnderlyingConnectionAllowed(true);
+ }
+
+ public void tearDown() throws Exception {
+ pool.close();
+ super.tearDown();
+ }
+
+ public void testPoolGuardConnectionWrapperEqualsSameDelegate() throws
Exception {
+ // Get a maximal set of connections from the pool
+ Connection[] c = new Connection[getMaxActive()];
+ for (int i = 0; i < c.length; i++) {
+ c[i] = newConnection();
+ }
+ // Close the delegate of one wrapper in the pool
+ ((DelegatingConnection) c[0]).getDelegate().close();
+
+ // Grab a new connection - should get c[0]'s closed connection
+ // so should be delegate-equivalent, so equal
+ Connection con = newConnection();
+ assertTrue(c[0].equals(con));
+ assertTrue(con.equals(c[0]));
+ for (int i = 0; i < c.length; i++) {
+ c[i].close();
+ }
+ }
+
+ private void checkPoolGuardConnectionWrapperEqualsReflexive() throws
Exception {
+ Connection con = ds.getConnection();
+ Connection con2 = con;
+ assertTrue(con2.equals(con));
+ assertTrue(con.equals(con2));
+ con.close();
+ }
+
+ /*
+ * JIRA: DBCP-198
+ */
+ public void testPoolGuardConnectionWrapperEqualsReflexive()
+ throws Exception {
+ // Statndard setup - using DelegatingConnections
+ // returned from PoolableConnectionFactory
+ checkPoolGuardConnectionWrapperEqualsReflexive();
+ // Force PoolGuardConnectionWrappers to wrap non-Delegating connections
+ pool.close();
+ pool = new GenericObjectPool();
+ pool.setMaxActive(getMaxActive());
+ pool.setMaxWait(getMaxWait());
+ Properties props = new Properties();
+ props.setProperty("user", "username");
+ props.setProperty("password", "password");
+ NonDelegatingPoolableConnectionFactory factory =
+ new NonDelegatingPoolableConnectionFactory(
+ new DriverConnectionFactory(new TesterDriver(),
+ "jdbc:apache:commons:testdriver", props), pool);
+ pool.setFactory(factory);
+ ds = new PoolingDataSource(pool);
+ checkPoolGuardConnectionWrapperEqualsReflexive();
+ }
+
+ public void testPoolGuardConnectionWrapperEqualsFail() throws Exception {
+ Connection con1 = ds.getConnection();
+ Connection con2 = ds.getConnection();
+ assertFalse(con1.equals(con2));
+ con1.close();
+ con2.close();
+ }
+
+ public void testPoolGuardConnectionWrapperEqualsNull() throws Exception {
+ Connection con1 = ds.getConnection();
+ Connection con2 = null;
+ assertFalse(con1.equals(con2));
+ con1.close();
+ }
+
+ public void testPoolGuardConnectionWrapperEqualsType() throws Exception {
+ Connection con1 = ds.getConnection();
+ Integer con2 = new Integer(0);
+ assertFalse(con1.equals(con2));
+ con1.close();
+ }
+
+ /** Factory to return non-delegating connections for DBCP-198 test */
+ private class NonDelegatingPoolableConnectionFactory extends
+ PoolableConnectionFactory {
+ public NonDelegatingPoolableConnectionFactory(ConnectionFactory
connFactory, ObjectPool pool) {
+ super(connFactory, pool, null, null, true, true);
+ }
+
+ synchronized public Object makeObject() throws Exception {
+ return _connFactory.createConnection();
+ }
+ }
+}
Propchange:
jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolingDataSource.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestPoolingDataSource.java
------------------------------------------------------------------------------
svn:executable = *
Modified: jakarta/commons/proper/dbcp/trunk/xdocs/changes.xml
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/dbcp/trunk/xdocs/changes.xml?view=diff&rev=441852&r1=441851&r2=441852
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/dbcp/trunk/xdocs/changes.xml Sat Sep 9 13:20:00 2006
@@ -134,6 +134,12 @@
Made userKeys an instance variable (i.e., not static)
in SharedPoolDataSource.
</action>
+ <action dev="psteitz" type="fix" issue="DBCP-198">
+ Changed implementation of equals in
+ PoolingDataSource.PoolGuardConnectionWrapper
+ to ensure it is reflexive, even when wrapped connections are not
+ DelegatingConnections.
+ </action>
</release>
<release version="1.2.1" date="2004-06-12" description="Maintenance
Release to restore JDK 1.3 compatibility">
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]