1 21 22 package org.apache.derbyTesting.functionTests.tests.store; 23 import java.sql.Connection ; 24 import java.sql.Statement ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import org.apache.derby.tools.ij; 29 import org.apache.derbyTesting.functionTests.util.TestUtil; 30 31 38 39 public class OnlineBackupTest3 { 40 41 private static final String TEST_DATABASE_NAME = "wombat" ; 42 private static final String BACKUP_PATH = "extinout/onlinebackuptest3"; 43 44 public static void main(String [] argv) throws Throwable { 45 46 OnlineBackupTest3 test = new OnlineBackupTest3(); 47 ij.getPropertyArg(argv); 48 49 try { 50 test.runTest(); 51 } 52 catch (SQLException sqle) { 53 dumpSQLException(sqle); 54 } 55 } 56 57 58 61 private void runTest() throws Exception { 62 logMessage("Begin Online Backup Test3"); 63 Connection conn = ij.startJBMS(); 64 conn.setAutoCommit(false); 65 Statement stmt = conn.createStatement(); 66 stmt.execute("create table t1(a int ) "); 67 stmt.execute("insert into t1 values(1)"); 68 stmt.execute("insert into t1 values(2)"); 69 stmt.execute("create table customer(id int , name varchar(100))"); 70 stmt.execute("insert into customer values(1, 'ABC')"); 71 stmt.execute("insert into customer values(2, 'XYZ')"); 72 String crproc = "create procedure addCustomer(id INT, name VARCHAR(100)) " + 73 "MODIFIES SQL DATA " + 74 "external name " + 75 "'org.apache.derbyTesting.backupRestore.Customer.addCustomer' " + 76 " language java parameter style java "; 77 78 stmt.execute(crproc); 79 80 String dvfunc = "create function dv(P1 INT) RETURNS INT NO SQL " + 81 " external name 'dbytesting.CodeInAJar.doubleMe' " + 82 " language java parameter style java " ; 83 84 stmt.execute(dvfunc) ; 85 conn.commit(); 86 87 logMessage("Initial Setup Complete"); 88 89 installJarTest(); 92 93 removeJarTest(); 96 97 logMessage("End Online Backup Test3"); 98 } 99 100 101 105 void shutdown(String dbName) { 106 107 try{ 108 TestUtil.getConnection(dbName, "shutdown=true"); 110 }catch(SQLException se){ 111 if (se.getSQLState() != null && se.getSQLState().equals("08006")) 112 System.out.println("database shutdown properly"); 113 else 114 dumpSQLException(se); 115 } 116 } 117 118 121 void logMessage(String str){ 122 System.out.println(str); 123 } 124 125 128 static private void dumpSQLException(SQLException sqle) { 129 130 org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle); 131 sqle.printStackTrace(System.out); 132 } 133 134 135 private int countRows(Connection conn, 136 String tableName) 137 throws SQLException 138 { 139 Statement s = conn.createStatement(); 140 ResultSet rs = s.executeQuery("SELECT count(*) from " + tableName ); 141 rs.next(); 142 int noRows = rs.getInt(1); 143 rs.close(); 144 s.close(); 145 return noRows; 146 } 147 148 151 void installJarTest() throws Exception { 152 logMessage("Begin Install Jar Test"); 153 Connection conn1 = TestUtil.getConnection(TEST_DATABASE_NAME, null); 154 conn1.setAutoCommit(false); 155 Statement conn1_stmt = conn1.createStatement(); 156 Connection conn2 = TestUtil.getConnection(TEST_DATABASE_NAME, null); 157 conn2.setAutoCommit(false); 158 Statement conn2_stmt = conn2.createStatement(); 159 160 161 conn1_stmt.execute( 162 "call sqlj.install_jar('extin/brtestjar.jar', 'math_routines', 0)"); 163 164 try { 165 conn2_stmt.execute( 167 "call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_NOWAIT('extinout/mybackup')"); 168 } catch (SQLException sqle) { 169 org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle); 171 } 172 173 176 OnlineBackup backup = new OnlineBackup(TEST_DATABASE_NAME, BACKUP_PATH); 178 Thread backupThread = new Thread (backup, "BACKUP1"); 179 backupThread.start(); 180 backup.waitForBackupToBegin(); 182 logMessage("Backup-1 Started"); 183 184 java.lang.Thread.sleep(1000); 187 188 if(!backup.isRunning()) 191 logMessage("Backup is not waiting for unlogged " + 192 "install jar action to commit"); 193 194 conn1_stmt.execute("insert into t1 values(3)"); 196 conn1_stmt.execute("insert into t1 values(4)"); 197 conn1_stmt.execute("insert into t1 values(5)"); 198 199 conn1_stmt.execute("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY( " + 201 "'derby.database.classpath', " + 202 "'APP.math_routines') " ) ; 203 204 conn1.commit(); 206 logMessage("The transaction that was blocking the backup has ended"); 207 208 backup.waitForBackupToEnd(); 210 backupThread.join(); 211 logMessage("Backup-1 Completed"); 212 213 conn1_stmt.execute("create index idx1 on customer(id)"); 216 conn1_stmt.execute("insert into t1 values(6)"); 217 218 backup = new OnlineBackup(TEST_DATABASE_NAME, BACKUP_PATH); 220 backupThread = new Thread (backup, "BACKUP2"); 221 backupThread.start(); 222 backup.waitForBackupToBegin(); 224 logMessage("Backup-2 Started"); 225 226 java.lang.Thread.sleep(1000); 229 230 if(!backup.isRunning()) 233 logMessage("Backup is not waiting for unlogged " + 234 "index action to commit"); 235 236 237 242 AsyncStatementThread asyncJarActionThread = 243 new AsyncStatementThread(conn2, 244 "call sqlj.install_jar('extin/obtest_customer.jar', 'customer_app', 0)"); 245 asyncJarActionThread.start(); 246 logMessage("Started obtest_customer.jar addition in seperate thread"); 247 248 java.lang.Thread.sleep(1000); 251 252 conn1.rollback(); 254 logMessage("The transaction that was blocking the backup has ended"); 255 256 backup.waitForBackupToEnd(); 258 backupThread.join(); 259 logMessage("Backup-2 Completed"); 260 261 asyncJarActionThread.join(); 263 logMessage("obtest_customer.jar addition is complete"); 264 265 conn1_stmt.execute("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY( " + 267 "'derby.database.classpath', " + 268 "'APP.customer_app:APP.math_routines') " ) ; 269 270 conn1.commit(); 271 272 conn1_stmt.execute("call addCustomer(3 , 'John')"); 275 conn1.commit(); 276 277 logMessage("No of rows in table t1: " + countRows(conn1, "T1")); 278 logMessage("No of rows in table customer: " + 279 countRows(conn1, "customer")); 280 conn1.commit(); 281 conn2.commit(); 282 conn1_stmt.close(); 283 conn2_stmt.close(); 284 conn1.close(); 285 conn2.close(); 286 287 shutdown(TEST_DATABASE_NAME); 289 backup.restoreFromBackup(); 291 logMessage("Restored From the Backup"); 292 Connection conn = TestUtil.getConnection(TEST_DATABASE_NAME, null); 293 Statement stmt = conn.createStatement(); 294 logMessage("No of rows in table t1: " + countRows(conn, "T1")); 295 logMessage("No of rows in table customer: " + 296 countRows(conn, "customer")); 297 stmt.execute("select dv(a) from t1"); 299 300 301 try { 302 stmt.execute("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY( " + 304 "'derby.database.classpath', " + 305 "'APP.customer_app:APP.math_routines') " ) ; 306 stmt.execute("call addCustomer(3 , 'John')"); 307 }catch(SQLException se) { 308 } 312 313 stmt.close(); 314 conn.close(); 315 316 shutdown(TEST_DATABASE_NAME); 318 logMessage("End Of Install Jar Test."); 319 320 } 321 322 323 326 void removeJarTest() throws Exception { 327 logMessage("Begin Remove Jar Test"); 328 Connection conn1 = TestUtil.getConnection(TEST_DATABASE_NAME, null); 329 conn1.setAutoCommit(false); 330 Statement conn1_stmt = conn1.createStatement(); 331 Connection conn2 = TestUtil.getConnection(TEST_DATABASE_NAME, null); 332 conn2.setAutoCommit(false); 333 Statement conn2_stmt = conn2.createStatement(); 334 try { 335 conn1_stmt.execute( 336 "call sqlj.install_jar('extin/obtest_customer.jar', 'customer_app', 0)"); 337 }catch(SQLException se) { 338 } 340 341 conn1_stmt.execute("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY( " + 344 "'derby.database.classpath', '')") ; 345 conn1.commit(); 346 347 conn1_stmt.execute( 348 "call sqlj.remove_jar('APP.math_routines', 0)"); 349 350 354 try { 355 conn2_stmt.execute( 358 "call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_NOWAIT('extinout/mybackup')"); 359 } catch (SQLException sqle) { 360 org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle); 362 } 363 364 365 366 369 372 OnlineBackup backup = new OnlineBackup(TEST_DATABASE_NAME, BACKUP_PATH); 374 Thread backupThread = new Thread (backup, "BACKUP3"); 375 backupThread.start(); 376 backup.waitForBackupToBegin(); 378 logMessage("Backup-3 Started"); 379 380 java.lang.Thread.sleep(1000); 383 384 if(!backup.isRunning()) 387 logMessage("Backup is not waiting for unlogged " + 388 "remove jar action to commit"); 389 390 conn1_stmt.execute("insert into t1 values(10)"); 392 conn1_stmt.execute("insert into t1 values(11)"); 393 394 conn1.commit(); 396 logMessage("The transaction that was blocking the backup has ended"); 397 398 backup.waitForBackupToEnd(); 400 backupThread.join(); 401 402 logMessage("Backup-3 Completed"); 403 404 conn1_stmt.execute("create index idx1 on customer(id)"); 407 conn1_stmt.execute("insert into t1 values(12)"); 408 409 backup = new OnlineBackup(TEST_DATABASE_NAME, BACKUP_PATH); 411 backupThread = new Thread (backup, "BACKUP4"); 412 backupThread.start(); 413 backup.waitForBackupToBegin(); 415 logMessage("Backup-4 Started"); 416 417 java.lang.Thread.sleep(1000); 420 421 if(!backup.isRunning()) 424 logMessage("Backup is not waiting for unlogged " + 425 "index action to commit"); 426 427 428 433 AsyncStatementThread asyncJarActionThread = 434 new AsyncStatementThread(conn2, 435 "call sqlj.remove_jar('APP.customer_app', 0)"); 436 asyncJarActionThread.start(); 437 logMessage("Started obtest_customer.jar remove in seperate thread"); 438 439 java.lang.Thread.sleep(1000); 442 443 conn1.rollback(); 445 logMessage("The transaction that was blocking the backup has ended"); 446 backup.waitForBackupToEnd(); 448 backupThread.join(); 449 logMessage("Backup-4 Completed"); 450 451 asyncJarActionThread.join(); 453 logMessage("obtest_customer.jar remove is complete"); 454 455 conn1_stmt.execute("insert into t1 values(13)"); 457 458 logMessage("No of rows in table t1: " + countRows(conn1, "T1")); 459 logMessage("No of rows in table customer: " + 460 countRows(conn1, "customer")); 461 conn1.commit(); 462 conn2.commit(); 463 conn1_stmt.close(); 464 conn2_stmt.close(); 465 conn1.close(); 466 conn2.close(); 467 468 shutdown(TEST_DATABASE_NAME); 470 backup.restoreFromBackup(); 472 logMessage("Restored From the Backup"); 473 Connection conn = TestUtil.getConnection(TEST_DATABASE_NAME, null); 474 Statement stmt = conn.createStatement(); 475 logMessage("No of rows in table t1: " + countRows(conn, "T1")); 476 logMessage("No of rows in table customer: " + 477 countRows(conn, "customer")); 478 479 try { 482 stmt.execute("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY( " + 485 "'derby.database.classpath', " + 486 "'APP.math_routines') " ) ; 487 }catch (SQLException sqle) { 488 org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle); 490 } 491 492 493 stmt.close(); 494 conn.close(); 495 496 shutdown(TEST_DATABASE_NAME); 498 logMessage("End Of Remove Jar Test."); 499 500 } 501 502 503 506 class AsyncStatementThread extends Thread { 507 Connection conn; 508 String stmt; 509 510 AsyncStatementThread(Connection conn, String stmt) { 511 this.conn = conn; 512 this.stmt = stmt; 513 } 514 515 public void run() { 516 Statement aStatement = null; 517 try { 518 aStatement = conn.createStatement(); 519 aStatement.execute(stmt); 520 aStatement.close(); 521 conn.commit(); 525 } catch (SQLException sqle) { 526 org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle); 527 sqle.printStackTrace(System.out); 528 } 529 aStatement = null; 530 } 531 } 532 533 } 534 | Popular Tags |