Database lock acquisition failure and hsqldb

前端 未结 8 2049
终归单人心
终归单人心 2020-12-30 00:00

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\\m         


        
相关标签:
8条回答
  • 2020-12-30 00:16

    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.

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

    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

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

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

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

    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();
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 00:33

    You can access HSQLDB database that has lock by:

    1- updating my-db-name.properties file and add:

    hsqldb.lock_file=false
    

    2- or removing my-db-name.lck file.

    3- or connect to the database using pass the properties as parameters:

    #Without Sqltool:
        java -cp [jar-path]/hsqldb-2.4.0.jar --inlineRc=url=jdbc:hsqldb:file:/[my-db-file-path]/[db-name];readonly=true;hsqldb.lock_file=false,user=sa
    
    #With Sqltool
        java -cp [jar-path]/hsqldb-2.4.0.jar:[jar-path]/sqltool-2.4.0.jar org.hsqldb.cmdline.SqlTool --inlineRc=url=jdbc:hsqldb:file:/[my-db-file-path]/[db-name];readonly=true;hsqldb.lock_file=false,user=sa
    

    Other HSQLDB database parameters can be found here, however note that only few of them can be updated after first time. http://hsqldb.org/doc/2.0/guide/dbproperties-chapt.html

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

    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
    
    0 讨论(0)
提交回复
热议问题