开发者

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

I have the following code in Java:

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

Instead of working, I am awarded with:

java.sql.SQLException: Connection already closed

My connection object is a Snaq.db.CacheConnection

I checked the JavaDocs for isClosed, and they state that:

This method generally cannot be called to determine whether a connection to a database is valid or invalid. A typical client can determine that a connection is invalid by catching any exceptions that might be thrown when an oper开发者_高级运维ation is attempted.

So my questions are:

1) What good is JDBC's isClosed() anyway? Since when do we use Exceptions in Java to check for validity?

2) What is the correct pattern to close a database? Should I just close and swallow exceptions?

3) Any idea why would SnaqDB be closing the connection? (My backend is a Postgres 8.3)


I'll answer your questions with corresponding numbers:

  1. I agree with you, it seems strange that isClosed provides the closed state only on a best effort basis, and that your code still has to be prepared to catch the exception when closing the connection. I think the reason is that the connection may be closed at any time by the database, and so any status returned by a query state method like isClosed is intrinsicly stale information - the state may change between checking isClosed and calling close on the Connection.
  2. Calling close has no affect on your data and on previous queries. JDBC operations execute with synchronous results, so all useful execution has either succeeded or failed by the time isClosed is called. (True with both autoCommit or explicit transaction boundaries.) If your application is a single user accessing a local database, then perhaps showing the error to the user might help them diagnose problems. In other environments, logging the exception and swallowing it is probably the best course of action. Either way, swallowing the excpetion is safe, as has no bearing on the state of the database.
  3. Looking at the source for SnaqDB CacheConnection, the isClosed method delegates to the underlying connection. So the problem is not there, but lies with the defined contract for isClosed() and Connection.close() throwing an exception.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜