Thursday, March 29, 2007

HSQLDB Database Locking and Shutdown

I have had a number of questions on how to shutdown multiple HSQLDB databases running on one JVM. I have written an example application which will shutdown all of the databases attached to a single database engine in a JVM. The application uses only one test database, but the principal applies to multiple databases.

The software is released under the Apache 2.0 license. The project was developed in Netbeans 5.5

Here is the software: HSQLDBLocking.zip

Here is the source code if you prefer to see it instead of downloading the project:

/*
* TestHSQLDB.java
*
* Created on February 9, 2007, 10:04 PM
*
* $Id: TestHSQLDB.java 12 2007-03-29 21:19:48Z jyeary $
*
*/
/*
* Copyright (C) 2007 Blue Lotus Holdings, LLC. All Rights Reserved.
* Copyright (C) 2007 Blue Lotus Software. All Rights Reserved.
* Copyright (C) 2007 White Lotus Software. All Rights Reserved.
* Copyright (C) 2007 John Yeary. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED "AS IS," WITHOUT A WARRANTY OF ANY KIND.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. BLUE LOTUS HOLDINGS, LLC
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
* SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
* OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
* BLUE LOTUS HOLDINGS, LLC OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
* PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE
* THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
* EVEN IF BLUE LOTUS HOLDINGS, LLC HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/

/*
* Copyright 2007 John Yeary
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bluelotussoftware.db.hsqldb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.Vector;
import org.hsqldb.DatabaseManager;

/**
*
* @author John Yeary
* @version $Revision: 12 $
*/
public class TestHSQLDB {

/** Creates a new instance of TestHSQLDB */
public TestHSQLDB() {
}

/**
* @param args the command line arguments
*/

public static void main(String args[]) throws Exception {

// Use the class loader to load the JDBC driver
Class.forName("org.hsqldb.jdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:hsqldb:file:test", "sa", "");

// Fetch all of the known database URIs and display them
Vector v = DatabaseManager.getDatabaseURIs();

for (Object o : v) {
System.out.println(((String) o));
}

ResultSet r = connection.createStatement().executeQuery("SELECT * FROM INFORMATION_SCHEMA.SYSTEM_USERS");
r.next();
System.out.println(r.getString(1));

// Shutdown the database normally (0), or shutdown and script out using (2)
DatabaseManager.closeDatabases(0);

connection.close();
}
}

0 comments :

Popular Posts