pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
PG 8.0.3 - PreparedStatement Can't Use Query Methods exception
3+ messages / 2 participants
[nested] [flat]

* PG 8.0.3 - PreparedStatement Can't Use Query Methods exception
@ 2005-08-10 16:41 David Wall <d.wall@computer.org>
  2005-08-10 16:50 ` Re: PG 8.0.3 - PreparedStatement Can't Use Query Methods Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 3+ messages in thread

From: David Wall @ 2005-08-10 16:41 UTC (permalink / raw)
  To: pgsql-jdbc

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

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: PG 8.0.3 - PreparedStatement Can't Use Query Methods
  2005-08-10 16:41 PG 8.0.3 - PreparedStatement Can't Use Query Methods exception David Wall <d.wall@computer.org>
@ 2005-08-10 16:50 ` Kris Jurka <books@ejurka.com>
  2005-08-10 17:18   ` Re: PG 8.0.3 - PreparedStatement Can't Use Query Methods exception David Wall <d.wall@computer.org>
  0 siblings, 1 reply; 3+ messages in thread

From: Kris Jurka @ 2005-08-10 16:50 UTC (permalink / raw)
  To: David Wall <d.wall@computer.org>; +Cc: pgsql-jdbc



On Wed, 10 Aug 2005, David Wall wrote:

> 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.
> 
> 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 
> 
> 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.
> 

The JDBC 3 Spec 13.2.4 says, "If any of the PreparedStatement execute 
methods is called with an SQL string as a parameter, an SQLException is 
thrown."

Kris Jurka



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: PG 8.0.3 - PreparedStatement Can't Use Query Methods exception
  2005-08-10 16:41 PG 8.0.3 - PreparedStatement Can't Use Query Methods exception David Wall <d.wall@computer.org>
  2005-08-10 16:50 ` Re: PG 8.0.3 - PreparedStatement Can't Use Query Methods Kris Jurka <books@ejurka.com>
@ 2005-08-10 17:18   ` David Wall <d.wall@computer.org>
  0 siblings, 0 replies; 3+ messages in thread

From: David Wall @ 2005-08-10 17:18 UTC (permalink / raw)
  To: pgsql-jdbc

Thanks, Kris.  I didn't even SEE that I had submitted the query string 
in both places (in the prepareStatement as well as the executeQuery 
methods), which is clearly not what I had intended, though I got away 
with it before I suppose.

David

>The JDBC 3 Spec 13.2.4 says, "If any of the PreparedStatement execute 
>methods is called with an SQL string as a parameter, an SQLException is 
>thrown."
>
>Kris Jurka
>
>  
>



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2005-08-10 17:18 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-08-10 16:41 PG 8.0.3 - PreparedStatement Can't Use Query Methods exception David Wall <d.wall@computer.org>
2005-08-10 16:50 ` Re: PG 8.0.3 - PreparedStatement Can't Use Query Methods Kris Jurka <books@ejurka.com>
2005-08-10 17:18   ` David Wall <d.wall@computer.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox