001/* Commit outstanding transactions in HSQL v1 databases
002 * 
003 * Copyright (c) 2015 The Regents of the University of California.
004 * All rights reserved.
005 *
006 * '$Author: crawl $'
007 * '$Date: 2015-01-26 21:47:32 +0000 (Mon, 26 Jan 2015) $' 
008 * '$Revision: 33213 $'
009 * 
010 * Permission is hereby granted, without written agreement and without
011 * license or royalty fees, to use, copy, modify, and distribute this
012 * software and its documentation for any purpose, provided that the above
013 * copyright notice and the following two paragraphs appear in all copies
014 * of this software.
015 *
016 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
017 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
018 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
019 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
020 * SUCH DAMAGE.
021 *
022 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
023 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
025 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
026 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
027 * ENHANCEMENTS, OR MODIFICATIONS.
028 *
029 */
030package org.kepler.util.sql;
031
032import java.io.IOException;
033import java.sql.Connection;
034import java.sql.DriverManager;
035import java.sql.SQLException;
036import java.sql.Statement;
037
038/** This class is used to commit outstanding transactions in HSQL v1 databases.
039 *
040 *  @author Daniel Crawl
041 *  @version $Id: CleanupHSQL1.java 33213 2015-01-26 21:47:32Z crawl $
042 * 
043 */
044public class CleanupHSQL1
045{
046    public static void main(String argv[])
047    {
048        try {
049            Class.forName("org.hsqldb.jdbcDriver");
050        } catch(ClassNotFoundException e) { 
051            System.out.println("HSQL JDBC driver not found: " + e.getMessage());
052            System.exit(-1);
053        }
054        
055        if(argv.length != 2) {
056            System.out.println("ERROR: must provide database and user name as command line arguments.");
057            System.exit(-1);
058        }
059        
060        String databaseName = argv[0];
061        String user = argv[1];
062        
063        String passwd = null;
064        try {
065            passwd = HSQLUtils.getAuthFilePassword(databaseName);
066        } catch (IOException e) {
067            System.out.println("ERROR reading auth file for " + databaseName + ": " + e.getMessage());
068            System.exit(-1);
069        }
070
071        System.out.println("Calling SHUTDOWN for " + databaseName);
072
073        // open the database and call SHUTDOWN to commit outstanding transactions.
074        
075        try(Connection connection = DriverManager.getConnection("jdbc:hsqldb:" + databaseName, user, passwd);
076            Statement statement = connection.createStatement();) {
077            statement.execute("SHUTDOWN");
078            connection.commit();
079        } catch(SQLException e) {
080            System.out.println("Error calling SHUTDOWN: " + e.getMessage());
081            System.exit(-1);
082        }
083    }
084}