Author: fhanik
Date: Tue Aug  7 00:14:50 2012
New Revision: 1370074

URL: http://svn.apache.org/viewvc?rev=1370074&view=rev
Log:
Implement equals/hashCode so that they survive connection closure


Added:
    
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/EqualsHashCodeTest.java
   (with props)
Modified:
    
tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DisposableConnectionFacade.java
    
tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java

Modified: 
tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DisposableConnectionFacade.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DisposableConnectionFacade.java?rev=1370074&r1=1370073&r2=1370074&view=diff
==============================================================================
--- 
tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DisposableConnectionFacade.java
 (original)
+++ 
tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DisposableConnectionFacade.java
 Tue Aug  7 00:14:50 2012
@@ -17,6 +17,7 @@
 package org.apache.tomcat.jdbc.pool;
 
 import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 import java.sql.SQLException;
 
 /**
@@ -44,10 +45,26 @@ public class DisposableConnectionFacade 
     public void reset(ConnectionPool parent, PooledConnection con) {
     }
 
+
+
+    @Override
+    public int hashCode() {
+        return System.identityHashCode(this);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        return this==obj;
+    }
+
     @Override
     public Object invoke(Object proxy, Method method, Object[] args)
             throws Throwable {
-        if (getNext()==null) {
+        if (compare(EQUALS_VAL, method)) {
+            return this.equals(Proxy.getInvocationHandler(args[0]));
+        } else if (compare(HASHCODE_VAL, method)) {
+            return this.hashCode();
+        } else if (getNext()==null) {
             if (compare(ISCLOSED_VAL, method)) {
                 return Boolean.TRUE;
             }

Modified: 
tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java?rev=1370074&r1=1370073&r2=1370074&view=diff
==============================================================================
--- 
tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java
 (original)
+++ 
tomcat/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java
 Tue Aug  7 00:14:50 2012
@@ -67,6 +67,16 @@ public abstract class JdbcInterceptor im
     public static final String ISVALID_VAL = "isValid";
 
     /**
+     * {@link java.lang.Object#equals(Object)}
+     */
+    public static final String EQUALS_VAL = "equals";
+
+    /**
+     * {@link java.lang.Object#hashCode()}
+     */
+    public static final String HASHCODE_VAL = "hashCode";
+
+    /**
      * Properties for this interceptor.
      */
     protected Map<String,InterceptorProperty> properties = null;

Added: 
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/EqualsHashCodeTest.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/EqualsHashCodeTest.java?rev=1370074&view=auto
==============================================================================
--- 
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/EqualsHashCodeTest.java
 (added)
+++ 
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/EqualsHashCodeTest.java
 Tue Aug  7 00:14:50 2012
@@ -0,0 +1,77 @@
+/*
+ * 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.tomcat.jdbc.test;
+
+import java.lang.management.ManagementFactory;
+import java.sql.Connection;
+import java.util.Hashtable;
+
+import javax.sql.PooledConnection;
+
+import org.apache.tomcat.jdbc.pool.ConnectionPool;
+import org.apache.tomcat.jdbc.test.driver.Driver;
+
+public class EqualsHashCodeTest extends DefaultTestCase{
+    public static final String password = "password";
+    public static final String username = "username";
+
+    public EqualsHashCodeTest(String s) {
+        super(s);
+    }
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        this.datasource.setDriverClassName(Driver.class.getName());
+        this.datasource.setUrl("jdbc:tomcat:test");
+        this.datasource.setPassword(password);
+        this.datasource.setMaxActive(1);
+        this.datasource.setMinIdle(datasource.getMaxActive());
+        this.datasource.setMaxIdle(datasource.getMaxActive());
+        this.datasource.setUsername(username);
+        this.datasource.getConnection().close();
+        ConnectionPool pool = datasource.createPool();
+        org.apache.tomcat.jdbc.pool.jmx.ConnectionPool jmxPool = new 
org.apache.tomcat.jdbc.pool.jmx.ConnectionPool(pool);
+    }
+
+    public void testEquals() throws Exception {
+        Connection con1 = datasource.getConnection();
+        Connection real1 = ((PooledConnection)con1).getConnection();
+        assertEquals(con1, con1);
+        con1.close();
+        assertEquals(con1, con1);
+        Connection con2 = datasource.getConnection();
+        Connection real2 = ((PooledConnection)con2).getConnection();
+        assertEquals(real1,real2);
+        assertEquals(con2, con2);
+        assertNotSame(con1, con2);
+        con2.close();
+        assertEquals(con2, con2);
+    }
+
+    public void testHashCode() throws Exception {
+        Connection con1 = datasource.getConnection();
+        assertEquals(con1.hashCode(), con1.hashCode());
+        con1.close();
+        assertEquals(con1.hashCode(), con1.hashCode());
+        Connection con2 = datasource.getConnection();
+        assertEquals(con2.hashCode(), con2.hashCode());
+        assertNotSame(con1.hashCode(), con2.hashCode());
+        con2.close();
+        assertEquals(con2.hashCode(), con2.hashCode());
+    }
+}

Propchange: 
tomcat/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/EqualsHashCodeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to