Database lock acquisition failure and hsqldb

2019-03-17 15:22发布

问题:

I was trying to connect to a hsql db. I created one by running from C:\myhsql:

java -cp .;C:\hsql\lib\hsqldb.jar org.hsqldb.Server -database.0 file:db\mydb -dbname.0 MYDB

This created mydb in a directory called db. This folder now has a .lck,tmp,script,properties files with name mydb, and similar files with name MYDB in current folder .

In java code I tried

Class.forName("org.hsqldb.jdbcDriver");
connection = DriverManager.getConnection("jdbc:hsqldb:file:db/sjdb", "SA", "");

When I run the program, I am getting this error:

java.sql.SQLException: Database lock acquisition failure: lockFile: org.hsqldb.persist.LockFile@f3811c1a[file =C:\myhsql\db\mydb.lc
k, exists=true, locked=false, valid=false, ] method: checkHeartbeat read: 2010-10-19 12:46:09 heartbeat - read: -6750 ms.
...

Here is the stacktrace:

java.sql.SQLException: Database lock acquisition failure: lockFile: org.hsqldb.persist.LockFile@f3811c1a[file =C:\myhsql\db\sjdb.lc
k, exists=true, locked=false, valid=false, ] method: checkHeartbeat read: 2010-10-19 12:46:09 heartbeat - read: -6750 ms.
        at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
        at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
        at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
        at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
        at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at ConnectHSQLDB.main(ConnectHSQLDB.java:20)
Caused by: org.hsqldb.HsqlException: Database lock acquisition failure: lockFile: org.hsqldb.persist.LockFile@f3811c1a[file =C:\myhsql\db\sjdb.lck, exists=true, locked=false, valid=false, ] method: checkHeartbeat read: 2010-10-19 12:46:09 heartbeat - read: -6750 ms.
        at org.hsqldb.error.Error.error(Unknown Source)
        at org.hsqldb.persist.LockFile.newLockFileLock(Unknown Source)
        at org.hsqldb.persist.Logger.acquireLock(Unknown Source)
        at org.hsqldb.persist.Logger.openPersistence(Unknown Source)
        at org.hsqldb.Database.reopen(Unknown Source)
        at org.hsqldb.Database.open(Unknown Source)
        at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
        at org.hsqldb.DatabaseManager.newSession(Unknown Source)
        ... 6 more
java.lang.NullPointerException
        at ConnectHSQLDB.main(ConnectHSQLDB.java:32)

Can somebody tell me what I am doing wrong? I can connect to the db using SwingDBManager and can insert, delete, and select records in the db. I was not running DBManager when I tried the java code. Still the lock problem happens.

回答1:

The first command starts a server. This server locks the database files so that "others" cannot modify them. You should use "-dbname.0 mydb" instead of "MYDB" as it should be in lowercase.

Your Java connection URL to connect to the database is wrong. You should use "jdbc:hsqldb:hsql://localhost/mydb" as the connection string. While the database files are locked by the server, you can access the database server but you cannot access the database "in-process" with a file: URL.



回答2:

If you have any other client running that connects to your db you need to close that.



回答3:

Whatever way you have tried is correct.

You don't have to start the HSQLDB Server using seperate java command, below line is not required, as it will lock the database. Prevent other process from starting and locking db.

java -cp .;C:\hsql\lib\hsqldb.jar org.hsqldb.Server -database.0 file:db\mydb -dbname.0 MYDB

just run the jdbc program

java -cp hsqldb.jar  HSQLAccess 

below line

jdbc:hsqldb:file:db/sjdb

will start the database and will give result.

in this way you don't have to start the server seperately, just have to run the program, which will start and stop HSQLDB for you.

import java.sql.*;

public class HSQLAccess {

    public static void main(String args[]) throws Exception
    {
        Connection con = null;
        try
        {
            Class.forName("org.hsqldb.jdbcDriver");         
            con = DriverManager.getConnection("jdbc:hsqldb:file:db/sjdb", "sa","");    

            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM CMDS_WO_MASTER");
            while(rs.next())
            {
                System.out.println(rs.getString(1));
            }

            con.close();

        }
        catch(Exception ex )
        {
            ex.printStackTrace();
        }
        finally
        {
            if(con!=null)
            {
                 con.close();
            }
        }
    }
}


回答4:

I face this error because I wanted to view a currently opened database in another client like IntelliJ database while the server is using the same db

so to make hsql db able to be connected to multiple clients, use

hsqldb.lock_file=false

so the connection url will be like

jdbc:hsqldb:file:./db/myDbInFile;hsqldb.lock_file=false


回答5:

on my Mac, the port for Connector on HTTP changed from 8080 to 8443 on server.xml. and that is what was giving me this error: both HTTP and HTTPS schema were using the same port



回答6:

try using the following connection url in windows connection = DriverManager.getConnection("jdbc:hsqldb:file:///c:/hsqldb/mydb", "SA", "");



回答7:

I just closed NetBeans, deleted database.lck and executed the application again. Everything worked fine.



标签: hsqldb