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

后端 未结 13 1111
时光取名叫无心
时光取名叫无心 2020-12-12 23:52

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 <

相关标签:
13条回答
  • 2020-12-13 00:28

    answer for this question => 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]

    close all tab from your browser where open h2 database also Exit h2 engine from your pc

    0 讨论(0)
  • 2020-12-13 00:30

    You can also visit the "Preferences" tab from the H2 Console and shutdown all active sessions by pressing the shutdown button.

    0 讨论(0)
  • 2020-12-13 00:30

    I got clue from Saman Salehi above. My usecase: Preparing REST application for client-side load balancing(running two JVM instances of REST). Here my MVC application will call this REST application that has ActiveMQ backend for DATA. I had the problem when I ran two instances of REST application in eclipse and trying to run both instances at the same time with the following configuration

    spring.datasource.url=jdbc:h2:file:./Database;
    spring.jpa.properties.hibernate.hbm2ddl.auto=update
    

    After adding DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE

    spring.datasource.url=jdbc:h2:file:./Database;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
    

    Both instances are running and showing in Eureka dasboard.

    Don't close the database when the VM exits : jdbc:h2:;DB_CLOSE_ON_EXIT=FALSE

    Multiple processes can access the same database without having to start the server manually ;AUTO_SERVER=TRUE

    Further reading: http://www.h2database.com/html/features.html

    0 讨论(0)
  • 2020-12-13 00:34

    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:\Database\Data\production;"/>
    

    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.

    0 讨论(0)
  • 2020-12-13 00:38

    Simple step: Go to the task manager and kill the java process

    then start your apllication

    0 讨论(0)
  • 2020-12-13 00:42

    I'm using h2db with a test T24 tafj application, I had the same problem but I managed to resolve it by identifying the application that is running h2 (launched when I attempted to setup a database connection).

    ps aux|grep java
    

    will give output as:

    sysadmin 22755  3.2  0.1 5189724 64008 pts/3   Sl   08:28   0:00 /usr/java/default/bin/java -server -Xmx2048M -XX:MaxPermSize=256M -cp h2-1.3.175.jar:/r14tafj/TAFJ/dbscripts/h2/TAFJFunctions.jar org.h2.tools.Server -tcp -tcpAllowOthers -baseDir /r14tafj/t24/data
    

    now kill this with its process id:

    kill -9 22755
    

    and at last remove the lock file:

    rm -f dbname.lock.db
    
    0 讨论(0)
提交回复
热议问题