1 package org.hsqldb.test; 2 3 13 import java.io.FileOutputStream ; 14 import java.io.PrintStream ; 15 import java.sql.Connection ; 16 import java.sql.DriverManager ; 17 import java.sql.PreparedStatement ; 18 import java.sql.ResultSet ; 19 import java.sql.SQLException ; 20 import java.sql.Statement ; 21 import java.util.Enumeration ; 22 import java.util.Vector ; 23 24 class JDBCBench { 25 26 27 public static int tps = 1; 28 public static int nbranches = 1; 29 public static int ntellers = 10; 30 public static int naccounts = 100000; 31 public static int nhistory = 864000; 32 public static final int TELLER = 0; 33 public static final int BRANCH = 1; 34 public static final int ACCOUNT = 2; 35 int failed_transactions = 0; 36 int transaction_count = 0; 37 static int n_clients = 10; 38 static int n_txn_per_client = 10; 39 long start_time = 0; 40 static boolean transactions = true; 41 static boolean prepared_stmt = false; 42 static String tableExtension = ""; 43 static String createExtension = ""; 44 static String ShutdownCommand = ""; 45 static String startupCommand = ""; 46 static PrintStream TabFile = null; 47 static boolean verbose = false; 48 MemoryWatcherThread MemoryWatcher; 49 50 55 public static void main(String [] Args) { 56 57 String DriverName = ""; 58 String DBUrl = ""; 59 String DBUser = ""; 60 String DBPassword = ""; 61 boolean initialize_dataset = false; 62 63 for (int i = 0; i < Args.length; i++) { 64 if (Args[i].equals("-clients")) { 65 if (i + 1 < Args.length) { 66 i++; 67 68 n_clients = Integer.parseInt(Args[i]); 69 } 70 } else if (Args[i].equals("-driver")) { 71 if (i + 1 < Args.length) { 72 i++; 73 74 DriverName = Args[i]; 75 76 if (DriverName.equals( 77 "org.enhydra.instantdb.jdbc.idbDriver")) { 78 ShutdownCommand = "SHUTDOWN"; 79 } 80 81 if (DriverName.equals( 82 "com.borland.datastore.jdbc.DataStoreDriver")) {} 83 84 if (DriverName.equals("com.mckoi.JDBCDriver")) { 85 ShutdownCommand = "SHUTDOWN"; 86 } 87 88 if (DriverName.equals("org.hsqldb.jdbcDriver")) { 89 tableExtension = "CREATE CACHED TABLE "; 90 ShutdownCommand = "SHUTDOWN"; 91 startupCommand = ""; 92 } 93 } 94 } else if (Args[i].equals("-url")) { 95 if (i + 1 < Args.length) { 96 i++; 97 98 DBUrl = Args[i]; 99 } 100 } else if (Args[i].equals("-user")) { 101 if (i + 1 < Args.length) { 102 i++; 103 104 DBUser = Args[i]; 105 } 106 } else if (Args[i].equals("-tabfile")) { 107 if (i + 1 < Args.length) { 108 i++; 109 110 try { 111 FileOutputStream File = new FileOutputStream (Args[i]); 112 113 TabFile = new PrintStream (File); 114 } catch (Exception e) { 115 TabFile = null; 116 } 117 } 118 } else if (Args[i].equals("-password")) { 119 if (i + 1 < Args.length) { 120 i++; 121 122 DBPassword = Args[i]; 123 } 124 } else if (Args[i].equals("-tpc")) { 125 if (i + 1 < Args.length) { 126 i++; 127 128 n_txn_per_client = Integer.parseInt(Args[i]); 129 } 130 } else if (Args[i].equals("-init")) { 131 initialize_dataset = true; 132 } else if (Args[i].equals("-tps")) { 133 if (i + 1 < Args.length) { 134 i++; 135 136 tps = Integer.parseInt(Args[i]); 137 } 138 } else if (Args[i].equals("-v")) { 139 verbose = true; 140 } 141 } 142 143 if (DriverName.length() == 0 || DBUrl.length() == 0) { 144 System.out.println( 145 "usage: java JDBCBench -driver [driver_class_name] -url [url_to_db] -user [username] -password [password] [-v] [-init] [-tpc n] [-clients n]"); 146 System.out.println(); 147 System.out.println("-v verbose error messages"); 148 System.out.println("-init initialize the tables"); 149 System.out.println("-tpc transactions per client"); 150 System.out.println("-clients number of simultaneous clients"); 151 System.exit(-1); 152 } 153 154 System.out.println( 155 "*********************************************************"); 156 System.out.println( 157 "* JDBCBench v1.1 *"); 158 System.out.println( 159 "*********************************************************"); 160 System.out.println(); 161 System.out.println("Driver: " + DriverName); 162 System.out.println("URL:" + DBUrl); 163 System.out.println(); 164 System.out.println("Scale factor value: " + tps); 165 System.out.println("Number of clients: " + n_clients); 166 System.out.println("Number of transactions per client: " 167 + n_txn_per_client); 168 System.out.println(); 169 170 try { 171 Class.forName(DriverName); 172 173 JDBCBench Me = new JDBCBench(DBUrl, DBUser, DBPassword, 174 initialize_dataset); 175 } catch (Exception E) { 176 System.out.println(E.getMessage()); 177 E.printStackTrace(); 178 } 179 } 180 181 public JDBCBench(String url, String user, String password, boolean init) { 182 183 Vector vClient = new Vector (); 184 Thread Client = null; 185 Enumeration e = null; 186 Connection guardian = null; 187 188 try { 189 if (init) { 190 System.out.println("Start: " 191 + (new java.util.Date ()).toString()); 192 System.out.print("Initializing dataset..."); 193 createDatabase(url, user, password); 194 System.out.println("done.\n"); 195 System.out.println("Complete: " 196 + (new java.util.Date ()).toString()); 197 } 198 199 guardian = connect(url, user, password); 200 201 if (startupCommand.length() != 0) { 202 Statement statement = guardian.createStatement(); 203 204 statement.execute(startupCommand); 205 statement.close(); 206 } 207 208 System.out.println("* Starting Benchmark Run *"); 209 210 MemoryWatcher = new MemoryWatcherThread(); 211 212 MemoryWatcher.start(); 213 214 transactions = true; 215 prepared_stmt = false; 216 start_time = System.currentTimeMillis(); 217 218 for (int i = 0; i < n_clients; i++) { 219 Client = new ClientThread(n_txn_per_client, url, user, 220 password); 221 222 Client.start(); 223 vClient.addElement(Client); 224 } 225 226 229 e = vClient.elements(); 230 231 while (e.hasMoreElements()) { 232 Client = (Thread ) e.nextElement(); 233 234 Client.join(); 235 } 236 237 vClient.removeAllElements(); 238 reportDone(); 239 checkSums(guardian); 240 241 if (!transactions) { 243 throw new Exception ("end after one round"); 244 } 245 246 transactions = true; 247 prepared_stmt = false; 248 start_time = System.currentTimeMillis(); 249 250 for (int i = 0; i < n_clients; i++) { 251 Client = new ClientThread(n_txn_per_client, url, user, 252 password); 253 254 Client.start(); 255 vClient.addElement(Client); 256 } 257 258 261 e = vClient.elements(); 262 263 while (e.hasMoreElements()) { 264 Client = (Thread ) e.nextElement(); 265 266 Client.join(); 267 } 268 269 vClient.removeAllElements(); 270 reportDone(); 271 checkSums(guardian); 272 273 transactions = true; 274 prepared_stmt = true; 275 start_time = System.currentTimeMillis(); 276 277 for (int i = 0; i < n_clients; i++) { 278 Client = new ClientThread(n_txn_per_client, url, user, 279 password); 280 281 Client.start(); 282 vClient.addElement(Client); 283 } 284 285 288 e = vClient.elements(); 289 290 while (e.hasMoreElements()) { 291 Client = (Thread ) e.nextElement(); 292 293 Client.join(); 294 } 295 296 vClient.removeAllElements(); 297 reportDone(); 298 checkSums(guardian); 299 300 transactions = true; 301 prepared_stmt = true; 302 start_time = System.currentTimeMillis(); 303 304 for (int i = 0; i < n_clients; i++) { 305 Client = new ClientThread(n_txn_per_client, url, user, 306 password); 307 308 Client.start(); 309 vClient.addElement(Client); 310 } 311 312 315 e = vClient.elements(); 316 317 while (e.hasMoreElements()) { 318 Client = (Thread ) e.nextElement(); 319 320 Client.join(); 321 } 322 323 vClient.removeAllElements(); 324 reportDone(); 325 checkSums(guardian); 326 } catch (Exception E) { 327 System.out.println(E.getMessage()); 328 E.printStackTrace(); 329 } finally { 330 MemoryWatcher.end(); 331 332 try { 333 MemoryWatcher.join(); 334 335 if (ShutdownCommand.length() > 0) { 336 Statement Stmt = guardian.createStatement(); 337 338 Stmt.execute(ShutdownCommand); 339 Stmt.close(); 340 connectClose(guardian); 341 } 342 343 if (TabFile != null) { 344 TabFile.close(); 345 } 346 } catch (Exception E1) {} 347 348 } 350 } 351 352 public void reportDone() { 353 354 long end_time = System.currentTimeMillis(); 355 double completion_time = ((double) end_time - (double) start_time) 356 / 1000; 357 358 if (TabFile != null) { 359 TabFile.print(tps + ";" + n_clients + ";" + n_txn_per_client 360 + ";"); 361 } 362 363 System.out.println("\n* Benchmark Report *"); 364 System.out.print("* Featuring "); 365 366 if (prepared_stmt) { 367 System.out.print("<prepared statements> "); 368 369 if (TabFile != null) { 370 TabFile.print("<prepared statements>;"); 371 } 372 } else { 373 System.out.print("<direct queries> "); 374 375 if (TabFile != null) { 376 TabFile.print("<direct queries>;"); 377 } 378 } 379 380 if (transactions) { 381 System.out.print("<transactions> "); 382 383 if (TabFile != null) { 384 TabFile.print("<transactions>;"); 385 } 386 } else { 387 System.out.print("<auto-commit> "); 388 389 if (TabFile != null) { 390 TabFile.print("<auto-commit>;"); 391 } 392 } 393 394 System.out.println("\n--------------------"); 395 System.out.println("Time to execute " + transaction_count 396 + " transactions: " + completion_time 397 + " seconds."); 398 System.out.println("Max/Min memory usage: " + MemoryWatcher.max 399 + " / " + MemoryWatcher.min + " kb"); 400 System.out.println(failed_transactions + " / " + transaction_count 401 + " failed to complete."); 402 403 double rate = (transaction_count - failed_transactions) 404 / completion_time; 405 406 System.out.println("Transaction rate: " + rate + " txn/sec."); 407 408 if (TabFile != null) { 409 TabFile.print(MemoryWatcher.max + ";" + MemoryWatcher.min + ";" 410 + failed_transactions + ";" + rate + "\n"); 411 } 412 413 transaction_count = 0; 414 failed_transactions = 0; 415 416 MemoryWatcher.reset(); 417 } 418 419 public synchronized void incrementTransactionCount() { 420 transaction_count++; 421 } 422 423 public synchronized void incrementFailedTransactionCount() { 424 failed_transactions++; 425 } 426 427 void createDatabase(String url, String user, 428 String password) throws Exception { 429 430 Connection Conn = connect(url, user, password); 431 ; 432 String s = Conn.getMetaData().getDatabaseProductName(); 433 434 System.out.println("DBMS: " + s); 435 436 transactions = true; 437 438 if (transactions) { 439 try { 440 Conn.setAutoCommit(false); 441 System.out.println("In transaction mode"); 442 } catch (SQLException Etrxn) { 443 transactions = false; 444 } 445 } 446 447 try { 448 int accountsnb = 0; 449 Statement Stmt = Conn.createStatement(); 450 String Query; 451 452 Query = "SELECT count(*) "; 453 Query += "FROM accounts"; 454 455 ResultSet RS = Stmt.executeQuery(Query); 456 457 Stmt.clearWarnings(); 458 459 while (RS.next()) { 460 accountsnb = RS.getInt(1); 461 } 462 463 if (transactions) { 464 Conn.commit(); 465 } 466 467 Stmt.close(); 468 469 if (accountsnb == (naccounts * tps)) { 470 System.out.println("Already initialized"); 471 connectClose(Conn); 472 473 return; 474 } 475 } catch (Exception E) {} 476 477 System.out.println("Drop old tables if they exist"); 478 479 try { 480 Statement Stmt = Conn.createStatement(); 481 String Query; 482 483 Query = "DROP TABLE history"; 484 485 Stmt.execute(Query); 486 Stmt.clearWarnings(); 487 488 Query = "DROP TABLE accounts"; 489 490 Stmt.execute(Query); 491 Stmt.clearWarnings(); 492 493 Query = "DROP TABLE tellers"; 494 495 Stmt.execute(Query); 496 Stmt.clearWarnings(); 497 498 Query = "DROP TABLE branches"; 499 500 Stmt.execute(Query); 501 Stmt.clearWarnings(); 502 503 if (transactions) { 504 Conn.commit(); 505 } 506 507 Stmt.close(); 508 } catch (Exception E) {} 509 510 System.out.println("Creates tables"); 511 512 try { 513 Statement Stmt = Conn.createStatement(); 514 String Query; 515 516 if (tableExtension.length() > 0) { 517 Query = tableExtension + " branches ("; 518 } else { 519 Query = "CREATE TABLE branches ("; 520 } 521 522 Query += "Bid INTEGER NOT NULL PRIMARY KEY, "; 523 Query += "Bbalance INTEGER,"; 524 Query += "filler CHAR(88))"; 525 526 if (createExtension.length() > 0) { 527 Query += createExtension; 528 } 529 530 Stmt.execute(Query); 531 Stmt.clearWarnings(); 532 533 if (tableExtension.length() > 0) { 534 Query = tableExtension + " tellers ("; 535 } else { 536 Query = "CREATE TABLE tellers ("; 537 } 538 539 Query += "Tid INTEGER NOT NULL PRIMARY KEY,"; 540 Query += "Bid INTEGER,"; 541 Query += "Tbalance INTEGER,"; 542 Query += "filler CHAR(84))"; 543 544 if (createExtension.length() > 0) { 545 Query += createExtension; 546 } 547 548 Stmt.execute(Query); 549 Stmt.clearWarnings(); 550 551 if (tableExtension.length() > 0) { 552 Query = tableExtension + " accounts ("; 553 } else { 554 Query = "CREATE TABLE accounts ("; 555 } 556 557 Query += "Aid INTEGER NOT NULL PRIMARY KEY, "; 558 Query += "Bid INTEGER, "; 559 Query += "Abalance INTEGER, "; 560 Query += "filler CHAR(84))"; 561 562 if (createExtension.length() > 0) { 563 Query += createExtension; 564 } 565 566 Stmt.execute(Query); 567 Stmt.clearWarnings(); 568 569 if (tableExtension.length() > 0) { 570 Query = tableExtension + " history ("; 571 } else { 572 Query = "CREATE TABLE history ("; 573 } 574 575 Query += "Tid INTEGER, "; 576 Query += "Bid INTEGER, "; 577 Query += "Aid INTEGER, "; 578 Query += "delta INTEGER, "; 579 Query += "tstime TIMESTAMP, "; 580 Query += "filler CHAR(22))"; 581 582 if (createExtension.length() > 0) { 583 Query += createExtension; 584 } 585 586 Stmt.execute(Query); 587 Stmt.clearWarnings(); 588 Stmt.execute("SET TABLE ACCOUNTS SOURCE \"ACCOUNTS.TXT\""); 589 Stmt.execute("SET TABLE BRANCHES SOURCE \"BBRANCHES.TXT\""); 590 Stmt.execute("SET TABLE TELLERS SOURCE \"TELLERS.TXT\""); 591 Stmt.execute("SET TABLE HISTORY SOURCE \"HISTORY.TXT\""); 592 593 if (transactions) { 594 Conn.commit(); 595 } 596 597 Stmt.close(); 598 } catch (Exception E) { 599 System.out.println( 600 "Delete elements in table in case Drop didn't work"); 601 } 602 603 System.out.println( 604 "Delete elements in table in case Drop didn't work"); 605 606 try { 607 Statement Stmt = Conn.createStatement(); 608 String Query; 609 610 Query = "DELETE FROM history"; 611 612 Stmt.execute(Query); 613 Stmt.clearWarnings(); 614 615 Query = "DELETE FROM accounts"; 616 617 Stmt.execute(Query); 618 Stmt.clearWarnings(); 619 620 Query = "DELETE FROM tellers"; 621 622 Stmt.execute(Query); 623 Stmt.clearWarnings(); 624 625 Query = "DELETE FROM branches"; 626 627 Stmt.execute(Query); 628 Stmt.clearWarnings(); 629 630 if (transactions) { 631 Conn.commit(); 632 } 633 634 639 PreparedStatement pstmt = null; 640 641 prepared_stmt = true; 642 643 if (prepared_stmt) { 644 try { 645 Query = "INSERT INTO branches(Bid,Bbalance) VALUES (?,0)"; 646 pstmt = Conn.prepareStatement(Query); 647 648 System.out.println("Using prepared statements"); 649 } catch (SQLException Epstmt) { 650 pstmt = null; 651 prepared_stmt = false; 652 } 653 } 654 655 System.out.println("Insert data in branches table"); 656 657 for (int i = 0; i < nbranches * tps; i++) { 658 if (prepared_stmt) { 659 pstmt.setInt(1, i); 660 pstmt.executeUpdate(); 661 pstmt.clearWarnings(); 662 } else { 663 Query = "INSERT INTO branches(Bid,Bbalance) VALUES (" + i 664 + ",0)"; 665 666 Stmt.executeUpdate(Query); 667 } 668 669 if ((i % 100 == 0) && (transactions)) { 670 Conn.commit(); 671 } 672 } 673 674 if (prepared_stmt) { 675 pstmt.close(); 676 } 677 678 if (transactions) { 679 Conn.commit(); 680 } 681 682 if (prepared_stmt) { 683 Query = 684 "INSERT INTO tellers(Tid,Bid,Tbalance) VALUES (?,?,0)"; 685 pstmt = Conn.prepareStatement(Query); 686 } 687 688 System.out.println("Insert data in tellers table"); 689 690 for (int i = 0; i < ntellers * tps; i++) { 691 if (prepared_stmt) { 692 pstmt.setInt(1, i); 693 pstmt.setInt(2, i / ntellers); 694 pstmt.executeUpdate(); 695 pstmt.clearWarnings(); 696 } else { 697 Query = "INSERT INTO tellers(Tid,Bid,Tbalance) VALUES (" 698 + i + "," + i / ntellers + ",0)"; 699 700 Stmt.executeUpdate(Query); 701 } 702 703 if ((i % 100 == 0) && (transactions)) { 704 Conn.commit(); 705 } 706 } 707 708 if (prepared_stmt) { 709 pstmt.close(); 710 } 711 712 if (transactions) { 713 Conn.commit(); 714 } 715 716 if (prepared_stmt) { 717 Query = 718 "INSERT INTO accounts(Aid,Bid,Abalance) VALUES (?,?,0)"; 719 pstmt = Conn.prepareStatement(Query); 720 } 721 722 System.out.println("Insert data in accounts table"); 723 724 for (int i = 0; i < naccounts * tps; i++) { 725 if (prepared_stmt) { 726 pstmt.setInt(1, i); 727 pstmt.setInt(2, i / naccounts); 728 pstmt.executeUpdate(); 729 pstmt.clearWarnings(); 730 } else { 731 Query = "INSERT INTO accounts(Aid,Bid,Abalance) VALUES (" 732 + i + "," + i / naccounts + ",0)"; 733 734 Stmt.executeUpdate(Query); 735 } 736 737 if ((i % 10000 == 0) && (transactions)) { 738 Conn.commit(); 739 } 740 741 if ((i > 0) && ((i % 10000) == 0)) { 742 System.out.println("\t" + i + "\t records inserted"); 743 } 744 } 745 746 if (prepared_stmt) { 747 pstmt.close(); 748 } 749 750 if (transactions) { 751 Conn.commit(); 752 } 753 754 System.out.println("\t" + (naccounts * tps) 755 + "\t records inserted"); 756 757 Stmt.execute(ShutdownCommand); 759 Stmt.close(); 760 } catch (Exception E) { 761 System.out.println(E.getMessage()); 762 E.printStackTrace(); 763 } 764 765 connectClose(Conn); 766 } 767 768 public static int getRandomInt(int lo, int hi) { 769 770 int ret = 0; 771 772 ret = (int) (Math.random() * (hi - lo + 1)); 773 ret += lo; 774 775 return ret; 776 } 777 778 public static int getRandomID(int type) { 779 780 int min = 0, 781 max = 0; 782 783 switch (type) { 784 785 case TELLER : 786 max = ntellers * tps - 1; 787 break; 788 789 case BRANCH : 790 max = nbranches * tps - 1; 791 break; 792 793 case ACCOUNT : 794 max = naccounts * tps - 1; 795 break; 796 } 797 798 return (getRandomInt(min, max)); 799 } 800 801 public static Connection connect(String DBUrl, String DBUser, 802 String DBPassword) { 803 804 try { 805 Connection conn = DriverManager.getConnection(DBUrl, DBUser, 806 DBPassword); 807 808 return conn; 809 } catch (Exception E) { 810 System.out.println(E.getMessage()); 811 E.printStackTrace(); 812 } 813 814 return null; 815 } 816 817 public static void connectClose(Connection c) { 818 819 if (c == null) { 820 return; 821 } 822 823 try { 824 c.close(); 825 } catch (Exception E) { 826 System.out.println(E.getMessage()); 827 E.printStackTrace(); 828 } 829 } 830 831 void checkSums(Connection conn) throws Exception { 832 833 Statement st1 = null; 834 ResultSet rs = null; 835 int bbalancesum; 836 int tbalancesum; 837 int abalancesum; 838 int deltasum; 839 840 try { 841 st1 = conn.createStatement(); 842 rs = st1.executeQuery("select sum(bbalance) from branches"); 843 844 rs.next(); 845 846 bbalancesum = rs.getInt(1); 847 848 rs.close(); 849 850 rs = st1.executeQuery("select sum(tbalance) from tellers"); 851 852 rs.next(); 853 854 tbalancesum = rs.getInt(1); 855 856 rs.close(); 857 858 rs = st1.executeQuery("select sum(abalance) from accounts"); 859 860 rs.next(); 861 862 abalancesum = rs.getInt(1); 863 864 rs.close(); 865 866 rs = st1.executeQuery("select sum(delta) from history"); 867 868 rs.next(); 869 870 deltasum = rs.getInt(1); 871 872 rs.close(); 873 874 rs = null; 875 876 st1.close(); 877 878 st1 = null; 879 880 if (abalancesum != bbalancesum || bbalancesum != tbalancesum 881 || tbalancesum != deltasum) { 882 System.out.println("sums don't match!"); 883 } else { 884 System.out.println("sums match!"); 885 } 886 887 System.out.println(abalancesum + " " + bbalancesum + " " 888 + tbalancesum + " " + deltasum); 889 } finally { 890 if (st1 != null) { 891 st1.close(); 892 } 893 } 894 } 895 896 class ClientThread extends Thread { 897 898 int ntrans = 0; 899 Connection Conn; 900 PreparedStatement pstmt1 = null; 901 PreparedStatement pstmt2 = null; 902 PreparedStatement pstmt3 = null; 903 PreparedStatement pstmt4 = null; 904 PreparedStatement pstmt5 = null; 905 906 public ClientThread(int number_of_txns, String url, String user, 907 String password) { 908 909 ntrans = number_of_txns; 910 Conn = connect(url, user, password); 911 912 if (Conn == null) { 913 return; 914 } 915 916 try { 917 if (transactions) { 918 Conn.setAutoCommit(false); 919 } 920 921 if (prepared_stmt) { 922 String Query; 923 924 Query = "UPDATE accounts "; 925 Query += "SET Abalance = Abalance + ? "; 926 Query += "WHERE Aid = ?"; 927 pstmt1 = Conn.prepareStatement(Query); 928 Query = "SELECT Abalance "; 929 Query += "FROM accounts "; 930 Query += "WHERE Aid = ?"; 931 pstmt2 = Conn.prepareStatement(Query); 932 Query = "UPDATE tellers "; 933 Query += "SET Tbalance = Tbalance + ? "; 934 Query += "WHERE Tid = ?"; 935 pstmt3 = Conn.prepareStatement(Query); 936 Query = "UPDATE branches "; 937 Query += "SET Bbalance = Bbalance + ? "; 938 Query += "WHERE Bid = ?"; 939 pstmt4 = Conn.prepareStatement(Query); 940 Query = "INSERT INTO history(Tid, Bid, Aid, delta) "; 941 Query += "VALUES (?,?,?,?)"; 942 pstmt5 = Conn.prepareStatement(Query); 943 } 944 } catch (Exception E) { 945 System.out.println(E.getMessage()); 946 E.printStackTrace(); 947 } 948 } 949 950 public void run() { 951 952 while (ntrans-- > 0) { 953 int account = JDBCBench.getRandomID(ACCOUNT); 954 int branch = JDBCBench.getRandomID(BRANCH); 955 int teller = JDBCBench.getRandomID(TELLER); 956 int delta = JDBCBench.getRandomInt(0, 1000); 957 958 doOne(branch, teller, account, delta); 959 incrementTransactionCount(); 960 } 961 962 if (prepared_stmt) { 963 try { 964 if (pstmt1 != null) { 965 pstmt1.close(); 966 } 967 968 if (pstmt2 != null) { 969 pstmt2.close(); 970 } 971 972 if (pstmt3 != null) { 973 pstmt3.close(); 974 } 975 976 if (pstmt4 != null) { 977 pstmt4.close(); 978 } 979 980 if (pstmt5 != null) { 981 pstmt5.close(); 982 } 983 } catch (Exception E) { 984 System.out.println(E.getMessage()); 985 E.printStackTrace(); 986 } 987 } 988 989 connectClose(Conn); 990 991 Conn = null; 992 } 993 994 997 int doOne(int bid, int tid, int aid, int delta) { 998 999 int aBalance = 0; 1000 1001 if (Conn == null) { 1002 incrementFailedTransactionCount(); 1003 1004 return 0; 1005 } 1006 1007 try { 1008 if (prepared_stmt) { 1009 pstmt1.setInt(1, delta); 1010 pstmt1.setInt(2, aid); 1011 pstmt1.executeUpdate(); 1012 pstmt1.clearWarnings(); 1013 pstmt2.setInt(1, aid); 1014 1015 ResultSet RS = pstmt2.executeQuery(); 1016 1017 pstmt2.clearWarnings(); 1018 1019 while (RS.next()) { 1020 aBalance = RS.getInt(1); 1021 } 1022 1023 pstmt3.setInt(1, delta); 1024 pstmt3.setInt(2, tid); 1025 pstmt3.executeUpdate(); 1026 pstmt3.clearWarnings(); 1027 pstmt4.setInt(1, delta); 1028 pstmt4.setInt(2, bid); 1029 pstmt4.executeUpdate(); 1030 pstmt4.clearWarnings(); 1031 pstmt5.setInt(1, tid); 1032 pstmt5.setInt(2, bid); 1033 pstmt5.setInt(3, aid); 1034 pstmt5.setInt(4, delta); 1035 pstmt5.executeUpdate(); 1036 pstmt5.clearWarnings(); 1037 } else { 1038 Statement Stmt = Conn.createStatement(); 1039 String Query = "UPDATE accounts "; 1040 1041 Query += "SET Abalance = Abalance + " + delta + " "; 1042 Query += "WHERE Aid = " + aid; 1043 1044 int res = Stmt.executeUpdate(Query); 1045 1046 Stmt.clearWarnings(); 1047 1048 Query = "SELECT Abalance "; 1049 Query += "FROM accounts "; 1050 Query += "WHERE Aid = " + aid; 1051 1052 ResultSet RS = Stmt.executeQuery(Query); 1053 1054 Stmt.clearWarnings(); 1055 1056 while (RS.next()) { 1057 aBalance = RS.getInt(1); 1058 } 1059 1060 Query = "UPDATE tellers "; 1061 Query += "SET Tbalance = Tbalance + " + delta + " "; 1062 Query += "WHERE Tid = " + tid; 1063 1064 Stmt.executeUpdate(Query); 1065 Stmt.clearWarnings(); 1066 1067 Query = "UPDATE branches "; 1068 Query += "SET Bbalance = Bbalance + " + delta + " "; 1069 Query += "WHERE Bid = " + bid; 1070 1071 Stmt.executeUpdate(Query); 1072 Stmt.clearWarnings(); 1073 1074 Query = "INSERT INTO history(Tid, Bid, Aid, delta) "; 1075 Query += "VALUES ("; 1076 Query += tid + ","; 1077 Query += bid + ","; 1078 Query += aid + ","; 1079 Query += delta + ")"; 1080 1081 Stmt.executeUpdate(Query); 1082 Stmt.clearWarnings(); 1083 Stmt.close(); 1084 } 1085 1086 if (transactions) { 1087 Conn.commit(); 1088 } 1089 1090 return aBalance; 1091 } catch (Exception E) { 1092 if (verbose) { 1093 System.out.println("Transaction failed: " 1094 + E.getMessage()); 1095 E.printStackTrace(); 1096 } 1097 1098 incrementFailedTransactionCount(); 1099 1100 if (transactions) { 1101 try { 1102 Conn.rollback(); 1103 } catch (SQLException E1) {} 1104 } 1105 } 1106 1107 return 0; 1108 } 1109 } 1110 1111 class MemoryWatcherThread extends Thread { 1112 1113 long min = 0; 1114 long max = 0; 1115 boolean keep_running = true; 1116 1117 public MemoryWatcherThread() { 1118 1119 this.reset(); 1120 1121 keep_running = true; 1122 } 1123 1124 public void reset() { 1125 1126 System.gc(); 1127 1128 long currentFree = Runtime.getRuntime().freeMemory(); 1129 long currentAlloc = Runtime.getRuntime().totalMemory(); 1130 1131 min = max = (currentAlloc - currentFree); 1132 } 1133 1134 public void end() { 1135 keep_running = false; 1136 } 1137 1138 public void run() { 1139 1140 while (keep_running) { 1141 long currentFree = Runtime.getRuntime().freeMemory(); 1142 long currentAlloc = Runtime.getRuntime().totalMemory(); 1143 long used = currentAlloc - currentFree; 1144 1145 if (used < min) { 1146 min = used; 1147 } 1148 1149 if (used > max) { 1150 max = used; 1151 } 1152 1153 try { 1154 sleep(100); 1155 } catch (InterruptedException E) {} 1156 } 1157 } 1158 } 1159} 1160 | Popular Tags |