You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by fh...@apache.org on 2012/08/07 02:15:51 UTC

svn commit: r1370075 - in /tomcat/tc7.0.x/trunk/modules/jdbc-pool/src: main/java/org/apache/tomcat/jdbc/pool/ test/java/org/apache/tomcat/jdbc/test/

Author: fhanik
Date: Tue Aug  7 00:15:50 2012
New Revision: 1370075

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

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

Modified: tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DisposableConnectionFacade.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DisposableConnectionFacade.java?rev=1370075&r1=1370074&r2=1370075&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DisposableConnectionFacade.java (original)
+++ tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DisposableConnectionFacade.java Tue Aug  7 00:15: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/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java?rev=1370075&r1=1370074&r2=1370075&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java (original)
+++ tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java Tue Aug  7 00:15: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/tc7.0.x/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/EqualsHashCodeTest.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/EqualsHashCodeTest.java?rev=1370075&view=auto
==============================================================================
--- tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/EqualsHashCodeTest.java (added)
+++ tomcat/tc7.0.x/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/EqualsHashCodeTest.java Tue Aug  7 00:15: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/tc7.0.x/trunk/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/test/EqualsHashCodeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



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