Close all JDBC Resources using connection object.
Hi,
I have a connection broker class that gives connections to the clients. What i want to do is close all JDBC resources that were created using this connection object like Statement, PreparedStatement, ResultSet etc.
Icannot close the connection as the connection might have to be reallocated. If the JDBC resources arent properly closed by the clients it throws exceptions like Maximum No. of Cursors Exceeded Exception.
I couldnt find anything in the API, I would be really thanful if you could suggest a solution or give some pointers.
Thanks.
# 1
> If the JDBC resources arent properly closed by the clients it throws exceptions
> like Maximum No. of Cursors Exceeded Exception.
Then just fix code so that it gently closes them directly after use?
# 2
I dont understand why you have a connection broker class. Instead, you should provide the datasource reference to each user. Within his function, he should get a connection from the datasource, use it , then close the connection (in a finally block of a try/catch/finally) all within the same function. Also, he shouldn't close the datasource or otherwise mess with it. You will not have to worry about exceeding max number of connections then.
# 3
> I have a connection broker class that gives
> connections to the clients. What i want to do is
> close all JDBC resources that were created using this
> connection object like Statement, PreparedStatement,
> ResultSet etc.
So assuming you have a connection pool and the Connection objects in a Collection, just iterate through and close the Connections.
> I cannot close the connection as the
> connection might have to be reallocated.
Then don't close the Connection! Simply have the user release/recycle the Connection back to the pool after using it.
> If the JDBC
> resources arent properly closed by the clients it
> throws exceptions like Maximum No. of Cursors
> Exceeded Exception.
> I couldnt find anything in the API, I would be really
> thanful if you could suggest a solution or give some
> pointers.
You should read up on how to implement connection pooling and you'll see that the Connections are established on program startup and closed on program shutdown. Connections aren't closed while the program is running, simply used and reused indefinitely.
# 4
Your correct, the connection pool doesnt actually close the connection when you call conn.close(), but just frees it up for reuse. I suppose the vendor's choice of callling it close() isnt so hot.