X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id DC34452956 for ; Mon, 27 Jun 2005 09:06:25 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 91860-02 for ; Mon, 27 Jun 2005 12:06:19 +0000 (GMT) Received: from mail.ecircle.de (mail.ecircle.de [195.140.186.200]) by svr1.postgresql.org (Postfix) with ESMTP id ACAF452943 for ; Mon, 27 Jun 2005 09:06:17 -0300 (ADT) Received: from [192.168.1.110] (unknown [192.168.1.110]) by mail.ecircle.de (READY) with ESMTP id 0237F11C02E; Mon, 27 Jun 2005 14:06:19 +0200 (CEST) Subject: [PATCH]Re: Which version of driver to use ? From: Csaba Nagy To: Dave Cramer Cc: Postgres JDBC In-Reply-To: <1119630533.3994.118.camel@coppola.muc.ecircle.de> References: <1119627180.3994.116.camel@coppola.muc.ecircle.de> <99EC30A6-C278-4CB2-9323-A9BFB5D21657@fastcrypt.com> <1119630533.3994.118.camel@coppola.muc.ecircle.de> Content-Type: multipart/mixed; boundary="=-c2pT7QDFdZPc2Ay6+HlU" Message-Id: <1119873978.3994.126.camel@coppola.muc.ecircle.de> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Mon, 27 Jun 2005 14:06:18 +0200 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.017 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/136 X-Sequence-Number: 13098 --=-c2pT7QDFdZPc2Ay6+HlU Content-Type: text/plain Content-Transfer-Encoding: 7bit If it is of interest to anyone, I have back-patched the PooledConnectionImpl in the JDBC driver coming with the 7.4 postgres server to properly handle connection problems. The code is taken from the latest 8.1 dev source with the necessary adaptation. Diff file is attached, was taken against CVS branch REL7_4_STABLE of the postgres server source. Cheers, Csaba. On Fri, 2005-06-24 at 18:28, Csaba Nagy wrote: > Thanks Dave, > > The connection URL version will be good I guess, I'll try tomorrow. > > Cheers, > Csaba. > > On Fri, 2005-06-24 at 18:25, Dave Cramer wrote: > > Csaba, > > > > Yeah, you can force v2 protocol on the 8.0 driver > > > > just add ?protocolVersion=2 to the connection url > > > > see > > http://jdbc.postgresql.org/documentation/80/connect.html#connection- > > parameters > > > > for more details > > > > re the 74 code : it's in the main server tree for version 7.4 tag is > > REL7_4_STABLE > > > > Dave > > > > On 24-Jun-05, at 11:33 AM, Csaba Nagy wrote: > > > > > Hi all, > > > > > > We have a postgres 7.4 installation with production code running on > > > it. > > > The code is written in Java and uses the JDBC driver to access it. > > > > > > I recently discovered that the JDBC driver we were using is not > > > properly > > > detecting connection problems in the pooled connection > > > implementation to > > > report them to our connection pool. However, when giving a go to fix > > > this, I also discovered that the latest 8.0 driver fixes this problem > > > (I've checked the code). > > > > > > So I've tried to upgrade to the 8.0 driver, but failed: our code is > > > pretty sloppy with the parameter types of prepared statements, and the > > > new driver is very strict about them. Eventually we will fix this, but > > > for the deployed code it's not an option. > > > > > > Question: is there a way to make the 8.0 driver less strict ? E.g. we > > > have quite a few places where numeric fields are set with "setString", > > > and other things like this. > > > > > > Alternatively, have anybody back-patched the connection error > > > notification fix to the 7.4 driver line ? If not, I could do that > > > myself, in this case what's the recommended way of getting the latest > > > 7.4 sources ? Is there actually a CVS branch for it for bug-fixes ? > > > > > > Thanks, > > > Csaba. > > > > > > > > > > > > ---------------------------(end of > > > broadcast)--------------------------- > > > TIP 4: Don't 'kill -9' the postmaster > > > > > > > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly --=-c2pT7QDFdZPc2Ay6+HlU Content-Disposition: attachment; filename=PooledConnectionImpl.java.diff Content-Type: text/x-patch; name=PooledConnectionImpl.java.diff; charset=UTF-8 Content-Transfer-Encoding: 7bit --- /home/cnagy/dev/src/pgsql-7_4_STABLE/src/interfaces/jdbc/org/postgresql/jdbc2/optional/PooledConnectionImpl.java 2004-02-24 14:11:44.000000000 +0100 +++ src/share/org/postgresql/jdbc2/optional/PooledConnectionImpl.java 2005-06-27 13:34:33.000000000 +0200 @@ -5,6 +5,8 @@ import java.util.*; import java.lang.reflect.*; import org.postgresql.PGConnection; +import org.postgresql.util.PSQLState; +import org.postgresql.util.PSQLException; /** * PostgreSQL implementation of the PooledConnection interface. This shouldn't @@ -14,7 +16,7 @@ * * @author Aaron Mulder (ammulder@chariotsolutions.com) * @author Csaba Nagy (ncsaba@yahoo.com) - * @version $Revision: 1.7.4.3 $ + * @version $Revision: 1.1.2.2 $ */ public class PooledConnectionImpl implements PooledConnection { @@ -97,7 +99,8 @@ if (con == null) { // Before throwing the exception, let's notify the registered listeners about the error - final SQLException sqlException = new SQLException("This PooledConnection has already been closed!"); + PSQLException sqlException = new PSQLException("This PooledConnection has already been closed.", + PSQLState.CONNECTION_DOES_NOT_EXIST); fireConnectionFatalError(sqlException); throw sqlException; } @@ -173,6 +176,50 @@ } } + // Classes we consider fatal. + private static String[] fatalClasses = { + "08", // connection error + "53", // insufficient resources + + // nb: not just "57" as that includes query cancel which is nonfatal + "57P01", // admin shutdown + "57P02", // crash shutdown + "57P03", // cannot connect now + + "58", // system error (backend) + "60", // system error (driver) + "99", // unexpected error + "F0", // configuration file error (backend) + "XX", // internal error (backend) + }; + + private static boolean isFatalState(String state) { + if (state == null) // no info, assume fatal + return true; + if (state.length() < 2) // no class info, assume fatal + return true; + + for (int i = 0; i < fatalClasses.length; ++i) + if (state.startsWith(fatalClasses[i])) + return true; // fatal + + return false; + } + + /** + * Fires a connection error event, but only if we + * think the exception is fatal. + * + * @param e the SQLException to consider + */ + private void fireConnectionError(SQLException e) + { + if (!isFatalState(e.getSQLState())) + return; + + fireConnectionFatalError(e); + } + /** * Instead of declaring a class implementing Connection, which would have * to be updated for every JDK rev, use a dynamic proxy to handle all @@ -236,7 +283,8 @@ } if (con == null && !method.getName().equals("close")) { - throw new SQLException(automatic ? "Connection has been closed automatically because a new connection was opened for the same PooledConnection or the PooledConnection has been closed" : "Connection has been closed"); + throw new PSQLException(automatic ? "Connection has been closed automatically because a new connection was opened for the same PooledConnection or the PooledConnection has been closed." : "Connection has been closed.", + PSQLState.CONNECTION_DOES_NOT_EXIST); } if (method.getName().equals("close")) { @@ -268,17 +316,22 @@ } return null; } - else if(method.getName().equals("createStatement")) + + // From here on in, we invoke via reflection, catch exceptions, + // and check if they're fatal before rethrowing. + + try { + if (method.getName().equals("createStatement")) { Statement st = (Statement)method.invoke(con, args); return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{Statement.class, org.postgresql.PGStatement.class}, new StatementHandler(this, st)); } - else if(method.getName().equals("prepareCall")) + else if (method.getName().equals("prepareCall")) { Statement st = (Statement)method.invoke(con, args); return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{CallableStatement.class, org.postgresql.PGStatement.class}, new StatementHandler(this, st)); } - else if(method.getName().equals("prepareStatement")) + else if (method.getName().equals("prepareStatement")) { Statement st = (Statement)method.invoke(con, args); return Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{PreparedStatement.class, org.postgresql.PGStatement.class}, new StatementHandler(this, st)); @@ -287,6 +340,12 @@ { return method.invoke(con, args); } + } catch (InvocationTargetException e) { + Throwable te = e.getTargetException(); + if (te instanceof SQLException) + fireConnectionError((SQLException)te); // Tell listeners about exception if it's fatal + throw te; + } } Connection getProxy() { @@ -323,11 +382,11 @@ * The StatementHandler is required in order to return the proper * Connection proxy for the getConnection method. */ - private static class StatementHandler implements InvocationHandler { - private ConnectionHandler con; + private class StatementHandler implements InvocationHandler { + private PooledConnectionImpl.ConnectionHandler con; private Statement st; - public StatementHandler(ConnectionHandler con, Statement st) { + public StatementHandler(PooledConnectionImpl.ConnectionHandler con, Statement st) { this.con = con; this.st = st; } @@ -379,22 +438,23 @@ } if (st == null || con.isClosed()) { - throw new SQLException("Statement has been closed"); + throw new PSQLException("Statement has been closed.", + PSQLState.UNKNOWN_STATE); } - else if (method.getName().equals("getConnection")) + + if (method.getName().equals("getConnection")) { return con.getProxy(); // the proxied connection, not a physical connection } - else - { + try { return method.invoke(st, args); - } - catch (InvocationTargetException e) - { - throw e.getTargetException(); - } + } catch (InvocationTargetException e) { + Throwable te = e.getTargetException(); + if (te instanceof SQLException) + fireConnectionError((SQLException)te); // Tell listeners about exception if it's fatal + throw te; } } } --=-c2pT7QDFdZPc2Ay6+HlU--