Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should java.sql.Statement.isClosed() throw an SQLException?

Tags:

java

jdbc

The API doc states @throws SQLException if a database access error occurs. Does this mean that implementations could/should also check whether the underlying database connection is working rather than just returning true if the client side state of the object isn't closed?

like image 235
makrom Avatar asked Feb 21 '26 01:02

makrom


1 Answers

Does this mean that implementations could/should also check whether the underlying database connection is working rather than just returning true if the client side state of the object isn't closed?

It means that some JDBC driver implementations1 might (or could) check an underlying database connection. If it does, and a database access error occurs, then the exception may be thrown.

It does NOT mean that it should (or must) check the connection.

I won't speculate on why a JDBC driver would need to check the connection. Or even whether such a driver actually exists. For most JDBC driver implementations, a client-side check should be sufficient, and the exception wouldn't be thrown by isClosed()


As to these followups in comments:

how would you generally handle that exception without knowing the underlying driver?

That will depend on the application. If there is nothing better, the application could log the exception and exit.

how would you implement a driver in accordance with the interface documentation?

One way would be to not check the connection and not throw the exception. That conforms with the interface documentation. Please see above where I wrote about what spec is saying and what it is not saying.

But as I said above, I am not going to speculate on why a (hypothetical) JDBC driver would need to check the connection.


1 - I am using the words "should", "could", "may", "might" and "must" in their normal English sense.

like image 128
Stephen C Avatar answered Feb 22 '26 14:02

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!