Author: markt
Date: Thu Feb 19 15:46:03 2009
New Revision: 745904

URL: http://svn.apache.org/viewvc?rev=745904&view=rev
Log:
Fix DBCP-235. Separate internal implementation and externally interfacing 
methods to allow equals and hashcode to work. Includes supporting test case.

Modified:
    
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/DelegatingConnection.java
    
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/ManagedConnection.java
    
commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/managed/TestManagedDataSource.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=745904&r1=745903&r2=745904&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
 Thu Feb 19 15:46:03 2009
@@ -107,7 +107,7 @@
     public String toString() {
         String s = null;
         
-        Connection c = this.getInnermostDelegate();
+        Connection c = this.getInnermostDelegateInternal();
         if (c != null) {
             try {
                 if (c.isClosed()) {
@@ -143,6 +143,13 @@
      * @return my underlying {...@link Connection}.
      */
     public Connection getDelegate() {
+        return getDelegateInternal();
+    }
+    
+    /**
+     * Should be final but can't be for compatability with previous releases.
+     */
+    protected Connection getDelegateInternal() {
         return _conn;
     }
     
@@ -154,7 +161,7 @@
      * @since 1.2.2
      */
     public boolean innermostDelegateEquals(Connection c) {
-        Connection innerCon = getInnermostDelegate();
+        Connection innerCon = getInnermostDelegateInternal();
         if (innerCon == null) {
             return c == null;
         } else {
@@ -169,7 +176,7 @@
         if (obj == this) {
             return true;
         }
-        Connection delegate = getInnermostDelegate();
+        Connection delegate = getInnermostDelegateInternal();
         if (delegate == null) {
             return false;
         }
@@ -183,7 +190,7 @@
     }
 
     public int hashCode() {
-        Object obj = getInnermostDelegate();
+        Object obj = getInnermostDelegateInternal();
         if (obj == null) {
             return 0;
         }
@@ -207,16 +214,20 @@
      * sure to obtain a "genuine" {...@link Connection}.
      */
     public Connection getInnermostDelegate() {
+        return getInnermostDelegateInternal();
+    }
+
+    protected final Connection getInnermostDelegateInternal() {
         Connection c = _conn;
         while(c != null && c instanceof DelegatingConnection) {
-            c = ((DelegatingConnection)c).getDelegate();
+            c = ((DelegatingConnection)c).getDelegateInternal();
             if(this == c) {
                 return null;
             }
         }
         return c;
     }
-
+    
     /** Sets my delegate. */
     public void setDelegate(Connection c) {
         _conn = c;

Modified: 
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/ManagedConnection.java
URL: 
http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/ManagedConnection.java?rev=745904&r1=745903&r2=745904&view=diff
==============================================================================
--- 
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/ManagedConnection.java
 (original)
+++ 
commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/ManagedConnection.java
 Thu Feb 19 15:46:03 2009
@@ -251,17 +251,9 @@
         }
     }
 
-    /**
-     * Gets the actual delegate without checking the 
isAccessToUnderlyingConnectionAllowed() flag.  This method is for internal use 
only.
-     * @return the delegate of this connection
-     */
-    protected Connection getDelegateInternal() {
-        return super.getDelegate();
-    }
-
     public Connection getInnermostDelegate() {
         if (isAccessToUnderlyingConnectionAllowed()) {
-            return super.getInnermostDelegate();
+            return super.getInnermostDelegateInternal();
         } else {
             return null;
         }

Modified: 
commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/managed/TestManagedDataSource.java
URL: 
http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/managed/TestManagedDataSource.java?rev=745904&r1=745903&r2=745904&view=diff
==============================================================================
--- 
commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/managed/TestManagedDataSource.java
 (original)
+++ 
commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/managed/TestManagedDataSource.java
 Thu Feb 19 15:46:03 2009
@@ -126,6 +126,28 @@
         connectionB.close();
     }
 
+    public void testManagedConnectionEqualsSameDelegateNoUnderlyingAccess() 
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();
+
+        // Disable access for the new connection
+        ds.setAccessToUnderlyingConnectionAllowed(false);
+        // 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();
+        }
+        ds.setAccessToUnderlyingConnectionAllowed(true);
+    }
+
     public void testManagedConnectionEqualsSameDelegate() throws Exception {
         // Get a maximal set of connections from the pool
         Connection[] c = new Connection[getMaxActive()];
@@ -145,6 +167,7 @@
         }
     }
 
+
     /*
     * JIRA: DBCP-198
     */


Reply via email to