How long does a JDBC connection live if not closed

2019-08-04 08:13发布

问题:

If I open up a JDBC connection in my java code and forget to close it, will it remain open for ever? Or is there a default time out value that I can specify somewhere?

回答1:

In first place, you really need to fix the code to close it. No excuses.

As to the concrete question, that depends on the DB server used. It's the DB server who will timeout and reclaim it. Refer the DB server specific administration manual for defaults and how to change it. In case of for example MySQL, it's the wait_timeout setting which defaults to 28800 seconds (8 hours).



回答2:

It depends on the specifics of the JDBC driver implementation. Some might have a timeout. Some might close when the connection object is garbage collected and finalize is called, but (1) you don't know how long after you stopped referencing the object it gets garbage collected, and (2) the finalize is not reliably called.



标签: jdbc