Mercurial > hg > monetdb-java
comparison src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java @ 167:89e954e7acbb
Correct implementation of method Connection.isValid().
It should restore the original query timeout after changing the query timeout via Stmt.setQueryTimeout()
Do the close() statements in the finally clause, so its is always done (and resources are released), also when an SQLException occurs.
In case the connection is in "Current transaction is aborted (please ROLLBACK)" state, the connection is valid and usable
and thus method Connection.isValid() should return true.
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Thu, 14 Sep 2017 13:57:08 +0200 (2017-09-14) |
parents | 7c9e386fe49a |
children | 60063c67f9e7 8700d9ef2ace |
comparison
equal
deleted
inserted
replaced
166:5c575fb21be0 | 167:89e954e7acbb |
---|---|
1230 return false; | 1230 return false; |
1231 | 1231 |
1232 // ping db using query: select 1; | 1232 // ping db using query: select 1; |
1233 Statement stmt = null; | 1233 Statement stmt = null; |
1234 ResultSet rs = null; | 1234 ResultSet rs = null; |
1235 boolean isValid = false; | |
1235 try { | 1236 try { |
1236 stmt = createStatement(); | 1237 stmt = createStatement(); |
1237 stmt.setQueryTimeout(timeout); | 1238 if (stmt != null) { |
1238 rs = stmt.executeQuery("SELECT 1"); | 1239 int original_timeout = stmt.getQueryTimeout(); |
1239 rs.close(); | 1240 if (timeout > 0 && original_timeout != timeout) { |
1240 rs = null; | 1241 // we need to change the requested timeout for this test query |
1241 stmt.close(); | 1242 stmt.setQueryTimeout(timeout); |
1242 return true; | 1243 } |
1243 } catch (Exception e) { | 1244 rs = stmt.executeQuery("SELECT 1"); |
1245 if (rs != null && rs.next()) { | |
1246 isValid = true; | |
1247 } | |
1248 if (timeout > 0 && original_timeout != timeout) { | |
1249 // restore the original server timeout value | |
1250 stmt.setQueryTimeout(original_timeout); | |
1251 } | |
1252 } | |
1253 } catch (SQLException se) { | |
1254 String msg = se.getMessage(); | |
1255 // System.out.println("Con.isValid(): " + msg); | |
1256 if (msg != null && msg.equals("Current transaction is aborted (please ROLLBACK)")) { | |
1257 isValid = true; | |
1258 } | |
1259 /* ignore stmt errors/exceptions, we are only testing if the connection is still alive and usable */ | |
1260 } finally { | |
1244 if (rs != null) { | 1261 if (rs != null) { |
1245 try { | 1262 try { |
1246 rs.close(); | 1263 rs.close(); |
1247 } catch (Exception e2) {} | 1264 } catch (Exception e2) { /* ignore error */ } |
1248 } | 1265 } |
1249 if (stmt != null) { | 1266 if (stmt != null) { |
1250 try { | 1267 try { |
1251 stmt.close(); | 1268 stmt.close(); |
1252 } catch (Exception e2) {} | 1269 } catch (Exception e2) { /* ignore error */ } |
1253 } | 1270 } |
1254 } | 1271 } |
1255 return false; | 1272 return isValid; |
1256 } | 1273 } |
1257 | 1274 |
1258 /** | 1275 /** |
1259 * Returns the value of the client info property specified by name. | 1276 * Returns the value of the client info property specified by name. |
1260 * This method may return null if the specified client info property | 1277 * This method may return null if the specified client info property |