Advertisement
Guest User

Untitled

a guest
May 16th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. package csci3030u.a3;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5.  
  6.  
  7. import com.mysql.jdbc.Connection;
  8. import com.mysql.jdbc.Statement;
  9.  
  10. /*
  11. * Creates 2 connections to database and by using 2 threads, accesses the database at the same time.
  12. * One thread will be inserting values and deleting them, the other thread will query the database
  13. */
  14. public class ConcurrentAccess {
  15.  
  16. public static void main(String[] args) {
  17. String host=null, user=null, password=null, database=null;
  18. int trials=0;
  19.  
  20. //collect arguments and store them in variables
  21. if(args.length !=5){
  22. System.out.println("Not enough args");
  23. System.exit(0);
  24. }else{
  25. host=args[0];
  26. user=args[1];
  27. password=args[2];
  28. database=args[3];
  29. trials = Integer.parseInt(args[4]);
  30. }
  31.  
  32. //connect to database twice
  33. try {
  34. Class.forName("com.mysql.jdbc.Driver");
  35. } catch(Exception e) {
  36. System.out.println("Driver not loaded."); System.exit(0);
  37. }
  38.  
  39. try {
  40.  
  41. //initialze connection 1 and 2
  42. Connection con1 = (Connection) DriverManager.getConnection(
  43. host+database, user, password);
  44.  
  45. Connection con2 = (Connection) DriverManager.getConnection(
  46. host+database, user, password);
  47.  
  48. //create table if not exists
  49. Statement stmt = (Statement) con1.createStatement();
  50. stmt.executeUpdate("CREATE TABLE IF NOT EXISTS A3(number INTEGER)");
  51.  
  52. //setup threads that will run connection
  53. InsertThread t1= new InsertThread(trials,con2);
  54. QueryThread t2= new QueryThread(trials,con1 );
  55.  
  56. //run threads
  57. t1.run();
  58. t2.run();
  59.  
  60. con1.close();
  61. con2.close();
  62. } catch(Exception e) {
  63. System.out.println(e.getMessage());
  64. System.out.println("Driver not loaded."); System.exit(0);
  65. }
  66. }
  67. }
  68.  
  69. /*
  70. * Thread used in max min querying
  71. */
  72. class QueryThread implements Runnable {
  73. int trials;
  74. Connection con;
  75. int collisions;
  76.  
  77. QueryThread(int trials, java.sql.Connection dbCon) {
  78. this.trials=trials;
  79. this.con=(Connection) dbCon;
  80. this.collisions=0;
  81. }
  82.  
  83. /**
  84. * Separate thread for database Queries
  85. */
  86. public void run() {
  87. try {
  88. con.setAutoCommit(true);
  89. for(int i=0; i<trials; i++){
  90. if(AccessTable.queryMaxMin(con)){
  91. collisions++;
  92. }
  93. }
  94.  
  95. System.out.println("Number of collisions: " + collisions);
  96. } catch (SQLException e) {
  97. System.out.println(e.getMessage());
  98. return;
  99. }
  100. }
  101. }
  102.  
  103. /*
  104. * Thread used in value inserting
  105. */
  106. class InsertThread implements Runnable {
  107. int trials;
  108. Connection con;
  109.  
  110. InsertThread(int trials, java.sql.Connection dbCon) {
  111. this.con=(Connection) dbCon;
  112. }
  113.  
  114. /**
  115. * Separate thread for database Queries
  116. */
  117. public void run() {
  118. try {
  119. con.setAutoCommit(true);
  120.  
  121. for(int i=0; i<trials; i++){
  122. AccessTable.insertValues(con, 1, 10);
  123. }
  124.  
  125. } catch (SQLException e) {
  126. System.out.println(e.getMessage());
  127. return;
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement