Running the hsqldb database programmatically

I want to programmatically start the HSQLdb server. I create a runnable as shown below and then run it in a thread.

   Runnable hsqlRunnable = new Runnable() {
                public void run()
                {

                    HsqlProperties props = new HsqlProperties();
                    props.setProperty("server.database.0", "file:C:\Documents and Settings\BThirup\Application Data\Rockwell Automation\FactoryTalk ProductionCentre\logs\ApplicationLog\mydb;shutdown=true;");
                    props.setProperty("server.dbname.0", "xdb");
org.hsqldb.Server server = new org.hsqldb.Server();

                    try {
                        server.setProperties(props);
                    } catch (Exception e) {
                        return;
                    }

                    server.start();
}
}

I get the error: [Thread [HSQLDB Server @ 4db602,6, main]]: shutdown due to lack of open databases

Someone can indicate where I am wrong.

Thanks for helping Bala

+3
source share
3 answers

As you commented, server.setTrace (true) helps in troubleshooting. Also server.setSilent (false).

But you should not create and run Runnable to start the server. The Server class does all this and starts the necessary threads.

0
source

You can use a separate class to manage the HSQLdb server instance:

public class DBManager {

final String dbLocation = "c:\\temp\\"; // change it to your db location
org.hsqldb.server.Server sonicServer;
Connection dbConn = null;

public void startDBServer() {
    HsqlProperties props = new HsqlProperties();
    props.setProperty("server.database.0", "file:" + dbLocation + "mydb;");
    props.setProperty("server.dbname.0", "xdb");
    sonicServer = new org.hsqldb.Server();
    try {
        sonicServer.setProperties(props);
    } catch (Exception e) {
        return;
    }
    sonicServer.start();
}

public void stopDBServer() {
    sonicServer.shutdown();
}

public Connection getDBConn() {
    try {
        Class.forName("org.hsqldb.jdbcDriver");
        dbConn = DriverManager.getConnection(
                "jdbc:hsqldb:hsql://localhost/xdb", "SA", "");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dbConn;
}
}

:

public class WFProcess extends Thread {

DBManager dbm = new DBManager();

public static void main(String[] args) {
    (new WFProcess()).start();
}

public void run() {
    dbm.startDBServer();

    // some usefull server work here
    Connection conn = dbm.getDBConn();
    try {
        Statement stmt = conn.createStatement();
        stmt.executeQuery("CREATE TABLE IF NOT EXISTS answers (num INT IDENTITY, answer VARCHAR(250))");
        stmt.executeQuery("INSERT INTO answers (answer) values ('this is a new answer')");
        ResultSet rs = stmt.executeQuery("SELECT num, answer FROM answers");
        while (rs.next()) {
            System.out.println("Answer number: " + rs.getString("num")
                    + "; answer text: " + rs.getString("answer"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // end of usefull server work

    dbm.stopDBServer();
}
}
+4

HSQL 2.3.x . , HSQL WebServer ( HSQL, 80 .)

fooobar.com/questions/1312599/...

0
source

All Articles