1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.Statement ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.ResultSetMetaData ; 30 import java.sql.SQLException ; 31 import java.sql.SQLWarning ; 32 33 import org.apache.derby.tools.ij; 34 import org.apache.derby.tools.JDBCDisplayUtil; 35 36 46 47 public class deadlockMode { 48 49 private static final int THREAD_COUNT = 20; 50 private static boolean passed = false; 51 private Object syncObject = new Object (); 52 private int doneCount; 53 private deadlockMode() {} 54 55 public static void main(String [] args) { 56 System.out.println("Test deadlockMode starting"); 57 58 deadlockMode tester = new deadlockMode(); 59 60 try { 61 ij.getPropertyArg(args); 64 Connection conn = ij.startJBMS(); 65 66 setup(conn, true); 67 passed = true; 68 tester.runtest(); 69 teardown(conn); 70 conn.close(); 71 72 73 } catch (Throwable e) { 74 System.out.println("FAIL: exception thrown:"); 75 passed = false; 76 JDBCDisplayUtil.ShowException(System.out,e); 77 e.printStackTrace(); 78 } 79 80 if (passed) 81 System.out.println("PASS"); 82 System.out.println("Test cursor finished"); 83 } 84 88 private void runtest() throws InterruptedException 89 { 90 Thread [] t = new Thread [THREAD_COUNT]; 91 for (int i = 0; i < THREAD_COUNT; i++) 92 { 93 t[i] = new Thread (new Runnable () { 94 public void run() {startnew(); }}); 95 t[i].start(); 96 } 97 boolean notdone = true; 98 while(notdone) 99 { 100 synchronized(syncObject) 101 { 102 if (doneCount == THREAD_COUNT) 103 notdone = false; 104 else 105 syncObject.wait(); 106 } 107 } 108 } 109 113 private void done() 114 { 115 System.out.println("Done Thread"); 116 synchronized(syncObject) 117 { 118 doneCount++; 119 syncObject.notify(); 120 } 121 } 122 129 private void startnew() 130 { 131 Connection conn = null; 132 try { 133 conn = ij.startJBMS(); 135 System.out.println("Starting thread"); 136 137 Statement stmt = conn.createStatement(); 138 stmt.executeUpdate("update t set i = 456 where i = 456"); 140 conn.setAutoCommit(false); 142 stmt.execute("set isolation serializable"); 143 for (int i = 0; i < 100 ; i++) 144 { 145 stmt.executeUpdate("update t set i = 456 where i = 456"); 146 conn.commit(); 147 } 148 done(); 149 150 } catch (Throwable e) { 151 synchronized(syncObject) 152 { 153 System.out.println("FAIL: exception thrown:"); 154 passed = false; 155 JDBCDisplayUtil.ShowException(System.out,e); 156 e.printStackTrace(); 157 done(); 158 } 159 } 160 finally { 161 try { 162 if (conn != null) { 163 conn.rollback(); 164 conn.close(); 165 } 166 } catch (SQLException sqle) { 167 System.out.println("FAIL: exception thrown:"); 168 passed = false; 169 JDBCDisplayUtil.ShowException(System.out,sqle); 170 sqle.printStackTrace(); 171 } 172 } 173 } 174 177 static void setup(Connection conn, boolean first) throws SQLException { 178 Statement stmt = conn.createStatement(); 179 180 if (first) { 181 verifyCount( 182 stmt.executeUpdate("create table t (i int)"), 183 0); 184 185 } else { 186 verifyBoolean( 187 stmt.execute("delete from t"), 188 false); 189 } 190 191 verifyCount( 192 stmt.executeUpdate("insert into t values (1956)"), 193 1); 194 195 verifyCount( 196 stmt.executeUpdate("insert into t values (456)"), 197 1); 198 199 verifyCount( 200 stmt.executeUpdate("insert into t values (180)"), 201 1); 202 203 verifyCount( 204 stmt.executeUpdate("insert into t values (3)"), 205 1); 206 207 stmt.close(); 208 209 System.out.println("PASS: setup complete"); 210 } 211 212 215 static void teardown(Connection conn) throws SQLException { 216 Statement stmt = conn.createStatement(); 217 218 verifyCount( 219 stmt.executeUpdate("drop table t"), 220 0); 221 222 stmt.close(); 223 224 System.out.println("PASS: teardown complete"); 225 } 226 229 static void verifyCount(int count, int expect) throws SQLException { 230 if (count!=expect) { 231 System.out.println("FAIL: Expected "+expect+" got "+count+" rows"); 232 throw new SQLException ("Wrong number of rows returned"); 233 } 234 else 235 System.out.println("PASS: expected and got "+count+ 236 (count == 1? " row":" rows")); 237 } 238 239 242 static void verifyBoolean(boolean got, boolean expect) throws SQLException { 243 if (got!=expect) { 244 System.out.println("FAIL: Expected "+expect+" got "+got); 245 throw new SQLException ("Wrong boolean returned"); 246 } 247 else 248 System.out.println("PASS: expected and got "+got); 249 } 250 } 251 | Popular Tags |