1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 63 64 import java.sql.Connection ; 65 import java.sql.Statement ; 66 import java.sql.PreparedStatement ; 67 import java.sql.ResultSet ; 68 69 import java.io.IOException ; 70 import java.sql.SQLException ; 71 72 import org.apache.derby.iapi.reference.SQLState; 73 74 import org.apache.derbyTesting.functionTests.util.TestUtil; 75 76 import org.apache.derby.iapi.error.StandardException; 77 78 79 80 public class ShutdownDatabase{ 81 82 83 public static void main(String [] args) { 84 try{ 85 87 testShutDownWithCommitedTransaction(); 88 testShutDownWithRollbackedTransaction(); 89 testShutDownWithLeftTransaction(); 90 91 }catch(SQLException e){ 92 e.printStackTrace(); 93 94 }catch(Throwable t){ 95 t.printStackTrace(); 96 97 } 98 99 } 100 101 102 private static void testShutDownWithCommitedTransaction() 103 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 104 105 testOnlyTransactionWasCommited(); 106 testTwiceCommited(); 107 testOnceRollbackedAndCommited(); 108 109 } 110 111 112 private static void testShutDownWithRollbackedTransaction() 113 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 114 115 testOnlyTransactionWasRollbacked(); 116 testOnceCommitedAndRollbacked(); 117 testTwiceRollbacked(); 118 119 } 120 121 122 private static void testShutDownWithLeftTransaction() 123 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 124 125 testOnlyTransactionWasLeft(); 126 testOnceCommitedAndLeft(); 127 testOnceRollbackedAndLeft(); 128 129 } 130 131 132 private static void testOnlyTransactionWasCommited() 133 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 134 135 final String dbname = "testOnlyTransactionWasCommitedDB"; 136 Connection conn = null; 137 138 139 try{ 140 conn = openConnectionToNewDatabase(dbname); 141 createTestTable(conn); 142 143 conn.setAutoCommit(false); 144 insertIntoTestTable(conn, 145 1, 146 1000); 147 conn.commit(); 148 149 shutdownDatabase(dbname); 150 151 }catch(SQLException e){ 152 verifyShutdownError(e); 153 } 154 155 156 conn = null; 157 158 try{ 159 conn = reopenConnectionToDatabase(dbname); 160 countRowInTestTable(conn); 161 162 }finally{ 163 if(conn != null){ 164 conn.close(); 165 conn = null; 166 } 167 } 168 169 } 170 171 172 private static void testTwiceCommited() 173 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 174 175 final String dbname = "testTwiceCommitedDB"; 176 Connection conn = null; 177 178 179 try{ 180 conn = openConnectionToNewDatabase(dbname); 181 createTestTable(conn); 182 183 conn.setAutoCommit(false); 184 insertIntoTestTable(conn, 185 1, 186 1000); 187 conn.commit(); 188 insertIntoTestTable(conn, 189 1001, 190 999); 191 conn.commit(); 192 193 shutdownDatabase(dbname); 194 195 }catch(SQLException e){ 196 verifyShutdownError(e); 197 } 198 199 200 conn = null; 201 202 try{ 203 conn = reopenConnectionToDatabase(dbname); 204 countRowInTestTable(conn); 205 206 }finally{ 207 if(conn != null){ 208 conn.close(); 209 conn = null; 210 } 211 } 212 213 } 214 215 216 private static void testOnceRollbackedAndCommited() 217 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 218 219 final String dbname = "testOnceRollbackedAndCommitedDB"; 220 Connection conn = null; 221 222 223 try{ 224 conn = openConnectionToNewDatabase(dbname); 225 createTestTable(conn); 226 227 conn.setAutoCommit(false); 228 insertIntoTestTable(conn, 229 1, 230 1000); 231 conn.rollback(); 232 insertIntoTestTable(conn, 233 1001, 234 999); 235 conn.commit(); 236 237 shutdownDatabase(dbname); 238 239 }catch(SQLException e){ 240 verifyShutdownError(e); 241 } 242 243 244 conn = null; 245 246 try{ 247 conn = reopenConnectionToDatabase(dbname); 248 countRowInTestTable(conn); 249 250 }finally{ 251 if(conn != null){ 252 conn.close(); 253 conn = null; 254 } 255 } 256 257 } 258 259 260 private static void testOnlyTransactionWasRollbacked() 261 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 262 263 final String dbname = "testOnlyTransactionWasRollbackedDB"; 264 Connection conn = null; 265 266 267 try{ 268 conn = openConnectionToNewDatabase(dbname); 269 createTestTable(conn); 270 271 conn.setAutoCommit(false); 272 insertIntoTestTable(conn, 273 1, 274 1000); 275 conn.rollback(); 276 277 shutdownDatabase(dbname); 278 279 }catch(SQLException e){ 280 verifyShutdownError(e); 281 } 282 283 284 conn = null; 285 286 try{ 287 conn = reopenConnectionToDatabase(dbname); 288 countRowInTestTable(conn); 289 290 }finally{ 291 if(conn != null){ 292 conn.close(); 293 conn = null; 294 } 295 } 296 297 } 298 299 300 private static void testOnceCommitedAndRollbacked() 301 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 302 303 final String dbname = "testOnceCommitedAndRollbackedDB"; 304 Connection conn = null; 305 306 307 try{ 308 conn = openConnectionToNewDatabase(dbname); 309 createTestTable(conn); 310 311 conn.setAutoCommit(false); 312 insertIntoTestTable(conn, 313 1, 314 1000); 315 conn.commit(); 316 insertIntoTestTable(conn, 317 1001, 318 999); 319 conn.rollback(); 320 321 shutdownDatabase(dbname); 322 323 }catch(SQLException e){ 324 verifyShutdownError(e); 325 } 326 327 328 conn = null; 329 330 try{ 331 conn = reopenConnectionToDatabase(dbname); 332 countRowInTestTable(conn); 333 334 }finally{ 335 if(conn != null){ 336 conn.close(); 337 conn = null; 338 } 339 } 340 341 } 342 343 344 private static void testTwiceRollbacked() 345 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 346 347 final String dbname = "testTwiceRollbackedDB"; 348 Connection conn = null; 349 350 351 try{ 352 conn = openConnectionToNewDatabase(dbname); 353 createTestTable(conn); 354 355 conn.setAutoCommit(false); 356 insertIntoTestTable(conn, 357 1, 358 1000); 359 conn.rollback(); 360 insertIntoTestTable(conn, 361 1001, 362 999); 363 conn.rollback(); 364 365 shutdownDatabase(dbname); 366 367 }catch(SQLException e){ 368 verifyShutdownError(e); 369 } 370 371 372 conn = null; 373 374 try{ 375 conn = reopenConnectionToDatabase(dbname); 376 countRowInTestTable(conn); 377 378 }finally{ 379 if(conn != null){ 380 conn.close(); 381 conn = null; 382 } 383 } 384 385 } 386 387 388 private static void testOnlyTransactionWasLeft() 389 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 390 391 final String dbname = "testOnlyTransactionWasLeftDB"; 392 Connection conn = null; 393 394 395 try{ 396 conn = openConnectionToNewDatabase(dbname); 397 createTestTable(conn); 398 399 conn.setAutoCommit(false); 400 insertIntoTestTable(conn, 401 1, 402 1000); 403 404 shutdownDatabase(dbname); 405 406 }catch(SQLException e){ 407 verifyShutdownError(e); 408 } 409 410 411 conn = null; 412 413 try{ 414 conn = reopenConnectionToDatabase(dbname); 415 countRowInTestTable(conn); 416 417 }finally{ 418 if(conn != null){ 419 conn.close(); 420 conn = null; 421 } 422 } 423 424 } 425 426 427 private static void testOnceCommitedAndLeft() 428 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 429 430 final String dbname = "testOnceCommitedAndLeftDB"; 431 Connection conn = null; 432 433 434 try{ 435 conn = openConnectionToNewDatabase(dbname); 436 createTestTable(conn); 437 438 conn.setAutoCommit(false); 439 insertIntoTestTable(conn, 440 1, 441 1000); 442 conn.commit(); 443 insertIntoTestTable(conn, 444 1001, 445 999); 446 447 shutdownDatabase(dbname); 448 449 }catch(SQLException e){ 450 verifyShutdownError(e); 451 } 452 453 454 conn = null; 455 456 try{ 457 conn = reopenConnectionToDatabase(dbname); 458 countRowInTestTable(conn); 459 460 }finally{ 461 if(conn != null){ 462 conn.close(); 463 conn = null; 464 } 465 } 466 467 } 468 469 470 private static void testOnceRollbackedAndLeft() 471 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 472 473 final String dbname = "testOnceRollbackedAndLeftDB"; 474 Connection conn = null; 475 476 477 try{ 478 conn = openConnectionToNewDatabase(dbname); 479 createTestTable(conn); 480 481 conn.setAutoCommit(false); 482 insertIntoTestTable(conn, 483 1, 484 1000); 485 conn.rollback(); 486 insertIntoTestTable(conn, 487 1001, 488 999); 489 490 shutdownDatabase(dbname); 491 492 }catch(SQLException e){ 493 verifyShutdownError(e); 494 } 495 496 497 conn = null; 498 499 try{ 500 conn = reopenConnectionToDatabase(dbname); 501 countRowInTestTable(conn); 502 503 }finally{ 504 if(conn != null){ 505 conn.close(); 506 conn = null; 507 } 508 } 509 510 } 511 512 513 private static Connection openConnectionToNewDatabase(String databaseName) 514 throws SQLException { 516 517 Connection conn = TestUtil.getConnection(databaseName, "create=true"); 518 519 System.out.println("A connection to " + databaseName + " was opened."); 520 521 return conn; 522 523 } 524 525 526 private static Connection reopenConnectionToDatabase(String databaseName) 527 throws SQLException { 529 530 return TestUtil.getConnection(databaseName, null); 531 532 } 533 534 535 private static void shutdownDatabase(String databaseName) 536 throws SQLException , ClassNotFoundException , InstantiationException , IllegalAccessException { 537 538 TestUtil.getConnection(databaseName, "shutdown=true"); 539 540 System.out.println(databaseName + " was shut down."); 541 542 } 543 544 545 private static void createTestTable(Connection conn) 546 throws SQLException { 547 548 Statement st = null; 549 550 try{ 551 st = conn.createStatement(); 552 st.execute( "create table " + 553 "TEST_TABLE " + 554 "( TEST_COL integer )" ); 555 556 }finally{ 557 if(st != null){ 558 st.close(); 559 st = null; 560 } 561 } 562 } 563 564 565 private static void insertIntoTestTable(Connection conn, 566 int val) 567 throws SQLException { 568 569 PreparedStatement st = null; 570 571 try{ 572 st = conn.prepareStatement( "insert into " + 573 "TEST_TABLE " + 574 "( TEST_COL ) " + 575 "values( ? )" ); 576 st.setInt(1,val); 577 st.execute(); 578 579 }finally{ 580 if(st != null){ 581 st.close(); 582 st = null; 583 } 584 } 585 } 586 587 588 private static void insertIntoTestTable(Connection conn, 589 int initialval, 590 int count) 591 throws SQLException { 592 593 for( int i = initialval ; 594 i < initialval + count ; 595 i ++ ){ 596 597 insertIntoTestTable(conn, i ); 598 599 } 600 601 } 602 603 604 private static void countRowInTestTable(Connection conn) 605 throws SQLException { 606 607 Statement st = null; 608 ResultSet rs = null; 609 610 try{ 611 st = conn.createStatement(); 612 rs = st.executeQuery( "select " + 613 "count(*) " + 614 "from " + 615 "TEST_TABLE " ); 616 617 rs.next(); 618 System.out.println(rs.getInt(1)); 619 620 }finally{ 621 622 if(rs != null){ 623 rs.close(); 624 rs = null; 625 } 626 627 if(st != null){ 628 st.close(); 629 st = null; 630 } 631 632 } 633 } 634 635 636 private static void verifyShutdownError(SQLException e) 637 throws SQLException { 638 639 if(!isShutdownError(e)) 640 throw e; 641 642 System.out.println("SQLException of shutting down was found."); 643 644 } 645 646 647 private static boolean isShutdownError(SQLException e){ 648 return e.getSQLState().equals(StandardException.getSQLStateFromIdentifier(SQLState.SHUTDOWN_DATABASE)); 649 } 650 651 652 } 653 | Popular Tags |