H2数据库错误。数据库可能已经在使用中。"被其他进程锁定"[英] H2 database error: Database may be already in use: "Locked by another process"

本文是小编为大家收集整理的关于H2数据库错误。数据库可能已经在使用中。"被其他进程锁定"的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试使用Java应用程序的H2数据库.

我通过H2控制台创建数据库及其表,然后尝试使用

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

但是我收到以下错误:

线程"主" org.h2.dbc.jdbc.jdbcsqlexception中的例外:数据库可能已经在使用中:"由另一个进程锁定".可能的解决方案:关闭所有其他连接;使用服务器模式[90020-161]

我尝试删除dbname.lock.db文件,但会自动重新创建.

如何解锁数据库从我的Java程序中使用它?

推荐答案

H2仍在运行(我可以保证).您需要为多个用户使用TCP连接,例如 - >

<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/C:\Database\Data\production;"/>

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

这也意味着您需要在TCP模式下启动服务器.尊敬的是,它在文档中非常直截了当.

强制杀死该过程(Windows的Javaw.exe),并确保任何可能启动的应用程序都会关闭.您有一个主动锁.

其他推荐答案

我也有同样的问题. 在Intellj中,当我想使用 H2 数据库时,我的程序运行时,我会出现相同的错误. 为了解决此问题,我从

更改了连接URL
spring.datasource.url=jdbc:h2:file:~/ipinbarbot

to:

spring.datasource.url=jdbc:h2:~/ipinbarbot;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE

然后我的问题消失了.现在,我可以在程序时连接到" ipinbarbot"数据库. 如果您使用冬眠,也不要忘记拥有:

spring.jpa.hibernate.ddl-auto = update

goodluck

其他推荐答案

我正在使用H2DB和测试T24 TAFJ应用程序,我遇到了相同的问题,但是我通过识别正在运行H2的应用程序来解决它(当我尝试设置数据库连接时启动).<<<<<<<<<<<<<<<<<<<<

ps aux|grep java

将输出为:

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

现在使用其流程ID杀死它:

kill -9 22755

最后删除锁定文件:

rm -f dbname.lock.db

本文地址:https://www.itbaoku.cn/post/597505.html

问题描述

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?

推荐答案

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.

其他推荐答案

I had the same problem. in Intellj, when i want to use h2 database when my program was running i got the same error. For solve this problem i changed the connection url from

spring.datasource.url=jdbc:h2:file:~/ipinbarbot

to:

spring.datasource.url=jdbc:h2:~/ipinbarbot;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE

And then my problem gone away. now i can connect to "ipinbarbot" database when my program is. If you use Hibernate, also don't forget to have:

spring.jpa.hibernate.ddl-auto = update

goodluck

其他推荐答案

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