KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > ConcurrentImplicitCreateSchema


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang. ConcurrentImplicitCreateSchema.java
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.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Statement JavaDoc;
27
28 import org.apache.derby.tools.ij;
29
30 /**
31  * Test for several threads creating tables in the same originally
32  * non-existing schema. This will cause an implicit creation of the
33  * schema. The test was created for the fix of JIRA issue DERBY-230
34  * where an error occurred if two threads try to create the schema in
35  * parallel.
36  *
37  * @author oystein.grovlen@sun.com
38  */

39
40 public class ConcurrentImplicitCreateSchema
41 {
42     
43     /**
44      * Runnable that will create and drop table.
45      */

46     class CreateTable implements Runnable JavaDoc
47     {
48         /** Id of the thread running this instance */
49         private int myId;
50         /** Which schema to use */
51         private int schemaId;
52         /** Database connection for this thread */
53         private Connection JavaDoc conn = null;
54         /** Test object. Used to inform about failures. */
55         private ConcurrentImplicitCreateSchema test;
56
57         CreateTable(int id, int schemaId,
58                     ConcurrentImplicitCreateSchema test)
59             throws SQLException JavaDoc, IllegalAccessException JavaDoc,
60                    ClassNotFoundException JavaDoc, InstantiationException JavaDoc
61         {
62             this.myId = id;
63             this.schemaId = schemaId;
64             this.test = test;
65
66             // Open a connection for this thread
67
conn = ij.startJBMS();
68             conn.setAutoCommit(false);
69         }
70      
71         public void run()
72         {
73             try {
74                 Statement JavaDoc s = conn.createStatement();
75                 s.execute("create table testschema" + schemaId + ".testtab"
76                           + myId + "(num int, addr varchar(40))");
77                 s.execute("drop table testschema" + schemaId + ".testtab"
78                           + myId);
79             } catch (SQLException JavaDoc e) {
80                 test.failed(e);
81             }
82   
83             // We should close the transaction regardless of outcome
84
// Otherwise, other threads may wait on transactional
85
// locks until this transaction times out.
86
try {
87                 conn.commit();
88                 conn.close();
89             } catch (SQLException JavaDoc e) {
90                 test.failed(e);
91             }
92             // System.out.println("Thread " + myId + " completed.");
93
}
94     }
95     
96     /**
97      * Outcome of test.
98      * Will be set to false if any failure is detected.
99      */

100     boolean passed = true;
101
102     public static void main(String JavaDoc[] args)
103     {
104         new ConcurrentImplicitCreateSchema().go(args);
105     }
106
107     void go(String JavaDoc[] args)
108     {
109         System.out.println("Test ConcurrentImplicitCreateSchema starting");
110
111         try {
112             // Load the JDBC Driver class
113
// use the ij utility to read the property file and
114
// make the initial connection.
115
ij.getPropertyArg(args);
116             Connection JavaDoc conn = ij.startJBMS();
117
118             conn.setAutoCommit(true);
119
120             // Drop the schemas we will be using in case they exist.
121
// This will ensure that they are implicitly created by this test
122
Statement JavaDoc s = conn.createStatement();
123
124             // Number of schemas to use. An equal number of threads
125
// will operate on each schema.
126
final int NSCHEMAS = 1;
127
128             for (int i=0; i < NSCHEMAS; ++i) {
129                 try {
130                     s.execute("drop schema testschema" + i + " restrict");
131                 } catch (SQLException JavaDoc e) {
132                     if (e.getSQLState().equals("42Y07")) {
133                         // IGNORE. Schema did not exist. That is our target.
134
} else {
135                         throw e;
136                     }
137                 }
138             }
139
140             // Number of threads to run.
141
final int NTHREADS = 100;
142
143             // Create threads
144
Thread JavaDoc[] threads = new Thread JavaDoc[NTHREADS];
145             for (int i=0; i<NTHREADS; ++i) {
146                 threads[i]
147                     = new Thread JavaDoc(new CreateTable(i, i%NSCHEMAS, this));
148             }
149             
150             // Start threads
151
for (int i=0; i<NTHREADS; ++i) {
152                 threads[i].start();
153             }
154             
155             // Wait for threads to complete
156
for (int i=0; i<NTHREADS; ++i) {
157                 threads[i].join();
158             }
159   
160             conn.close();
161             System.out.println("Closed connection");
162         } catch (Throwable JavaDoc e) {
163             System.out.println("exception thrown:");
164             failed(e);
165         }
166
167         System.out.print("Test ConcurrentImplicitCreateSchema ");
168         if (passed) {
169             System.out.println("PASSED");
170         } else {
171             System.out.println("FAILED");
172     }
173     }
174
175     void failed(Throwable JavaDoc e)
176     {
177         if (e instanceof SQLException JavaDoc) {
178             printSQLError((SQLException JavaDoc) e);
179         } else {
180             e.printStackTrace();
181         }
182         passed = false;
183     }
184
185     void printSQLError(SQLException JavaDoc e)
186     {
187         while (e != null)
188         {
189             System.out.println(e.toString());
190             e.printStackTrace();
191             e = e.getNextException();
192         }
193     }
194 }
195
Popular Tags