NikhilWanpal
NikhilWanpal

Reputation: 3000

In what cases can a statement.close throw exception? What does it mean for the connection?

Everywhere we see that the Statement.close() is 'handled' by eating up the exception that it throws. What are the cases where it can throw an exception in the first place? And what does it mean for the connection with which this statement was created?

In other words, when does statement.close() throw an exception and would the connection still be 'healthy' to be used for creating new statements?

Also, what happens if resultset.close() throws?

Upvotes: 1

Views: 1375

Answers (3)

Mark Rotteveel
Mark Rotteveel

Reputation: 108982

When you close a statement, a lot of things can happen. These basic things can happen when closing a statement:

  • The open result set - if any - is closed, which may require communication to the database
  • The statement handle on the database server is released, which requires communication to the database

Given this involves communication to the database, all kinds of errors can occur: file system errors, network connection problems, etc. These may be safe to ignore, but could also indicated something very wrong with your application or database.

A secondary effect of a statement close can be a transaction completion (a commit or rollback). This could - for example - happen in auto-commit mode when you execute a data-modifying statement that produces a result set: the transaction ends when the result set is closed by the close of the statement. If this transaction commit fails, and you ignore it, your application may have just had a data-loss event (because the data was not persisted), and you just went ahead and ignored it.

In other words: you should not just ignore or swallow exceptions from Statement.close() unless you are absolutely sure there will be no detrimental effects. At minimum log them so you can trace them in your logs (and maybe define alerts on the number of exceptions logged), but always consider if you need to wrap them in application-specific exceptions and throw them higher up the call chain for handling, or - for commit failures - if you need to retry anything.

Upvotes: 0

Andreas
Andreas

Reputation: 159086

First, consider what the close() method might need to do, and what might cause an exception.

E.g. a PreparedStatement might have created a stored procedure, which needs to be deleted by the close() method. executeQuery() may have opened a cursor, which is used by the ResultSet, and close() needs to close that cursor.

Exception could of course be an internal error, but is most likely a communication error, preventing the close operation from succeeding.

So, what does that mean? It means that resources are not being explicitly cleaned up. Since your operation is already complete, it's generally ok to ignore those close() exceptions, since resources will be reclaimed eventually anyway.

However, since the cause is probably a communication error, the connection is likely broken, which means that you'll just get another exception on whatever you try next, making it even less likely that your handling of the close() exception matters.

To be safe, an exception means that something is wrong, and unless you examine the exception to understand how bad it is, you should abort whatever you're doing. A new connection should be established if you want to try again.

But, as already mentioned, ignoring close() exceptions aren't really a big issue. It may lead to resource leaks, but if the problem is bad, you're just going to get another exception on your next action anyway.

Upvotes: 1

ItFreak
ItFreak

Reputation: 2369

Simplest case for such an exception: the connection, that handled the statement is closed before you are trying to close the statement or if the statement was closed -somehow- already. speaking for jdbc, the connection should be healthy. In general: As Peter stated, if the documentation of the driver does not contain any recommendations how to handle such an exception, you can only log or debug it. maybe you could re-establish the connection to be sure it is healthy.

Upvotes: 0

Related Questions