001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.store.bootLock
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.store;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.ResultSetMetaData;
027: import java.sql.ResultSet;
028: import java.sql.Statement;
029: import java.sql.SQLException;
030: import java.sql.Types;
031: import java.io.File;
032: import java.io.OutputStream;
033: import java.io.RandomAccessFile;
034: import java.io.IOException;
035: import java.nio.channels.FileChannel;
036: import java.nio.channels.FileLock;
037:
038: import org.apache.derby.tools.ij;
039: import org.apache.derby.tools.JDBCDisplayUtil;
040: import java.util.Properties;
041: import org.apache.derbyTesting.functionTests.util.TestUtil;
042:
043: /**
044: *Testing for FileLocks that prevent Derby Double Boot.
045: * @author suresht
046: */
047:
048: public class bootLock {
049: public static void main(String[] args) {
050: Connection con;
051: Statement stmt;
052:
053: try {
054:
055: System.out.println("Test BootLock Starting");
056: // use the ij utility to read the property file and
057: // make the initial connection.
058: ij.getPropertyArg(args);
059: String derbyHome = System.getProperty("derby.system.home");
060: con = ij.startJBMS();
061:
062: stmt = con.createStatement();
063: stmt.execute("create table t1 (a int)");
064: stmt.close();
065: con.close();
066: try {
067: TestUtil.shutdownUsingDataSource("wombat");
068: } catch (Exception e) {
069: //Shutdown will throw exception , just ignore it.
070: }
071:
072: //Invoke anothe jvm that makes a connection to database wombat
073:
074: String cmd = "java org.apache.derbyTesting.functionTests.tests.store.bootLock1";
075: Runtime rtime = Runtime.getRuntime();
076: Process p1 = rtime.exec(cmd, (String[]) null, new File(
077: derbyHome));
078:
079: //sleep for some with the hope that other jvm has made the
080: //connection.
081:
082: Thread.sleep(30000);
083:
084: //Now if we try to boot , we should get an multiple
085: //instance exception
086: try {
087: Properties prop = new Properties();
088: prop.setProperty("databaseName", "wombat");
089: TestUtil.getDataSourceConnection(prop);
090: } catch (SQLException e) {
091: System.out.println("expected exception");
092: dumpSQLExceptions(e);
093: }
094:
095: //kill the sub process
096: p1.destroy();
097:
098: } catch (SQLException e) {
099: System.out.println("FAIL -- unexpected exception");
100: dumpSQLExceptions(e);
101: e.printStackTrace();
102: } catch (Throwable e) {
103: System.out.println("FAIL -- unexpected exception: " + e);
104: e.printStackTrace();
105: }
106:
107: System.out.println("Test BootLock finished");
108: }
109:
110: static private void dumpSQLExceptions(SQLException se) {
111: while (se != null) {
112: System.out.println("SQLSTATE(" + se.getSQLState() + "): ");
113: se = se.getNextException();
114: }
115: }
116: }
|