Skip to content

Commit 773f8e4

Browse files
author
Dave Cramer
committed
added isValid implementation from Luis Flores
1 parent 6bd8784 commit 773f8e4

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

org/postgresql/jdbc4/AbstractJdbc4Connection.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2004-2011, PostgreSQL Global Development Group
44
*
55
* IDENTIFICATION
6-
* $PostgreSQL: pgjdbc/org/postgresql/jdbc4/AbstractJdbc4Connection.java,v 1.11 2011/03/31 06:25:42 jurka Exp $
6+
* $PostgreSQL: pgjdbc/org/postgresql/jdbc4/AbstractJdbc4Connection.java,v 1.12 2011/08/02 13:49:23 davecramer Exp $
77
*
88
*-------------------------------------------------------------------------
99
*/
@@ -117,8 +117,28 @@ private static void appendArray(StringBuffer sb, Object elements)
117117
public boolean isValid(int timeout) throws SQLException
118118
{
119119
checkClosed();
120-
throw org.postgresql.Driver.notImplemented(this.getClass(), "isValid(int)");
121-
}
120+
if (timeout < 0) {
121+
throw new PSQLException(GT.tr("Invalid timeout ({0}<0).", timeout), PSQLState.INVALID_PARAMETER_VALUE);
122+
}
123+
boolean valid = false;
124+
Statement stmt = null;
125+
try {
126+
if (!isClosed()) {
127+
stmt = createStatement();
128+
stmt.setQueryTimeout( timeout );
129+
stmt.executeQuery( "SELECT 1" );
130+
valid = true;
131+
}
132+
}
133+
catch ( SQLException e) {
134+
getLogger().log(GT.tr("Validating connection."),e);
135+
}
136+
finally
137+
{
138+
if(stmt!=null) try {stmt.close();}catch(Exception ex){}
139+
}
140+
return valid;
141+
}
122142

123143
public void setClientInfo(String name, String value) throws SQLClientInfoException
124144
{

0 commit comments

Comments
 (0)