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 99E1552889 for ; Wed, 10 Aug 2005 13:41:34 -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 40227-10 for ; Wed, 10 Aug 2005 16:41:32 +0000 (GMT) Received: from mail2.yozons.com (mail2.yozons.com [206.80.109.180]) by svr1.postgresql.org (Postfix) with ESMTP id 9A55C52806 for ; Wed, 10 Aug 2005 13:41:29 -0300 (ADT) Received: from [192.168.1.51] (soho2.yozons.com [206.80.109.178] (may be forged)) (authenticated bits=0) by mail2.yozons.com (8.12.8/8.12.8) with ESMTP id j7AGetVq002240 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Wed, 10 Aug 2005 09:41:18 -0700 Message-ID: <42FA2E1D.1060109@computer.org> Date: Wed, 10 Aug 2005 09:41:01 -0700 From: David Wall User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: PG 8.0.3 - PreparedStatement Can't Use Query Methods exception Content-Type: multipart/alternative; boundary="------------050404050407020709060502" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.47 tagged_above=0 required=5 tests=DNS_FROM_RFC_ABUSE, HTML_50_60, HTML_MESSAGE X-Spam-Level: X-Archive-Number: 200508/70 X-Sequence-Number: 13639 This is a multi-part message in MIME format. --------------050404050407020709060502 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit We are converting from a 7.3 PG database to 8.0.3 and mostly all is working okay. We are using postgresql-8.1dev-400.jdbc3.jar for JDBC. Previously, we never received this sort of error. The idea is that certain SQLExceptions are passed to a routine in our connection pool that attempts to see if the Connection object is still valid or not by doing a simple SELECT 1 command. This command still works fine via psql. Here's our exception: SQLState: 22023 ErrorCode: 0 SQLException: ConnectionPool.closeIfBadConnection(bpn) dummy SQL (SELECT 1) detected problematic Connection; closing it: Message: Can''t use query methods that take a query string on a PreparedStatement. org.postgresql.util.PSQLException: Can''t use query methods that take a query string on a PreparedStatement. at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:218) at com.xx.db.ConnectionPool.closeIfBadConnection(ConnectionPool.java:431) at com.xx.db.ConnectionPool.rollbackIgnoreException(ConnectionPool.java:379) at com.xx.bpn.BpnBackgrounder.doDailyCleanup(BpnBackgrounder.java:104) at com.xx.bpn.BpnBackgrounder.doBackgroundTasks(BpnBackgrounder.java:123) at com.xx.bpn.BpnBackgrounder.run(BpnBackgrounder.java:142) at java.lang.Thread.run(Thread.java:595) The Java code that actually issues this is as follows: public final void closeIfBadConnection(Connection con, SQLException e) { if ( con != null ) { // First, let's try to the SQLState check since it's straightforward. if ( e != null ) { if ( e.getSQLState() != null && e.getSQLState().startsWith("08") ) // 08 class is for connection exceptions { if ( app.isDebugEnabled() ) app.sqlerr(e,"ConnectionPool.closeIfBadConnection(" + name + ") SQLState (" + e.getSQLState() + ") detected problematic Connection; closing it"); else app.err("ConnectionPool.closeIfBadConnection(" + name + ") SQLState (" + e.getSQLState() + ") detected problematic Connection; closing it - " + e.getMessage()); try { con.close(); // it's no good, and our pool will reopen it when it's requested again later } catch( SQLException e2 ) {} return; // we already handled matters here } } // Well, we're not sure, so let's do a dummy query and see if anything bad happens. PreparedStatement stmt = null; String dummyQuery = ( isOracle() ) ? "SELECT 1 FROM dummy_table" : "SELECT 1"; try { stmt = con.prepareStatement(dummyQuery); stmt.executeQuery(dummyQuery); } catch(SQLException e2) { if ( app.isDebugEnabled() ) app.sqlerr(e2,"ConnectionPool.closeIfBadConnection(" + name + ") dummy SQL (" + dummyQuery + ") detected problematic Connection; closing it"); else app.err("ConnectionPool.closeIfBadConnection(" + name + ") dummy SQL (" + dummyQuery + ") detected problematic Connection; closing it - " + e2.getMessage()); try { con.close(); // it's no good, and our pool will reopen it when it's requested again later } catch( SQLException e3 ) {} } finally { if ( stmt != null ) try { stmt.close(); } catch( Exception e4 ) {} } } } What am I doing wrong here? It seems so straightforward. We create a preparedStatement with a simple SELECT and then execute the query and ignore the result set that may come back. Thanks, David --------------050404050407020709060502 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit We are converting from a 7.3 PG database to 8.0.3 and mostly all is working okay.  We are using postgresql-8.1dev-400.jdbc3.jar for JDBC.

Previously, we never received this sort of error.  The idea is that certain SQLExceptions are passed to a routine in our connection pool that attempts to see if the Connection object is still valid or not by doing a simple SELECT 1 command.  This command still works fine via psql.

Here's our exception:

SQLState:  22023
ErrorCode: 0
SQLException: ConnectionPool.closeIfBadConnection(bpn) dummy SQL (SELECT 1) detected problematic Connection; closing it:
Message:   Can''t use query methods that take a query string on a PreparedStatement.
org.postgresql.util.PSQLException: Can''t use query methods that take a query string on a PreparedStatement.
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:218)
        at com.xx.db.ConnectionPool.closeIfBadConnection(ConnectionPool.java:431)
        at com.xx.db.ConnectionPool.rollbackIgnoreException(ConnectionPool.java:379)
        at com.xx.bpn.BpnBackgrounder.doDailyCleanup(BpnBackgrounder.java:104)
        at com.xx.bpn.BpnBackgrounder.doBackgroundTasks(BpnBackgrounder.java:123)
        at com.xx.bpn.BpnBackgrounder.run(BpnBackgrounder.java:142)
        at java.lang.Thread.run(Thread.java:595)


The Java code that actually issues this is as follows:

public final void closeIfBadConnection(Connection con, SQLException e)
{
    if ( con != null )
    {
        // First, let's try to the SQLState check since it's straightforward.
        if ( e != null )
        {
            if ( e.getSQLState() != null && e.getSQLState().startsWith("08") ) // 08 class is for connection exceptions
            {
                if ( app.isDebugEnabled() )
                    app.sqlerr(e,"ConnectionPool.closeIfBadConnection(" + name + ") SQLState (" + e.getSQLState() + ") detected problematic Connection; closing it");
                else
                    app.err("ConnectionPool.closeIfBadConnection(" + name + ") SQLState (" + e.getSQLState() + ") detected problematic Connection; closing it - " + e.getMessage());
                   
                try
                {
                    con.close();  // it's no good, and our pool will reopen it when it's requested again later
                }
                catch( SQLException e2 ) {}
               
                return; // we already handled matters here
            }
        }
           
        // Well, we're not sure, so let's do a dummy query and see if anything bad happens.
        PreparedStatement stmt = null;
        String dummyQuery = ( isOracle() ) ? "SELECT 1 FROM dummy_table" : "SELECT 1";
        try
        {
            stmt = con.prepareStatement(dummyQuery);
            stmt.executeQuery(dummyQuery);
        }
        catch(SQLException e2)
        {
            if ( app.isDebugEnabled() )
                app.sqlerr(e2,"ConnectionPool.closeIfBadConnection(" + name + ") dummy SQL (" + dummyQuery + ") detected problematic Connection; closing it");
            else
                app.err("ConnectionPool.closeIfBadConnection(" + name + ") dummy SQL (" + dummyQuery + ") detected problematic Connection; closing it - " + e2.getMessage());
            try
            {
                con.close();  // it's no good, and our pool will reopen it when it's requested again later
            }
            catch( SQLException e3 ) {}
        }
        finally
        {
            if ( stmt != null )
                try { stmt.close(); } catch( Exception e4 ) {}
        }
    }
}   

What am I doing wrong here?  It seems so straightforward.  We create a preparedStatement with a simple SELECT and then execute the query and ignore the result set that may come back.

Thanks,
David
--------------050404050407020709060502--