KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > store > bootLock


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.store.bootLock
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.store;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Types JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.RandomAccessFile JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.nio.channels.FileChannel JavaDoc;
36 import java.nio.channels.FileLock JavaDoc;
37
38
39 import org.apache.derby.tools.ij;
40 import org.apache.derby.tools.JDBCDisplayUtil;
41 import java.util.Properties JavaDoc;
42 import org.apache.derbyTesting.functionTests.util.TestUtil;
43
44 /**
45  *Testing for FileLocks that prevent Derby Double Boot.
46  * @author suresht
47  */

48
49 public class bootLock {
50     public static void main(String JavaDoc[] args) {
51         Connection JavaDoc con;
52         Statement JavaDoc stmt;
53
54         try
55         {
56
57             System.out.println("Test BootLock Starting");
58             // use the ij utility to read the property file and
59
// make the initial connection.
60
ij.getPropertyArg(args);
61             String JavaDoc derbyHome = System.getProperty("derby.system.home");
62             con = ij.startJBMS();
63
64             stmt = con.createStatement();
65             stmt.execute("create table t1 (a int)");
66             stmt.close();
67             con.close();
68             try{
69                 TestUtil.shutdownUsingDataSource("wombat");
70             }catch(Exception JavaDoc e)
71             {
72                 //Shutdown will throw exception , just ignore it.
73
}
74
75             //Invoke anothe jvm that makes a connection to database wombat
76

77             String JavaDoc cmd = "java org.apache.derbyTesting.functionTests.tests.store.bootLock1";
78             Runtime JavaDoc rtime = Runtime.getRuntime();
79             Process JavaDoc p1 = rtime.exec(cmd, (String JavaDoc[])null, new File JavaDoc(derbyHome));
80             
81             //sleep for some with the hope that other jvm has made the
82
//connection.
83

84             Thread.sleep(30000);
85
86             //Now if we try to boot , we should get an multiple
87
//instance exception
88
try{
89                 Properties JavaDoc prop = new Properties JavaDoc();
90                 prop.setProperty("databaseName", "wombat");
91                 TestUtil.getDataSourceConnection(prop);
92             }catch(SQLException JavaDoc e) {
93                 System.out.println("expected exception");
94                 dumpSQLExceptions(e);
95             }
96
97             //kill the sub process
98
p1.destroy();
99
100         }
101         catch (SQLException JavaDoc e) {
102             System.out.println("FAIL -- unexpected exception");
103             dumpSQLExceptions(e);
104             e.printStackTrace();
105         }
106         catch (Throwable JavaDoc e) {
107             System.out.println("FAIL -- unexpected exception: "+e);
108             e.printStackTrace();
109         }
110
111         System.out.println("Test BootLock finished");
112     }
113
114     static private void dumpSQLExceptions (SQLException JavaDoc se) {
115         while (se != null) {
116             System.out.println("SQLSTATE("+se.getSQLState()+"): ");
117             se = se.getNextException();
118         }
119     }
120 }
121
122
123
124
Popular Tags