Monday, 26 August 2013

release db connections in standalone application

release db connections in standalone application

I am working on a standalone application using Spring/JPA and I am trying
to release properly the database resources used.
In a Web application using tomcat for example, we shutdown the server, and
this way, we let Tomcat manage the resources.
But as I am in a standalone app, I have to take care about this, I use
Runtime.getRuntime().addShutdownHook to "catch" the shutdown event and
call ((ClassPathXmlApplicationContext) context).close();, something like
this:
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
((ClassPathXmlApplicationContext) context).close();
}
It works but with an exception in the stacktrace if a thread was using a
connection. I am wondering if there is another option? Maybe getting a
list of open transactions and force them to rollback?

No comments:

Post a Comment