jdbc4 isValid(int) implementation

From: Luis Flores <luiscamposflores(at)gmail(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: jdbc4 isValid(int) implementation
Date: 2012-01-02 23:16:19
Message-ID: CA+nXnG_-3vZ=WOSh0ON6j+EDc4KJmkr-bCo2TUrZf-F8ROUuVA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2012-01-03 11:51:57 Re: JDBC builds broken
Previous Message Andrew Dunstan 2012-01-02 21:09:54 Re: JDBC builds broken