Search code examples
javacrashh2embedded-database

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 ?


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