001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.harness.procedure
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.storetests;
023:
024: import org.apache.derby.iapi.services.sanity.SanityManager;
025:
026: import org.apache.derbyTesting.functionTests.tests.store.BaseTest;
027:
028: import java.sql.CallableStatement;
029: import java.sql.Connection;
030: import java.sql.PreparedStatement;
031: import java.sql.ResultSet;
032: import java.sql.SQLException;
033: import java.sql.Statement;
034:
035: import org.apache.derby.tools.ij;
036:
037: /**
038:
039: The purpose of this test is to reproduce JIRA DERBY-715:
040:
041: Sometimes a deadlock would be incorrectly reported as a deadlock. The
042: bug seemed to always reproduce at least once if the following test
043: was run (at least one of the iterations in the loop would get an
044: incorrect timeout vs. a deadlock).
045:
046: **/
047:
048: public class st_derby715 extends BaseTest {
049: static boolean verbose = false;
050:
051: public st_derby715() {
052: }
053:
054: /**
055: * Create the base table that the 2 threads will use.
056: **/
057: private static void setup() throws Exception {
058: Connection conn = ij.startJBMS();
059: Statement stmt = conn.createStatement();
060:
061: // drop table, ignore table does not exist error.
062:
063: try {
064: stmt.executeUpdate("drop table a");
065: } catch (Exception e) {
066: // ignore drop table errors.
067: }
068:
069: try {
070: stmt.executeUpdate("drop table b");
071: } catch (Exception e) {
072: // ignore drop table errors.
073: }
074:
075: stmt.executeUpdate("create table a (a integer)");
076: stmt.executeUpdate("create table b (b integer)");
077: stmt.close();
078: conn.commit();
079: conn.close();
080: }
081:
082: public static class t1 implements Runnable {
083: String[] argv;
084:
085: public t1(String[] argv) {
086: argv = argv;
087: }
088:
089: public void run() {
090: try {
091: ij.getPropertyArg(argv);
092: Connection conn = ij.startJBMS();
093: conn.setAutoCommit(false);
094: conn
095: .setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
096:
097: Statement stmt = conn.createStatement();
098: if (verbose)
099: System.out
100: .println("Thread 1 before selecting from b");
101:
102: // get row locks on all rows in b
103: ResultSet rs = stmt.executeQuery("select * from b");
104:
105: if (verbose)
106: System.out
107: .println("Thread 1 before selecting next from b");
108:
109: while (rs.next()) {
110: if (verbose)
111: System.out.println("Thread t1 got "
112: + rs.getString(1));
113: }
114: if (verbose)
115: System.out.println("Thread 1 after all next.");
116:
117: // give thread 2 a chance to catch up.
118: Thread.sleep(500);
119:
120: if (verbose)
121: System.out
122: .println("Thread 1 before inserting into a...");
123:
124: // now wait on lock inserting row into table a - either
125: // thread 1 or thread 2 should get a deadlock, NOT a timeout.
126: stmt.executeUpdate("insert into a values(1)");
127:
128: if (verbose)
129: System.out
130: .println("Thread 1 after inserting into a...");
131:
132: conn.rollback();
133: } catch (SQLException sqle) {
134: if (sqle.getSQLState().equals("40001")) {
135: // only expected exception is a deadlock, we should
136: // get at least one deadlock, so print it to output.
137: // Don't know which thread will get the deadlock, so
138: // don't label it.
139: System.out.println("Got a Deadlock.");
140: } else {
141: org.apache.derby.tools.JDBCDisplayUtil
142: .ShowSQLException(System.out, sqle);
143: sqle.printStackTrace(System.out);
144: }
145: if (verbose)
146: System.out.println("Thread 1 got exception:\n");
147: } catch (Exception ex) {
148: System.out.println("got unexpected exception: " + ex);
149: }
150: }
151: }
152:
153: public static class t2 implements Runnable {
154: String[] argv;
155:
156: public t2(String[] argv) {
157: argv = argv;
158: }
159:
160: public void run() {
161: try {
162: ij.getPropertyArg(argv);
163: Connection conn = ij.startJBMS();
164: conn.setAutoCommit(false);
165: conn
166: .setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
167:
168: Statement stmt = conn.createStatement();
169:
170: if (verbose)
171: System.out
172: .println("Thread 2 before selecting from a");
173:
174: ResultSet rs = stmt.executeQuery("select * from a");
175:
176: if (verbose)
177: System.out
178: .println("Thread 2 before selecting next from a");
179:
180: while (rs.next()) {
181: if (verbose)
182: System.out.println("Thread t2 got "
183: + rs.getString(1));
184: }
185:
186: if (verbose)
187: System.out.println("Thread 2 after all next.");
188:
189: Thread.sleep(500);
190:
191: if (verbose)
192: System.out
193: .println("Thread 2 before inserting into b");
194:
195: stmt.executeUpdate("insert into b values(2)");
196:
197: if (verbose)
198: System.out
199: .println("Thread 2 after inserting into b");
200:
201: conn.rollback();
202: } catch (SQLException sqle) {
203: if (verbose)
204: System.out.println("Thread 1 got exception:\n");
205:
206: if (sqle.getSQLState().equals("40001")) {
207: // only expected exception is a deadlock, we should
208: // get at least one deadlock, so print it to output.
209: // Don't know which thread will get the deadlock, so
210: // don't label it.
211: System.out.println("Got a Deadlock.");
212: } else {
213: org.apache.derby.tools.JDBCDisplayUtil
214: .ShowSQLException(System.out, sqle);
215: sqle.printStackTrace(System.out);
216: }
217: } catch (Exception ex) {
218: System.out.println("got unexpected exception: " + ex);
219: }
220: }
221: }
222:
223: public void testList(Connection conn) throws SQLException {
224: }
225:
226: public static void main(String[] argv) throws Throwable {
227: ij.getPropertyArg(argv);
228:
229: st_derby715 setup_ddl = new st_derby715();
230: setup_ddl.setup();
231: setup_ddl = null;
232:
233: {
234: for (int i = 0; i < 5; i++) {
235: Thread test1 = new Thread(new t1(argv));
236: Thread test2 = new Thread(new t2(argv));
237: test1.start();
238: test2.start();
239: test1.join();
240: test2.join();
241: }
242: }
243: /*
244: catch (SQLException sqle)
245: {
246: org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(
247: System.out, sqle);
248: sqle.printStackTrace(System.out);
249: }
250: */
251: }
252: }
|