Advertisement
Guest User

Untitled

a guest
May 31st, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.io.*;
  6.  
  7. public class IsolationBreach {
  8. public static Connection conn;
  9. static Scanner sc = new Scanner(System.in);
  10. static String dbuser = "di18";
  11. static String passwd = "QbhrEtut";
  12.  
  13. public static void main (String[] args) {
  14. try {
  15. Class.forName("org.postgresql.Driver");
  16.  
  17. try {
  18. conn = DriverManager.getConnection(
  19. "jdbc:postgresql://castle.ewi.utwente.nl/"+dbuser,
  20. dbuser, passwd);
  21. conn.setAutoCommit(true);
  22. conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
  23.  
  24. // Preliminaries
  25. System.out.println("Isolation Breach test");
  26. System.out.println();
  27. double chg = Double.parseDouble(args[0]);
  28. System.out.println("Amount to be transferred : "+chg);
  29. System.out.println();
  30. System.out.println("Initialize accounts");
  31. updateAccount("A",1000);
  32. updateAccount("B",2000);
  33. System.out.println();
  34.  
  35. // Now start the scenario
  36. transferMoney(chg);
  37.  
  38. // Finalize
  39. conn.setAutoCommit(true);
  40. conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
  41. System.out.println();
  42. System.out.println("Final state in database");
  43. readAccount("A");
  44. readAccount("B");
  45. conn.close();
  46. } catch(SQLException e) {
  47. System.err.println("Oops: " + e.getMessage() );
  48. System.err.println("SQLState: " + e.getSQLState() );
  49. }
  50. }
  51. catch (ClassNotFoundException e) {
  52. System.err.println("JDBC driver not loaded");
  53. }
  54. }
  55.  
  56. public static void waitForKey(String msg) {
  57. System.out.print(msg+" ... Press ENTER");
  58. // On Windows a return produces two characters, i.e., it does
  59. // two steps. Read while available().
  60. sc.nextLine();
  61. // Reads everything until the ENTER
  62. // We do not use System.in.read() because on Windows a
  63. // return/enter produces TWO characters
  64. }
  65.  
  66. public static void transferMoney(double chg) {
  67. // waitForKey("a:=read A");
  68. // double a=readAccount("A");
  69. // System.out.println();
  70. // waitForKey("b:=read B");
  71. // double b=readAccount("B");
  72. // System.out.println();
  73. // waitForKey("put "+(a-chg)+" in A");
  74. // updateAccount("A",a-chg);
  75. // System.out.println();
  76. // waitForKey("put "+(b+chg)+" in B");
  77. // updateAccount("B",b+chg);
  78.  
  79. updateAccountEdited("A", -chg);
  80. updateAccountEdited("B", chg);
  81. }
  82.  
  83. public static double readAccount(String acc) {
  84. double amount=0;
  85. String query="SELECT amount From account WHERE name='"+acc+"'";
  86. try {
  87. Statement st = conn.createStatement();
  88. System.out.println("Query: "+query);
  89. ResultSet rs = st.executeQuery(query);
  90. while (rs.next())
  91. {
  92. amount=rs.getDouble("amount");
  93. System.out.println("Result for account "+acc+" : "+amount);
  94. }
  95. rs.close();
  96. st.close();
  97. } catch(SQLException e) {
  98. System.err.println("Oops: " + e.getMessage() );
  99. System.err.println("SQLState: " + e.getSQLState() );
  100. }
  101. return amount;
  102. }
  103.  
  104. public static void updateAccount(String acc, double amount) {
  105. String query="UPDATE account SET amount="+amount+"WHERE name='"+acc+"'";
  106. // String query="UPDATE account SET amount= (SELECT account.amount FROM account WHERE name = '" + acc + "')+ " + amount + " WHERE name='"+acc+"'";
  107.  
  108. try {
  109. Statement st = conn.createStatement();
  110. System.out.println("Query: "+query);
  111. st.executeUpdate(query);
  112. System.out.println("Done");
  113. st.close();
  114. } catch(SQLException e) {
  115. System.err.println("Oops: " + e.getMessage() );
  116. System.err.println("SQLState: " + e.getSQLState() );
  117. }
  118. }
  119. public static void updateAccountEdited(String acc, double amount) {
  120. // String query="UPDATE account SET amount="+amount+"WHERE name='"+acc+"'";
  121. String query="UPDATE account SET amount= (SELECT account.amount FROM account WHERE name = '" + acc + "')+ " + amount + " WHERE name='"+acc+"'";
  122.  
  123. try {
  124. Statement st = conn.createStatement();
  125. System.out.println("Query: "+query);
  126. st.executeUpdate(query);
  127. System.out.println("Done");
  128. st.close();
  129. } catch(SQLException e) {
  130. System.err.println("Oops: " + e.getMessage() );
  131. System.err.println("SQLState: " + e.getSQLState() );
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement