1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 import java.sql.Connection ; 25 import java.sql.Statement ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.ResultSetMetaData ; 29 import java.sql.DatabaseMetaData ; 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 import org.apache.derbyTesting.functionTests.util.TestUtil; 36 37 48 public class closed implements Runnable { 49 50 private static boolean jsr169_test = false; 51 52 public static void main(String [] args) { 53 System.out.println("Test closed starting"); 54 boolean passed = true; 55 56 try { 57 Connection conn; 58 59 ij.getPropertyArg(args); 62 conn = ij.startJBMS(); 63 64 passed = testDerby62(conn) && passed; 65 66 passed = testStatement(conn) && passed; 68 69 passed = testPreparedStatement(conn) && passed; 70 71 passed = testResultSet(conn) && passed; 72 73 passed = testConnection(conn) && passed; 76 77 if (!conn.isClosed()) { 78 passed = false; 79 System.out.println("FAIL -- connection not closed by test"); 80 conn.close(); 81 } 82 83 System.out.println("Test database shutdown ..."); 85 passed = shutdownTest("wombat", "shutdown=true"); 86 87 System.out.println("Test system shutdown ..."); 89 passed = shutdownTest("", "shutdown=true"); 90 91 92 93 } catch (Throwable e) { 94 passed = false; 95 System.out.println("FAIL -- unexpected exception:"); 96 JDBCDisplayUtil.ShowException(System.out, e); 97 } 98 99 if (passed) 100 System.out.println("PASS"); 101 System.out.println("Test closed finished"); 102 } 103 104 static boolean shutdownTest(String databaseName, String shutdownString) throws SQLException { 105 107 boolean passed = true; 108 109 Connection c1 = TestUtil.getConnection("wombat",null); 110 Connection c2 = TestUtil.getConnection("wombat",null); 111 Connection c3a = TestUtil.getConnection("wombat",null); 112 Connection c3b = TestUtil.getConnection("wombat",null); 113 114 try { 115 c3a.createStatement().execute("DROP TABLE CLOSED.LOCKME"); 116 } catch (SQLException sqle) { 117 } 118 try { 119 c3a.createStatement().execute("DROP PROCEDURE SLEEP"); 120 } catch (SQLException sqle) { 121 } 122 123 c3a.createStatement().execute("CREATE TABLE CLOSED.LOCKME(i int)"); 124 125 c3a.createStatement().execute("create procedure sleep(t INTEGER) dynamic result sets 0 language java external name 'java.lang.Thread.sleep' parameter style java"); 126 c3a.setAutoCommit(false); 127 c3a.createStatement().execute("LOCK TABLE CLOSED.LOCKME IN SHARE MODE"); 128 129 closed r2 = new closed(c2, "CALL sleep(10000)"); 130 closed r3 = new closed(c3b, "LOCK TABLE CLOSED.LOCKME IN EXCLUSIVE MODE"); 131 132 Thread t2 = new Thread (r2); 133 t2.start(); 134 Thread t3 = new Thread (r3); 135 t3.start(); 136 137 try { 138 Thread.currentThread().sleep(2000); 139 } catch (InterruptedException ie) { 140 System.out.println(ie); 141 } 142 143 SQLException s = null; 144 try { 145 TestUtil.getConnection(databaseName, shutdownString); 146 } catch (SQLException sqle) { 147 s = sqle; 148 } 149 150 try { 151 t2.join(); 152 } catch (InterruptedException ie) { 153 System.out.println(ie); 154 } 155 try { 156 t3.join(); 157 } catch (InterruptedException ie) { 158 System.out.println(ie); 159 } 160 161 System.out.println(r2.result); 162 System.out.println(r3.result); 163 164 if (s != null) 165 JDBCDisplayUtil.ShowException(System.out, s); 166 167 if (!c1.isClosed()) { 168 passed = false; 169 System.out.println("FAIL -- connection not shutdown " + databaseName + ";" + shutdownString); 170 c1.close(); 171 } 172 if (!c2.isClosed()) { 173 passed = false; 174 System.out.println("FAIL -- active connection not shutdown " + databaseName + ";" + shutdownString); 175 c2.close(); 176 } 177 178 System.out.println("Shutdown test completed."); 179 return passed; 180 } 181 182 private Connection cc; 184 private String sql; 185 String result; 186 private closed(Connection cc, String sql) { 187 this.cc = cc; 188 this.sql = sql; 189 } 190 public void run() { 191 192 try { 193 cc.createStatement().execute(sql); 194 result = "Sleep thread completed " + sql; 195 } catch (SQLException sqle) { 196 197 StringBuffer sb = new StringBuffer (); 200 sb.append(sql); 201 sb.append(" - "); 202 sb.append(sqle.getSQLState()); 203 while (sqle != null) 204 { 205 if (sqle != null) { 206 sb.append(", "); 207 sb.append(sqle.getSQLState()); 208 sb.append(" -- "); 209 if (sqle.getMessage().indexOf("InterruptedException") != -1) 210 sb.append("InterruptedException"); 211 else 212 { 213 sb.append(sqle.getMessage()); 214 sqle.printStackTrace(System.out); 215 } 216 } else { 217 sb.append(sqle.getMessage()); 218 } 219 sqle = sqle.getNextException(); 220 } 221 result = sb.toString(); 222 } 223 } 224 225 static boolean testStatement(Connection conn) throws SQLException { 226 Statement s; 227 boolean passed = true; 228 229 s = conn.createStatement(); 230 s.execute("create table t (i int)"); 231 s.execute("create table s (i int)"); 232 233 try { 234 s.execute("create table u (i int)"); 235 } catch (SQLException se) { 236 passed = false; JDBCDisplayUtil.ShowSQLException(System.out,se); 241 } 242 if (!passed) 243 System.out.println("FAIL -- no error on execute of closed statement"); 244 return passed; 245 } 246 247 static boolean testPreparedStatement(Connection conn) throws SQLException { 248 PreparedStatement ps; 249 boolean passed = true; 250 251 ps = conn.prepareStatement("insert into t values (1)"); 252 ps.execute(); 253 ps.execute(); 254 ps.close(); 255 256 try { 257 passed = false; ps.execute(); 259 } catch (SQLException se) { 260 passed = true; 261 JDBCDisplayUtil.ShowSQLException(System.out,se); 263 } 264 if (!passed) 265 System.out.println("FAIL -- no error on execute of closed prepared statement"); 266 267 return passed; 268 } 269 270 271 static boolean testResultSet(Connection conn) throws SQLException { 272 PreparedStatement ps; 273 Statement s; 274 ResultSet rs; 275 boolean passed = true; 276 277 ps = conn.prepareStatement("insert into s values (1)"); 279 ps.execute(); 280 ps.execute(); 281 ps.execute(); 282 ps.execute(); 283 ps.execute(); 284 ps.close(); 285 286 s = conn.createStatement(); 287 rs = s.executeQuery("select * from s"); 288 289 rs.next(); 290 rs.next(); 291 rs.close(); 292 293 try { 294 passed = false; rs.next(); 296 } catch (SQLException se) { 297 passed = true; 298 JDBCDisplayUtil.ShowSQLException(System.out,se); 300 } 301 if (!passed) 302 System.out.println("FAIL -- no error on next of closed result set"); 303 304 rs = s.executeQuery("select * from s"); 306 307 rs.next(); 308 rs.next(); 309 s.close(); 310 311 try { 312 passed = false; rs.next(); 314 } catch (SQLException se) { 315 passed = true; 316 JDBCDisplayUtil.ShowSQLException(System.out,se); 318 } 319 if (!passed) 320 System.out.println("FAIL -- no error on next of result set with closed statement"); 321 322 return passed; 323 } 324 325 static boolean testConnection(Connection conn) throws SQLException { 326 DatabaseMetaData dmd; 327 ResultSet rs; 328 Statement s; 329 PreparedStatement ps; 330 boolean passed = true; 331 332 dmd = conn.getMetaData(); 333 s = conn.createStatement(); 334 ps = conn.prepareStatement("create table w (i int)"); 335 336 rs = dmd.getTables("%","%","%",null); 338 conn.close(); 339 340 try { 342 passed = false; s.execute("create table x (i int)"); 344 } catch (SQLException se) { 345 passed = true; 346 JDBCDisplayUtil.ShowSQLException(System.out,se); 348 } 349 if (!passed) 350 System.out.println("FAIL -- no error on statement execute after connection close"); 351 352 try { 354 passed = false; ps.execute(); 356 } catch (SQLException se) { 357 passed = true; 358 JDBCDisplayUtil.ShowSQLException(System.out,se); 360 } 361 if (!passed) 362 System.out.println("FAIL -- no error on prepared statement execute after connection close"); 363 364 try { 366 passed = false; s = conn.createStatement(); 368 } catch (SQLException se) { 369 passed = true; 370 JDBCDisplayUtil.ShowSQLException(System.out,se); 372 } 373 if (!passed) 374 System.out.println("FAIL -- no error on statement creation after connection close"); 375 376 try { 378 passed = false; ps = conn.prepareStatement("create table z (i int)"); 380 } catch (SQLException se) { 381 passed = true; 382 JDBCDisplayUtil.ShowSQLException(System.out,se); 384 } 385 if (!passed) 386 System.out.println("FAIL -- no error on statement preparation after connection close"); 387 388 try { 390 passed = false; rs.next(); 392 } catch (SQLException se) { 393 passed = true; 394 JDBCDisplayUtil.ShowSQLException(System.out,se); 396 } 397 if (!passed) 398 System.out.println("FAIL -- no error on metadata reading after connection close"); 399 400 try { 402 passed = false; rs = dmd.getColumns("%","%","%","%"); 404 } catch (SQLException se) { 405 passed = true; 406 JDBCDisplayUtil.ShowSQLException(System.out,se); 408 } 409 if (!passed) 410 System.out.println("FAIL -- no error on metadata collecting after connection close"); 411 412 try { 414 passed = false; dmd = conn.getMetaData(); 416 } catch (SQLException se) { 417 passed = true; 418 JDBCDisplayUtil.ShowSQLException(System.out,se); 420 } 421 if (!passed) 422 System.out.println("FAIL -- no error on getting metadata after connection close"); 423 424 return passed; 425 } 426 427 static boolean testDerby62(Connection conn) throws SQLException { 428 429 System.out.println("Test case for Derby-62 - serialization error with SQLException"); 430 try { 431 conn.createStatement().execute("DROP TABLE APP.DERBY62_DAIN_SUNDSTROM"); 432 return false; 433 } catch (SQLException sqle) { 434 boolean passed = true; 435 try { 436 java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream (new java.io.ByteArrayOutputStream (1024)); 438 oos.writeObject(sqle); 439 oos.close(); 440 } catch (java.io.IOException ioe) 441 { 442 System.out.println("IOException " + ioe.getMessage()); 443 passed = false; 444 445 } 446 System.out.println(sqle.getMessage()); 447 return passed; 448 } 449 } 450 451 } 452 | Popular Tags |