简体   繁体   中英

H2 embedded mode and software crashes

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 ?

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

@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>)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM