Advertisement
Guest User

U14

a guest
May 30th, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.sql.*;
  3.  
  4. public class Main {
  5. public static void main(String[] args) throws SQLException {
  6. initializeTable();
  7. TranA a = new TranA();
  8. TranB b = new TranB();
  9. a.start();
  10. b.start();
  11. }
  12. private static void initializeTable() throws SQLException {
  13. Connection c = DbContext.getConnectionA();
  14. c.setAutoCommit(true);
  15. Statement s = c.createStatement();
  16. s.execute("DROP TABLE IF EXISTS datas");
  17. System.out.println("wtf");
  18. s.execute("CREATE TABLE datas (type integer, value numeric)");
  19. s.execute("INSERT INTO datas VALUES (1, 10), (1, 20), (2, 100), (2,200)");
  20. s.close();
  21. c.setAutoCommit(false);
  22. }
  23. }
  24.  
  25. class DbContext {
  26. public static Connection connectionA;
  27. public static Connection connectionB;
  28.  
  29. public static Connection getConnectionA() throws SQLException {
  30. if (connectionA == null) {
  31. connectionA = DriverManager.getConnection(
  32. "jdbc:postgresql://db.dai.fmph.uniba.sk/playground",
  33. "gajdosech2@uniba.sk",
  34. "datakyseliny");
  35. connectionA.setAutoCommit(false);
  36. }
  37. return connectionA;
  38. }
  39.  
  40. public static Connection getConnectionB() throws SQLException {
  41. if (connectionB == null) {
  42. connectionB = DriverManager.getConnection(
  43. "jdbc:postgresql://db.dai.fmph.uniba.sk/playground",
  44. "gajdosech2@uniba.sk",
  45. "datakyseliny");
  46. connectionB.setAutoCommit(false);
  47. }
  48. return connectionB;
  49. }
  50. }
  51.  
  52. class TranB extends Thread {
  53. int count = 0;
  54. int countInserts = 0;
  55. int serializationErrorCounts = 0;
  56.  
  57. @Override
  58. public void run() {
  59. while (countInserts < 1000) {
  60. count++;
  61. System.out.println("B");
  62. try {
  63. Connection c = DbContext.getConnectionB();
  64. c.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
  65. Statement s = c.createStatement();
  66. ResultSet r = s.executeQuery("SELECT SUM(value)::NUMERIC AS y FROM datas WHERE type = 2");
  67. r.next();
  68. BigDecimal y = r.getBigDecimal("y");
  69. s.execute("INSERT INTO datas (type,value) VALUES (1," + y + ")");
  70. c.commit();
  71. countInserts++;
  72. } catch (SQLException e) {
  73. if (e.getSQLState().equals("40001"))
  74. serializationErrorCounts++;
  75. e.printStackTrace();
  76. }
  77. }
  78. System.out.println("Transaction B has run totally: " + count + " times");
  79. System.out.println("Transaction B had " + serializationErrorCounts + " serialization errors");
  80. }
  81. }
  82.  
  83. class TranA extends Thread {
  84. int count = 0;
  85. int countInserts = 0;
  86. int serializationErrorCounts = 0;
  87.  
  88. @Override
  89. public void run() {
  90. while (countInserts < 1000) {
  91. count++;
  92. System.out.println("A");
  93. try {
  94. Connection c = DbContext.getConnectionA();
  95. c.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
  96. Statement s = c.createStatement();
  97. ResultSet r = s.executeQuery("SELECT SUM(value)::NUMERIC AS x FROM datas WHERE type = 1");
  98. r.next();
  99. BigDecimal x = r.getBigDecimal("x");
  100. s.execute("INSERT INTO datas (type,value) VALUES (2," + x + ")");
  101. c.commit();
  102. countInserts++;
  103. } catch (SQLException e) {
  104. if (e.getSQLState().equals("40001"))
  105. serializationErrorCounts++;
  106. e.printStackTrace();
  107. }
  108. }
  109. System.out.println("Transaction A has run totally: " + count + " times");
  110. System.out.println("Transaction A had " + serializationErrorCounts + " serialization errors");
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement