Database lock acquisition failure and hsqldb

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.


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

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();
            }
        }
    }
}

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

Tags:

Hsqldb