Question

As you all know H2 is a powerful pure Java DBMS with several features like server/client mode and embedded
When working on a little software with a H2 database ,I ran into a problem :
the software crashes and the connection remains open ,when restarting the software I cannot access the database again (It's in embedded mode so it's locked) and to bypass this problem I had to shutdown Java virtual machine manually using task manager
Is there a way in case such an event happens (application crash) and yet I can restore the connection normally ?

Was it helpful?

Solution

When the JVM exists normally, H2 normally closes the database itself, if you haven't done it yourself explicitly.

In worst case scenarios, you might be able to use Thread#setDefaultUncaughtExceptionHandler to terminate the JVM safely and/or close the database

OTHER TIPS

I have strong suspicion your program suffer from poor code failing to close connection on exception. Review all your database code and make sure all connection are closed even when exception is thrown.

A common approach is to close the connection on the finally block of try-catch-finally.

@Ossama Nasser: yes, you can trap everything. And you better do it, or know in advance which exceptions you decide to let terminate your program, and what effect there will be on your programs resources.

Unix-C programs use setjmp() & longjmp(). It is primitive, but it is effective for most signals.

However, the JVM offers an alternative to the approach of "put a try/finally around everything in main()":

Runtime.getRuntime().addShutdownHook(new <whatever you write as a handler class>)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top