Levi Putna
Levi Putna

Reputation: 3073

Will the Java JRE close connections if an applications crases?

Will the Java JRE close database connections if an applications crashes or exits without closing its active connections? If not is it the responsibility of the database to time out these connections?

I understand that if Java crashes then the database will need to time out all open connections as there is nothing else to do the job.

EDIT Additional thought. If a WAR deployed in Tomcat crashes, will the Tomcat server cleanup the open connections?

Upvotes: 3

Views: 424

Answers (2)

Herm
Herm

Reputation: 2999

Most times your application should close the connections in any proper errorhandlers. I prefer to set up the data source in Tomcat, like Ewald mentioned. There you can define a maximum of connections, and a timeout in your context.xml. When your application stacks too much connections it will release it.

Upvotes: 0

Ewald
Ewald

Reputation: 5751

That really depends.

If your JRE itself crashes, it might not be able to close all the connections. If just your application crashes, it might be able to close connections when it frees the resources. This seems to be the case most of the time, in my experience, as long as the JRE itself does not die. The best defence is of course proper error handling and making sure you do not have more connections open than required.

From my experience, it's better to set up a data source in Tomcat, that way, even if your application crashes, it's not an issue with open resources. I'm a rather big fan of making my application server handle as much resource management as possible, that way I am protected a bit more from my sometimes adventurous code.

Upvotes: 2

Related Questions