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 OnlineBackupTest1 { 40 41 private static final String TEST_DATABASE_NAME = "wombat" ; 42 private static final String TEST_TABLE_NAME = "emp"; 43 private static final String TEST_TABLE_NAME_1 = "emp_1"; 44 private static final String TEST_TABLE_NAME_2 = "emp_2"; 45 private static final String BACKUP_PATH = "extinout/onlinebackuptest1"; 46 47 public static void main(String [] argv) throws Throwable { 48 49 OnlineBackupTest1 test = new OnlineBackupTest1(); 50 ij.getPropertyArg(argv); 51 52 try { 53 test.runTest(); 54 } 55 catch (SQLException sqle) { 56 dumpSQLException(sqle); 57 } 58 } 59 60 61 67 private void runTest() throws Exception { 68 logMessage("Begin Online Backup Test1"); 69 Connection conn = ij.startJBMS(); 70 conn.setAutoCommit(false); 71 DatabaseActions dbActions = new DatabaseActions(conn); 72 dbActions.createTable(TEST_TABLE_NAME); 74 dbActions.createTable(TEST_TABLE_NAME_1); 75 dbActions.createTable(TEST_TABLE_NAME_2); 76 conn.commit(); 77 78 dbActions.startUnloggedAction(TEST_TABLE_NAME_1); 80 logMessage("First Transaction with Unlogged Operation Started"); 81 82 Connection conn1 = ij.startJBMS(); 84 conn1.setAutoCommit(false); 85 DatabaseActions dbActions1 = new DatabaseActions(conn1); 86 dbActions1.startUnloggedAction(TEST_TABLE_NAME_2); 87 logMessage("Second Transaction with Unlogged Operation Started"); 88 89 OnlineBackup backup = new OnlineBackup(TEST_DATABASE_NAME, BACKUP_PATH); 92 Thread backupThread = new Thread (backup, "BACKUP"); 93 94 Connection dmlConn = TestUtil.getConnection(TEST_DATABASE_NAME, null); 96 DatabaseActions dmlActions = 97 new DatabaseActions(DatabaseActions.DMLACTIONS, dmlConn); 98 Thread dmlThread = new Thread (dmlActions, "DML_THREAD"); 99 100 Connection ddlConn = TestUtil.getConnection(TEST_DATABASE_NAME, null); 102 103 DatabaseActions ddlActions = 104 new DatabaseActions(DatabaseActions.CREATEDROPS, ddlConn); 105 Thread ddlThread = new Thread (ddlActions, "DDL_THREAD"); 106 107 try { 108 backupThread.start(); 110 backup.waitForBackupToBegin(); 112 logMessage("BACKUP STARTED"); 113 114 dmlThread.start(); 116 117 ddlThread.start(); 119 120 java.lang.Thread.sleep(50000); 124 125 if(!backup.isRunning()) 128 logMessage("Backup is not waiting for unlogged actions to commit"); 129 130 dbActions.endUnloggedAction(TEST_TABLE_NAME_1); 132 dbActions1.endUnloggedAction(TEST_TABLE_NAME_2); 134 135 backup.waitForBackupToEnd(); 136 137 }finally { 138 backupThread.join(); 140 dmlActions.stopActivity(); 141 ddlActions.stopActivity(); 142 dmlThread.join(); 143 ddlThread.join(); 144 } 145 conn.close(); 147 conn1.close(); 148 dmlConn.close(); 149 ddlConn.close() ; 150 151 152 shutdown(TEST_DATABASE_NAME); 154 155 backup.restoreFromBackup(); 157 logMessage("Restored From the Backup"); 158 runConsistencyChecker(TEST_DATABASE_NAME); 159 logMessage("Consistency Check is Done"); 160 shutdown(TEST_DATABASE_NAME); 162 logMessage("End Online Backup Test1"); 163 } 164 165 166 170 void runConsistencyChecker(String dbName) throws SQLException { 171 Connection conn = TestUtil.getConnection(dbName, null); 172 Statement stmt = conn.createStatement(); 173 stmt.execute("values SYSCS_UTIL.SYSCS_CHECK_TABLE('APP', 'EMP')"); 174 DatabaseActions dbActions = new DatabaseActions(conn); 176 dbActions.select(TEST_TABLE_NAME); 177 dbActions.select(TEST_TABLE_NAME_1); 178 dbActions.select(TEST_TABLE_NAME_2); 179 conn.close(); 180 181 } 182 183 184 185 189 void shutdown(String dbName) { 190 191 try{ 192 TestUtil.getConnection(dbName, "shutdown=true"); 194 }catch(SQLException se){ 195 if (se.getSQLState() != null && se.getSQLState().equals("08006")) 196 System.out.println("database shutdown properly"); 197 else 198 dumpSQLException(se); 199 } 200 } 201 202 205 void logMessage(String str) { 206 System.out.println(str); 207 } 208 209 210 213 static private void dumpSQLException(SQLException sqle) { 214 215 org.apache.derby.tools.JDBCDisplayUtil. ShowSQLException(System.out, sqle); 216 sqle.printStackTrace(System.out); 217 } 218 219 225 226 class DatabaseActions implements Runnable { 227 228 public static final int DMLACTIONS = 1; 229 public static final int CREATEDROPS = 2; 230 231 private static final int COMMIT = 1; 232 private static final int ROLLBACK = 2; 233 private static final int OPENTX = 3; 234 235 private int action = 0; 236 private volatile boolean stopActivity = false ; 237 private Connection conn; 238 239 DatabaseActions(Connection conn) { 240 this.conn = conn; 241 }; 242 243 DatabaseActions(int action, Connection conn) { 244 this.action = action; 245 this.conn = conn; 246 } 247 248 251 public void stopActivity() { 252 stopActivity = true; 253 } 254 255 261 public void run() { 262 try { 263 conn.setAutoCommit(false); 264 switch(action) { 265 case DMLACTIONS : 266 performDmlActions(); 267 break; 268 case CREATEDROPS: 269 performCreateDropTables() ; 270 break; 271 } 272 } catch (SQLException sqle) { 273 org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle); 274 sqle.printStackTrace(System.out); 275 } 276 } 277 278 279 282 void performDmlActions() throws SQLException { 283 284 while(!stopActivity) { 285 insert(TEST_TABLE_NAME, 100, COMMIT, 10); 286 insert(TEST_TABLE_NAME, 100, ROLLBACK, 10); 287 update(TEST_TABLE_NAME, 50, ROLLBACK, 10); 288 select(TEST_TABLE_NAME); 289 } 290 } 291 292 293 294 299 void startUnloggedAction(String tableName) throws SQLException { 300 insert(tableName, 100, COMMIT, 10); 302 Statement s = conn.createStatement(); 304 305 s.executeUpdate("create index " + tableName + "_name_idx on " + 307 tableName + "(name) "); 308 s.close(); 309 } 310 311 312 317 void endUnloggedAction(String tableName) throws SQLException { 318 insert(tableName, 1000, OPENTX, 10); 321 conn.commit(); 322 } 323 324 325 329 void performCreateDropTables() throws SQLException { 330 331 Statement s = conn.createStatement(); 332 while(!stopActivity) { 333 for( int i = 0 ; i < 10; i++) { 334 String tableName = "emp" + i ; 335 createTable(tableName); 336 insert(tableName, 100, OPENTX, 10); 338 if((i % 2) == 0) { 339 conn.commit(); 340 } 341 else 342 conn.rollback(); 343 } 344 345 for( int i = 0 ; i < 10 ; i=i+2) { 347 String tableName = "emp" + i ; 348 s.executeUpdate("drop TABLE " + "emp" +i ); 349 conn.commit(); 350 } 351 } 352 s.close(); 353 } 354 355 356 357 365 void insert(String tableName, int rowCount, 366 int txStatus, int commitCount) throws SQLException { 367 368 PreparedStatement ps = conn.prepareStatement("INSERT INTO " + 369 tableName + 370 " VALUES(?,?,?)"); 371 for (int i = 0; i < rowCount; i++) { 372 373 ps.setInt(1, i); ps.setString(2 , "skywalker" + i); 375 ps.setFloat(3, (float)(i * 2000)); 376 ps.executeUpdate(); 377 if ((i % commitCount) == 0) 378 { 379 endTransaction(txStatus); 380 } 381 } 382 383 endTransaction(txStatus); 384 ps.close(); 385 } 386 387 388 389 394 void endTransaction(int txStatus) throws SQLException 395 { 396 switch(txStatus){ 397 case COMMIT: 398 conn.commit(); 399 break; 400 case ROLLBACK: 401 conn.rollback(); 402 break; 403 case OPENTX: 404 break; 406 } 407 } 408 409 418 419 void update(String tableName, int rowCount, 420 int txStatus, int commitCount) throws SQLException 421 { 422 423 PreparedStatement ps = conn.prepareStatement("update " + tableName + 424 " SET name = ? where id=?"); 425 426 for (int i = 0; i < rowCount; i++) { 427 ps.setString(1 , "moonwalker" + i); 428 ps.setInt(2, i); ps.executeUpdate(); 430 if ((i % commitCount) == 0) 431 { 432 endTransaction(txStatus); 433 } 434 } 435 endTransaction(txStatus); 436 ps.close(); 437 } 438 439 440 445 void select(String tableName) throws SQLException { 446 447 Statement s = conn.createStatement(); 448 ResultSet rs = s.executeQuery("SELECT ID, name from " + 449 tableName + " order by id" ); 450 int count = 0; 451 int id = 0; 452 while(rs.next()) 453 { 454 int tid = rs.getInt(1); 455 String name = rs.getString(2); 456 if(name.equals("skywalker" + id) && tid!= id) 457 { 458 logMessage("DATA IN THE TABLE IS NOT AS EXPECTED"); 459 logMessage("Got :ID=" + tid + " Name=:" + name); 460 logMessage("Expected: ID=" + id + "Name=" + "skywalker" + id ); 461 } 462 463 id++; 464 count++; 465 } 466 467 rs.close(); 468 s.close(); 469 conn.commit(); 470 } 471 472 477 void createTable(String tableName) throws SQLException { 478 479 Statement s = conn.createStatement(); 480 s.executeUpdate("CREATE TABLE " + tableName + 481 "(id INT," + 482 "name CHAR(200),"+ 483 "salary float)"); 484 s.executeUpdate("create index " + tableName + "_id_idx on " + 485 tableName + "(id)"); 486 s.close(); 487 } 488 489 } 490 } 491 | Popular Tags |