jdbc4 isValid(int) implementation - Mailing list pgsql-jdbc

From Luis Flores
Subject jdbc4 isValid(int) implementation
Date
Msg-id CA+nXnG_-3vZ=WOSh0ON6j+EDc4KJmkr-bCo2TUrZf-F8ROUuVA@mail.gmail.com
Whole thread Raw
List pgsql-jdbc
Hello,

I develop some java desktop apps used in laptops. The apps use remote postgresql databases. To deal with sleep/interrupted connection issues I want to use JDBC4 isValid(int) method. Current driver didn't implemented it, so here it is a sample implementation (to replace in AbstractJdbc4Connection.java file):


    public boolean isValid(int timeout) throws SQLException
    {
    if (timeout < 0) {
    throw new PSQLException(GT.tr("Invalid timeout ({0}<0).", timeout), PSQLState.INVALID_PARAMETER_VALUE);
    }
    boolean valid = false;
    try {
       if (!isClosed()) {
        Statement stm = createStatement();
        stm.setQueryTimeout( timeout );
        stm.executeQuery( "SELECT 1" ).close();
        valid = true;
       }
    }
    catch ( SQLException e) {
    getLogger().log(GT.tr("Validating connection."),e);
}
        return valid;
    }


That's it.

Thanks,

Luis Flores

pgsql-jdbc by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: JDBC builds broken
Next
From: Dave Cramer
Date:
Subject: Re: JDBC builds broken