1 package org.apache.ojb.broker; 2 3 import junit.framework.TestCase; 4 import org.apache.ojb.broker.accesslayer.ConnectionFactory; 5 import org.apache.ojb.broker.accesslayer.ConnectionFactoryFactory; 6 import org.apache.ojb.broker.metadata.ClassDescriptor; 7 import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor; 8 import org.apache.ojb.broker.metadata.MetadataManager; 9 import org.apache.ojb.broker.query.Criteria; 10 import org.apache.ojb.broker.query.Query; 11 import org.apache.ojb.broker.query.QueryByCriteria; 12 import org.apache.ojb.broker.util.logging.Logger; 13 import org.apache.ojb.broker.util.logging.LoggerFactory; 14 15 import java.sql.Connection ; 16 import java.sql.PreparedStatement ; 17 import java.sql.ResultSet ; 18 19 25 public class PerformanceJdbcFailoverTest extends TestCase 26 { 27 private Logger logger = LoggerFactory.getLogger("failover");; 28 29 32 static int articleCount = 10000; 33 34 37 static int iterations = 2; 38 39 42 static int maxRetries = 5; 43 44 47 static int maxWait = 30; 48 49 50 53 int offsetId = 10000; 54 PersistenceBroker broker; 55 private PerformanceArticle[] arr; 56 private int actualRetries = 0; 57 58 62 public PerformanceJdbcFailoverTest(String name) 63 64 { 65 super(name); 66 } 67 68 74 public static void main(String [] args) 75 { 76 if (args.length > 0) 77 { 78 articleCount = Integer.parseInt(args[0]); 79 } 80 if (args.length > 1) 81 { 82 iterations = Integer.parseInt(args[1]); 83 } 84 if (args.length > 2) 85 { 86 maxRetries = Integer.parseInt(args[2]); 87 } 88 if (args.length > 3) 89 { 90 maxWait = Integer.parseInt(args[3]); 91 } 92 93 94 String [] arr = {PerformanceJdbcFailoverTest.class.getName()}; 95 junit.textui.TestRunner.main(arr); 96 } 97 98 101 public void setUp() throws Exception 102 { 103 104 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 105 clearTable(); 106 107 arr = new PerformanceArticle[articleCount]; 108 for (int i = 0; i < articleCount; i++) 109 { 110 PerformanceArticle a = createArticle(offsetId + i); 111 arr[i] = a; 112 } 113 } 114 115 118 public void tearDown() 119 { 120 broker.close(); 121 } 122 123 128 private PerformanceArticle createArticle(int id) 129 { 130 PerformanceArticle a = new PerformanceArticle(); 131 a.setArticleId(id); 132 a.setArticleName("New Performance Article " + id); 133 a.setMinimumStock(100); 134 a.setOrderedUnits(17); 135 a.setPrice(100.0); 136 a.setProductGroupId(1); 137 a.setStock(234); 138 a.setSupplierId(4); 139 a.setUnit("bottle"); 140 return a; 141 } 142 143 148 private Connection getConnection() throws Exception 149 { 150 Connection conn = null; 151 long startToWait = System.currentTimeMillis(); 152 System.out.print("["); 153 System.out.flush(); 154 while (true) 155 { 156 try 157 { 158 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 161 JdbcConnectionDescriptor jcd = 162 MetadataManager.getInstance().connectionRepository().getDescriptor( 163 TestHelper.DEF_KEY); 164 ConnectionFactory cf = 165 ConnectionFactoryFactory.getInstance().createConnectionFactory(); 166 conn = cf.lookupConnection(jcd); 167 System.out.println("] Waited for connection " + (System.currentTimeMillis() - startToWait) + "msecs"); 168 break; 169 } 170 catch (Throwable t) 171 { 172 long now = System.currentTimeMillis(); 173 if ((now - startToWait) > (1000* maxWait)) 174 { 175 System.out.print("Timeout exceeded in getConnection(), DB not available!"); 176 throw new OJBException(t); 177 } 178 else 179 { 180 if ((now % 1000) == 0) 181 { 182 System.out.print("#"); 183 System.out.flush(); 184 } 185 186 } 187 } 188 } 189 return conn; 190 } 191 192 195 protected void deleteArticles() throws Exception 196 { 197 Connection conn = getConnection(); 198 199 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 202 String sql = broker.serviceSqlGenerator().getPreparedDeleteStatement(cld); 203 204 logger.debug("delete stmt: " + sql); 205 206 long start = System.currentTimeMillis(); 207 try 208 { 209 conn.setAutoCommit(false); 210 PreparedStatement stmt = conn.prepareStatement(sql); 211 for (int i = 0; i < articleCount; i++) 212 { 213 PerformanceArticle a = arr[i]; 214 stmt.setInt(1, a.articleId); 215 stmt.execute(); 216 } 217 conn.commit(); 218 } 219 catch (Throwable t) 220 { 221 actualRetries++; 222 if (actualRetries <= maxRetries) 223 { 224 logger.error("error during db operations:", t); 225 try 226 { 227 conn.close(); 228 } 229 catch (Throwable ignored) 230 { 231 } 232 deleteArticles(); 233 } 234 else 235 { 236 logger.error("retry count exceeded!"); 237 fail(t.getMessage()); 238 } 239 } 240 finally 241 { 242 try 243 { 244 conn.close(); 245 } 246 catch (Throwable ignored) 247 { 248 } 249 } 250 long stop = System.currentTimeMillis(); 251 logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec"); 252 } 253 254 258 protected void insertNewArticles() throws Exception 259 { 260 Connection conn = getConnection(); 261 262 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 265 String sql = broker.serviceSqlGenerator().getPreparedInsertStatement(cld); 266 267 logger.debug("insert stmt: " + sql); 268 269 long start = System.currentTimeMillis(); 270 try 271 { 272 conn.setAutoCommit(false); 273 PreparedStatement stmt = conn.prepareStatement(sql); 274 275 for (int i = 0; i < articleCount; i++) 276 { 277 PerformanceArticle a = arr[i]; 278 279 stmt.setInt(1, a.articleId); 280 stmt.setString(2, a.articleName); 281 stmt.setInt(3, a.supplierId); 282 stmt.setInt(4, a.productGroupId); 283 stmt.setString(5, a.unit); 284 stmt.setDouble(6, a.price); 285 stmt.setInt(7, a.stock); 286 stmt.setInt(8, a.orderedUnits); 287 stmt.setInt(9, a.minimumStock); 288 289 stmt.execute(); 290 } 291 conn.commit(); 292 } 293 catch (Throwable t) 294 { 295 actualRetries++; 296 if (actualRetries <= maxRetries) 297 { 298 logger.error("error during db operations:", t); 299 try 300 { 301 conn.close(); 302 } 303 catch (Throwable ignored) 304 { 305 } 306 insertNewArticles(); 307 } 308 else 309 { 310 logger.error("retry count exceeded!"); 311 fail(t.getMessage()); 312 } 313 } 314 finally 315 { 316 try 317 { 318 conn.close(); 319 } 320 catch (Throwable ignored) 321 { 322 } 323 } 324 long stop = System.currentTimeMillis(); 325 logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec"); 326 327 } 328 329 protected void clearTable() throws Exception 330 { 331 Connection conn = getConnection(); 332 try 333 { 334 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 335 String table = cld.getFullTableName(); 336 String sql = "DELETE FROM " + table; 337 PreparedStatement stmt = conn.prepareStatement(sql); 338 stmt.execute(); 339 conn.close(); 340 } 341 catch (Throwable t) 342 { 343 actualRetries++; 344 if (actualRetries <= maxRetries) 345 { 346 logger.error("error during db operations:", t); 347 try 348 { 349 conn.close(); 350 } 351 catch (Throwable ignored) 352 { 353 } 354 clearTable(); 355 } 356 else 357 { 358 logger.error("retry count exceeded!"); 359 fail(t.getMessage()); 360 } 361 } 362 finally 363 { 364 try 365 { 366 conn.close(); 367 } 368 catch (Throwable ignored) 369 { 370 } 371 } 372 } 373 374 379 protected void readArticles() throws Exception 380 { 381 Connection conn = getConnection(); 382 383 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 386 String sql = broker.serviceSqlGenerator().getPreparedSelectByPkStatement(cld); 387 logger.debug("select stmt: " + sql); 388 389 String colId = cld.getFieldDescriptorByName("articleId").getColumnName(); 390 String colName = cld.getFieldDescriptorByName("articleName").getColumnName(); 391 String colSupplier = cld.getFieldDescriptorByName("supplierId").getColumnName(); 392 String colGroup = cld.getFieldDescriptorByName("productGroupId").getColumnName(); 393 String colUnit = cld.getFieldDescriptorByName("unit").getColumnName(); 394 String colPrice = cld.getFieldDescriptorByName("price").getColumnName(); 395 String colStock = cld.getFieldDescriptorByName("stock").getColumnName(); 396 String colOrdered = cld.getFieldDescriptorByName("orderedUnits").getColumnName(); 397 String colMin = cld.getFieldDescriptorByName("minimumStock").getColumnName(); 398 399 long start = System.currentTimeMillis(); 400 try 401 { 402 conn.setAutoCommit(false); 403 PreparedStatement stmt = conn.prepareStatement(sql); 404 for (int i = 0; i < articleCount; i++) 405 { 406 stmt.setInt(1, offsetId + i); 407 ResultSet rs = stmt.executeQuery(); 408 rs.next(); 409 410 PerformanceArticle a = new PerformanceArticle(); 411 a.articleId = rs.getInt(colId); 412 a.articleName = rs.getString(colName); 413 a.supplierId = rs.getInt(colSupplier); 414 a.productGroupId = rs.getInt(colGroup); 415 a.unit = rs.getString(colUnit); 416 a.price = rs.getFloat(colPrice); 417 a.stock = rs.getInt(colStock); 418 a.orderedUnits = rs.getInt(colOrdered); 419 a.minimumStock = rs.getInt(colMin); 420 } 421 } 422 catch (Throwable t) 423 { 424 actualRetries++; 425 if (actualRetries <= maxRetries) 426 { 427 logger.error("error during db operations:", t); 428 try 429 { 430 conn.close(); 431 } 432 catch (Throwable ignored) 433 { 434 } 435 readArticles(); 436 } 437 else 438 { 439 logger.error("retry count exceeded!"); 440 fail(t.getMessage()); 441 } 442 } 443 finally 444 { 445 try 446 { 447 conn.close(); 448 } 449 catch (Throwable ignored) 450 { 451 } 452 } 453 454 455 long stop = System.currentTimeMillis(); 456 logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec"); 457 458 } 459 460 467 protected void readArticlesByCursor() throws Exception 468 { 469 Connection conn = getConnection(); 470 471 Criteria c = new Criteria(); 472 c.addBetween("articleId", new Integer (offsetId), new Integer (offsetId + articleCount)); 473 Query query = new QueryByCriteria(PerformanceArticle.class, c); 474 475 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 478 String sql = broker.serviceSqlGenerator().getPreparedSelectStatement(query, cld); 479 480 logger.debug("select stmt: " + sql); 481 482 String colId = cld.getFieldDescriptorByName("articleId").getColumnName(); 483 String colName = cld.getFieldDescriptorByName("articleName").getColumnName(); 484 String colSupplier = cld.getFieldDescriptorByName("supplierId").getColumnName(); 485 String colGroup = cld.getFieldDescriptorByName("productGroupId").getColumnName(); 486 String colUnit = cld.getFieldDescriptorByName("unit").getColumnName(); 487 String colPrice = cld.getFieldDescriptorByName("price").getColumnName(); 488 String colStock = cld.getFieldDescriptorByName("stock").getColumnName(); 489 String colOrdered = cld.getFieldDescriptorByName("orderedUnits").getColumnName(); 490 String colMin = cld.getFieldDescriptorByName("minimumStock").getColumnName(); 491 492 493 int fetchCount = 0; 494 long start = System.currentTimeMillis(); 495 try 496 { 497 conn.setAutoCommit(false); 498 PreparedStatement stmt = conn.prepareStatement(sql); 499 stmt.setInt(1, offsetId); 500 stmt.setInt(2, offsetId + articleCount); 501 ResultSet rs = stmt.executeQuery(); 502 while (rs.next()) 503 { 504 fetchCount++; 505 506 PerformanceArticle a = new PerformanceArticle(); 507 a.articleId = rs.getInt(colId); 508 a.articleName = rs.getString(colName); 509 a.supplierId = rs.getInt(colSupplier); 510 a.productGroupId = rs.getInt(colGroup); 511 a.unit = rs.getString(colUnit); 512 a.price = rs.getFloat(colPrice); 513 a.stock = rs.getInt(colStock); 514 a.orderedUnits = rs.getInt(colOrdered); 515 a.minimumStock = rs.getInt(colMin); 516 } 517 } 518 catch (Throwable t) 519 { 520 actualRetries++; 521 if (actualRetries <= maxRetries) 522 { 523 logger.error("error during db operations:", t); 524 try 525 { 526 conn.close(); 527 } 528 catch (Throwable ignored) 529 { 530 } 531 readArticlesByCursor(); 532 } 533 else 534 { 535 logger.error("retry count exceeded!"); 536 fail(t.getMessage()); 537 } 538 } 539 finally 540 { 541 try 542 { 543 conn.close(); 544 } 545 catch (Throwable ignored) 546 { 547 } 548 } 549 550 551 long stop = System.currentTimeMillis(); 552 logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec"); 553 554 } 555 556 560 protected void updateExistingArticles() throws Exception 561 { 562 Connection conn = getConnection(); 563 564 ClassDescriptor cld = broker.getClassDescriptor(PerformanceArticle.class); 567 String sql = broker.serviceSqlGenerator().getPreparedUpdateStatement(cld); 568 logger.debug("update stmt: " + sql); 569 570 for (int i = 0; i < articleCount; i++) 572 { 573 arr[i].setPrice(arr[i].getPrice() * 1.95583); 574 } 575 576 long start = System.currentTimeMillis(); 577 try 578 { 579 conn.setAutoCommit(false); 580 PreparedStatement stmt = conn.prepareStatement(sql); 581 for (int i = 0; i < articleCount; i++) 582 { 583 PerformanceArticle a = arr[i]; 584 stmt.setString(1, a.articleName); 585 stmt.setInt(2, a.supplierId); 586 stmt.setInt(3, a.productGroupId); 587 stmt.setString(4, a.unit); 588 stmt.setDouble(5, a.price); 589 stmt.setInt(6, a.stock); 590 stmt.setInt(7, a.orderedUnits); 591 stmt.setInt(8, a.minimumStock); 592 stmt.setInt(9, a.articleId); 593 stmt.execute(); 594 } 595 conn.commit(); 596 } 597 catch (Throwable t) 598 { 599 actualRetries++; 600 if (actualRetries <= maxRetries) 601 { 602 logger.error("error during db operations:", t); 603 try 604 { 605 conn.close(); 606 } 607 catch (Throwable ignored) 608 { 609 } 610 updateExistingArticles(); 611 } 612 else 613 { 614 logger.error("retry count exceeded!"); 615 fail(t.getMessage()); 616 } 617 } 618 finally 619 { 620 try 621 { 622 conn.close(); 623 } 624 catch (Throwable ignored) 625 { 626 } 627 } 628 long stop = System.currentTimeMillis(); 629 logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec"); 630 631 } 632 633 644 public void testBenchmark() 645 { 646 try 647 { 648 logger.info("Test for native JDBC"); 649 for (int i = 0; i < iterations; i++) 650 { 651 logger.info(""); 652 653 insertNewArticles(); 655 656 updateExistingArticles(); 658 659 readArticles(); 661 662 readArticles(); 663 664 readArticlesByCursor(); 666 667 deleteArticles(); 669 } 670 } 671 catch (Throwable t) 672 { 673 logger.error(t); 674 fail(t.getMessage()); 675 } 676 } 677 678 } 679 | Popular Tags |