What is JDBC's Connection.isClosed() good for, and why is Snaq DBPool misbehaving on close? -


i have following code in java:

if(!conn.isclosed()) {     conn.close(); } 

instead of working, awarded with:

java.sql.sqlexception: connection closed

my connection object snaq.db.cacheconnection

i checked javadocs isclosed, , state that:

this method cannot called determine whether connection database valid or invalid. typical client can determine connection invalid catching exceptions might thrown when operation attempted.

so questions are:

1) jdbc's isclosed() anyway? since when use exceptions in java check validity?

2) correct pattern close database? should close , swallow exceptions?

3) idea why snaqdb closing connection? (my backend postgres 8.3)

i'll answer questions corresponding numbers:

  1. i agree you, seems strange isclosed provides closed state on best effort basis, , code still has prepared catch exception when closing connection. think reason connection may closed @ time database, , status returned query state method isclosed intrinsicly stale information - state may change between checking isclosed , calling close on connection.
  2. calling close has no affect on data , on previous queries. jdbc operations execute synchronous results, useful execution has either succeeded or failed time isclosed called. (true both autocommit or explicit transaction boundaries.) if application single user accessing local database, perhaps showing error user might them diagnose problems. in other environments, logging exception , swallowing best course of action. either way, swallowing excpetion safe, has no bearing on state of database.
  3. looking @ source snaqdb cacheconnection, isclosed method delegates underlying connection. problem not there, lies defined contract isclosed() , connection.close() throwing exception.

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -