Marsh
Marsh

Reputation: 8125

Will JDBC database connections get released if they're used in a thread when Tomcat server shuts down?

We have a Tomcat stack for our web app, and use javax.sql.DataSource for our database connections. We have some methods that do a lot of database work that users don't really need to wait for that we want to put in a thread and run in the background and let users continue on their merry way.

The simplest method I came up with to do this is to wrap the database access (DAO) code in an anonymous Runnable and pass it to a Thread.

The senior developer is concerned but unsure about whether, if the Tomcat server is stopped in the middle of execution, the database connection will be released or if it will sit idle in transaction until the end of time.

Is this a likely problem? If so, how would we resolve it so that if the thread is interrupted/killed the database connection gets properly released?

PS - I'm pretty sure I've left something out, just ask for clarification in the comments.

Upvotes: 2

Views: 778

Answers (1)

M A
M A

Reputation: 72854

When a process is terminated all TCP/IP connections established by the process will be shut down. However a leak may still be present if the database server (depending on the vendor/version) does not recognize this and still keeps its sessions alive. This may happen because the connection was not closed in the normal way (i.e. calling connection.close()) by the application.

There should be a way to check for connections or sessions that are still alive on the DB server if you have the sufficient privileges.

Upvotes: 2

Related Questions