KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > storetests > st_derby715


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.procedure
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.storetests;
23
24
25 import org.apache.derby.iapi.services.sanity.SanityManager;
26
27 import org.apache.derbyTesting.functionTests.tests.store.BaseTest;
28
29 import java.sql.CallableStatement JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import java.sql.Statement JavaDoc;
35
36 import org.apache.derby.tools.ij;
37
38
39 /**
40
41 The purpose of this test is to reproduce JIRA DERBY-715:
42
43 Sometimes a deadlock would be incorrectly reported as a deadlock. The
44 bug seemed to always reproduce at least once if the following test
45 was run (at least one of the iterations in the loop would get an
46 incorrect timeout vs. a deadlock).
47
48 **/

49
50 public class st_derby715 extends BaseTest
51 {
52     static boolean verbose = false;
53
54     public st_derby715()
55     {
56     }
57
58
59     /**
60      * Create the base table that the 2 threads will use.
61      **/

62     private static void setup()
63         throws Exception JavaDoc
64     {
65         Connection JavaDoc conn = ij.startJBMS();
66         Statement JavaDoc stmt = conn.createStatement();
67
68         // drop table, ignore table does not exist error.
69

70         try
71         {
72             stmt.executeUpdate("drop table a");
73         }
74         catch (Exception JavaDoc e)
75         {
76             // ignore drop table errors.
77
}
78
79         try
80         {
81             stmt.executeUpdate("drop table b");
82         }
83         catch (Exception JavaDoc e)
84         {
85             // ignore drop table errors.
86
}
87
88         stmt.executeUpdate("create table a (a integer)");
89         stmt.executeUpdate("create table b (b integer)");
90         stmt.close();
91         conn.commit();
92         conn.close();
93     }
94
95     public static class t1 implements Runnable JavaDoc
96     {
97         String JavaDoc[] argv;
98
99         public t1(String JavaDoc[] argv)
100         {
101             argv = argv;
102         }
103         public void run()
104         {
105             try
106             {
107                 ij.getPropertyArg(argv);
108                 Connection JavaDoc conn = ij.startJBMS();
109                 conn.setAutoCommit(false);
110                 conn.setTransactionIsolation(
111                         Connection.TRANSACTION_SERIALIZABLE);
112
113                 Statement JavaDoc stmt = conn.createStatement();
114                 if (verbose)
115                     System.out.println("Thread 1 before selecting from b");
116
117                 // get row locks on all rows in b
118
ResultSet JavaDoc rs = stmt.executeQuery("select * from b");
119
120                 if (verbose)
121                     System.out.println("Thread 1 before selecting next from b");
122
123                 while (rs.next())
124                 {
125                     if (verbose)
126                         System.out.println("Thread t1 got " + rs.getString(1));
127                 }
128                 if (verbose)
129                     System.out.println("Thread 1 after all next.");
130
131                 // give thread 2 a chance to catch up.
132
Thread.sleep(500);
133
134                 if (verbose)
135                     System.out.println("Thread 1 before inserting into a...");
136
137                 // now wait on lock inserting row into table a - either
138
// thread 1 or thread 2 should get a deadlock, NOT a timeout.
139
stmt.executeUpdate("insert into a values(1)");
140
141                 if (verbose)
142                     System.out.println("Thread 1 after inserting into a...");
143
144                 conn.rollback();
145             }
146             catch (SQLException JavaDoc sqle)
147             {
148                 if (sqle.getSQLState().equals("40001"))
149                 {
150                     // only expected exception is a deadlock, we should
151
// get at least one deadlock, so print it to output.
152
// Don't know which thread will get the deadlock, so
153
// don't label it.
154
System.out.println("Got a Deadlock.");
155                 }
156                 else
157                 {
158                     org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(
159                         System.out, sqle);
160                     sqle.printStackTrace(System.out);
161                 }
162                 if (verbose)
163                     System.out.println("Thread 1 got exception:\n");
164             }
165             catch (Exception JavaDoc ex)
166             {
167                 System.out.println("got unexpected exception: " + ex);
168             }
169         }
170     }
171
172     public static class t2 implements Runnable JavaDoc
173     {
174         String JavaDoc[] argv;
175         public t2 (String JavaDoc[] argv)
176         {
177             argv = argv;
178         }
179         public void run()
180         {
181             try
182             {
183                 ij.getPropertyArg(argv);
184                 Connection JavaDoc conn = ij.startJBMS();
185                 conn.setAutoCommit(false);
186                 conn.setTransactionIsolation(
187                         Connection.TRANSACTION_SERIALIZABLE);
188
189                 Statement JavaDoc stmt = conn.createStatement();
190
191                 if (verbose)
192                     System.out.println("Thread 2 before selecting from a");
193
194                 ResultSet JavaDoc rs = stmt.executeQuery("select * from a");
195
196                 if (verbose)
197                     System.out.println("Thread 2 before selecting next from a");
198
199                 while (rs.next())
200                 {
201                     if (verbose)
202                         System.out.println("Thread t2 got " + rs.getString(1));
203                 }
204
205                 if (verbose)
206                     System.out.println("Thread 2 after all next.");
207
208
209                 Thread.sleep(500);
210                 
211                 if (verbose)
212                     System.out.println("Thread 2 before inserting into b");
213
214                 stmt.executeUpdate("insert into b values(2)");
215
216                 if (verbose)
217                     System.out.println("Thread 2 after inserting into b");
218
219                 conn.rollback();
220             }
221             catch (SQLException JavaDoc sqle)
222             {
223                 if (verbose)
224                     System.out.println("Thread 1 got exception:\n");
225
226                 if (sqle.getSQLState().equals("40001"))
227                 {
228                     // only expected exception is a deadlock, we should
229
// get at least one deadlock, so print it to output.
230
// Don't know which thread will get the deadlock, so
231
// don't label it.
232
System.out.println("Got a Deadlock.");
233                 }
234                 else
235                 {
236                     org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(
237                         System.out, sqle);
238                     sqle.printStackTrace(System.out);
239                 }
240             }
241             catch (Exception JavaDoc ex)
242             {
243                 System.out.println("got unexpected exception: " + ex);
244             }
245         }
246     }
247     
248
249     public void testList(Connection JavaDoc conn)
250         throws SQLException JavaDoc
251     {
252     }
253
254     public static void main(String JavaDoc[] argv)
255         throws Throwable JavaDoc
256     {
257         ij.getPropertyArg(argv);
258
259         st_derby715 setup_ddl = new st_derby715();
260         setup_ddl.setup();
261         setup_ddl = null;
262
263         {
264             for (int i = 0; i < 5; i++)
265             {
266                 Thread JavaDoc test1 = new Thread JavaDoc(new t1(argv));
267                 Thread JavaDoc test2 = new Thread JavaDoc(new t2(argv));
268                 test1.start();
269                 test2.start();
270                 test1.join();
271                 test2.join();
272             }
273         }
274         /*
275         catch (SQLException sqle)
276         {
277             org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(
278                 System.out, sqle);
279             sqle.printStackTrace(System.out);
280         }
281         */

282     }
283 }
284
Popular Tags