遇到h2 db資料庫檔案被鎖住了該怎麼辦?
阿新 • • 發佈:2019-02-06
H2 DB 是一種輕量級的檔案型資料庫,還提供 Web Console 視覺化操作資料庫,支援標準的JDBC API。
因是檔案型資料庫,在使用的時候經常會遇到如下問題:
Error Message:
Database may be already in use: "Locked by another process"
Detail Stack:
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]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:169)
at org.h2.message.DbException.get(DbException.java:146)
at org.h2.store.FileLock.getExceptionAlreadyInUse(FileLock.java:435)
at org.h2.store.FileLock.lockFile (FileLock.java:332)
at org.h2.store.FileLock.lock(FileLock.java:128)
at org.h2.engine.Database.open(Database.java:539)
at org.h2.engine.Database.openDatabase(Database.java:219)
at org.h2.engine.Database.<init>(Database.java:214)
at org.h2.engine.Engine.openSession(Engine.java :56)
at org.h2.engine.Engine.openSession(Engine.java:159)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:138)
at org.h2.engine.Engine.createSession(Engine.java:121)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:124)
at java.lang.Thread.run(Unknown Source)
at org.h2.engine.SessionRemote.done(SessionRemote.java:538)
at org.h2.engine.SessionRemote.initTransfer(SessionRemote.java:109)
at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:373)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:267)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:110)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:94)
at org.h2.Driver.connect(Driver.java:72)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.h2.tools.RunScript.process(RunScript.java:312)
at org.h2.tools.RunScript.runTool(RunScript.java:140)
at org.h2.tools.RunScript.main(RunScript.java:68)
在工程裡面會生成 “dbname.lock.db” , 怎麼刪除都刪不掉。 原因可想而知,某個程序佔用了該檔案,必須先關閉該程序。
解決辦法:
開啟“終端“ | “命令列“,輸入指令 : ps aux | grep java
找到帶有關鍵字 h2
的程序,例如程序編號是:356322, 輸入命令kill 之
kill 356322
之後,就可以刪除 lock住的檔案了
rm -f dbname.lock.db
重啟 h2 db 服務就不會報錯了!