postgresql-jdbc.git / summary / log / commit / refs

commit    4fc1b4aafd67fa613f72069eba0135946d7a88ed
Author:   Kris Jurka <books@ejurka.com>
Date:     Wed Jan 07 05:28:59 2009 +0000

    The Statement and Connection proxies used for connection pooling
    code relied on the underlying real connection and statement code
    for equals and hashcode support.  When the proxies are closed we
    discard the references to the real objects, so we can't rely on
    them for this support because we'll get a NullPointerException.
    
    As reported by Radu Buzila.


.../ds/jdbc23/AbstractJdbc23PooledConnection.java | 38 +++++--------------- .../test/jdbc2/optional/PoolingDataSourceTest.java | 42 ++++++++++++++++++++-- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/org/postgresql/ds/jdbc23/AbstractJdbc23PooledConnection.java b/org/postgresql/ds/jdbc23/AbstractJdbc23PooledConnection.java index b22c97fa..b7137bdd 100644 --- a/org/postgresql/ds/jdbc23/AbstractJdbc23PooledConnection.java +++ b/org/postgresql/ds/jdbc23/AbstractJdbc23PooledConnection.java @@ -3,7 +3,7 @@ * Copyright (c) 2004-2008, PostgreSQL Global Development Group * * IDENTIFICATION -* $PostgreSQL: pgjdbc/org/postgresql/ds/jdbc23/AbstractJdbc23PooledConnection.java,v 1.3 2007/11/14 22:03:36 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/ds/jdbc23/AbstractJdbc23PooledConnection.java,v 1.4 2008/01/08 06:56:27 jurka Exp $ * *------------------------------------------------------------------------- */ @@ -118,7 +118,7 @@ public abstract class AbstractJdbc23PooledConnection } // If any error occures while opening a new connection, the listeners // have to be notified. This gives a chance to connection pools to - // elliminate bad pooled connections. + // eliminate bad pooled connections. try { // Only one connection can be open at a time from this PooledConnection. See JDBC 2.0 Optional Package spec section 6.2.3 @@ -270,24 +270,13 @@ public abstract class AbstractJdbc23PooledConnection { return "Pooled connection wrapping physical connection " + con; } - if (method.getName().equals("hashCode")) + if (method.getName().equals("equals")) { - return new Integer(con.hashCode()); + return proxy == args[0]; } - if (method.getName().equals("equals")) + if (method.getName().equals("hashCode")) { - if (args[0] == null) - { - return Boolean.FALSE; - } - try - { - return Proxy.isProxyClass(args[0].getClass()) && ((ConnectionHandler) Proxy.getInvocationHandler(args[0])).con == con ? Boolean.TRUE : Boolean.FALSE; - } - catch (ClassCastException e) - { - return Boolean.FALSE; - } + return System.identityHashCode(proxy); } try { @@ -424,22 +413,11 @@ public abstract class AbstractJdbc23PooledConnection } if (method.getName().equals("hashCode")) { - return new Integer(st.hashCode()); + return System.identityHashCode(proxy); } if (method.getName().equals("equals")) { - if (args[0] == null) - { - return Boolean.FALSE; - } - try - { - return Proxy.isProxyClass(args[0].getClass()) && ((StatementHandler) Proxy.getInvocationHandler(args[0])).st == st ? Boolean.TRUE : Boolean.FALSE; - } - catch (ClassCastException e) - { - return Boolean.FALSE; - } + return proxy == args[0]; } return method.invoke(st, args); } diff --git a/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java b/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java index bb6f8eab..d4012c3c 100644 --- a/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java +++ b/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java @@ -3,14 +3,13 @@ * Copyright (c) 2004-2008, PostgreSQL Global Development Group * * IDENTIFICATION -* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java,v 1.8 2007/10/07 23:32:46 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java,v 1.9 2008/01/08 06:56:31 jurka Exp $ * *------------------------------------------------------------------------- */ package org.postgresql.test.jdbc2.optional; -import java.sql.SQLException; -import java.sql.Statement; +import java.sql.*; import org.postgresql.test.TestUtil; import org.postgresql.jdbc2.optional.PoolingDataSource; import org.postgresql.ds.common.BaseDataSource; @@ -127,4 +126,41 @@ public class PoolingDataSourceTest extends BaseDataSourceTest stmt.close(); con.close(); } + + public void testConnectionObjectMethods() throws SQLException + { + con = getDataSourceConnection(); + + Connection conRef = con; + assertEquals(con, conRef); + + int hc1 = con.hashCode(); + con.close(); + int hc2 = con.hashCode(); + + assertEquals(con, conRef); + assertEquals(hc1, hc2); + } + + public void testStatementObjectMethods() throws SQLException + { + con = getDataSourceConnection(); + + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT 1"); + Statement stmtRef = stmt; + + assertEquals(stmt, stmtRef); + // Currently we aren't proxying ResultSet, so this doesn't + // work, see Bug #1010542. + // assertEquals(stmt, rs.getStatement()); + + int hc1 = stmt.hashCode(); + stmt.close(); + int hc2 = stmt.hashCode(); + + assertEquals(stmt, stmtRef); + assertEquals(hc1, hc2); + } + } [parent: 70ac25f5804e]