Lucia
Lucia

Reputation: 371

Can´t delete HSQLDB files

I´m developing an application that basically is a installer. At startup creates an HSQL database, a schema and insert some data. After that, if some conditions are met, i want to completely delete the database (files included), for example, if the installation for some reason fails. In my particular case, the error must happen when the user wants to install the application in a folder where there is already the app installed. Because the HSQLDB is already there with the same name and the credentials are changed, the error happens at schema creation time when i try to connect to the db with the default credentials.

After i catch this error, the problem i´m having is that when i try to delete the directory where the database is, i get an error because the .log file is locked (and it must be from my running app that is holding that lock, but i can´t figure why).

Here is the relevant code that gets executed before the authorization error:

private static void createDB(String dbName, String dbPath) {

    Server server = new Server();
    server.setDatabaseName(0, dbName);
    server.setDatabasePath(0, "file:" + dbPath);
    server.setSilent(true);
    server.setRestartOnShutdown(false);
    server.signalCloseAllServerConnections();
    server.setNoSystemExit(true);

    server.start();
    server.stop();
    server.shutdown();
}

private static void createSchema() throws SQLException, FileNotFoundException, IOException {

    Connection conn = null;

    try {   
        conn = DBManager.getConnection(); <---- AUTH ERROR HAPPENS HERE!
        ScriptRunner runner = new ScriptRunner(conn, true, true);

        runner.runScript(new BufferedReader(new FileReader("someScript.sql")));
        runner.runScript(new BufferedReader(new FileReader("someScript.sql")));

    }  finally {
        DBUtils.closeQuietly(conn);
    }

}

After i catch the auth exception, i try to delete the hsqldb folder by doing:

 File directoryFile = new File(dirPathDB);
 org.apache.commons.io.FileUtils.deleteQuietly(directoryFile);

But i get an error saying "unable to delete file somepath/somelogfile.log" I dont know why this happens as i´m closing the connection in the finally block, no matter of the auth error...

Thank you and excuse my english!

Upvotes: 1

Views: 1418

Answers (1)

fredt
fredt

Reputation: 24352

You cannot delete the database because it is open at this point.

When there is an existing installation, and you connect (even with the wrong credentials), the database is opened first in order to check the username and password. If the check fails, the database is not closed as normally another attempt is made to connect with the correct credentials.

If a new installation is not complete, you should simply execute the SQL statement "SHUTDOWN" with an account that has admin privileges. This allows you to delete the database.

Upvotes: 3

Related Questions