Skip to content
Advertisement

H2 database error: Database may be already in use: “Locked by another process”

I am trying to use the H2 database from a Java application.

I created the database and its tables through the H2 Console and then I try to connect from Java using

Connection con = DriverManager.getConnection("jdbc:h2:~/dbname", "username", "password");

However I receive the following error:

Exception in thread “main” org.h2.jdbc.JdbcSQLException: Database may be already in use: “Locked by another process”. Possible solutions: close all other connection(s); use the server mode [90020-161]

I tried to delete the dbname.lock.db file but it is automatically re-created.

How can I unlock the database to use it from my Java program?

Advertisement

Answer

H2 is still running (I can guarantee it). You need to use a TCP connection for multiple users such as ->

<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/C:DatabaseDataproduction;"/>

OR

DriverManager.getConnection("jdbc:h2:tcp://localhost/server~/dbname","username","password");

It also means you need to start the server in TCP mode. Honesetly, it is pretty straight forward in the documentation.

Force kill the process (javaw.exe for Windows), and make sure that any application that might have started it is shut down. You have an active lock.

Advertisement