Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.sql.*;
- public class Main {
- public static void main(String[] args) throws SQLException {
- initializeTable();
- TranA a = new TranA();
- TranB b = new TranB();
- a.start();
- b.start();
- }
- private static void initializeTable() throws SQLException {
- Connection c = DbContext.getConnectionA();
- c.setAutoCommit(true);
- Statement s = c.createStatement();
- s.execute("DROP TABLE IF EXISTS datas");
- System.out.println("wtf");
- s.execute("CREATE TABLE datas (type integer, value numeric)");
- s.execute("INSERT INTO datas VALUES (1, 10), (1, 20), (2, 100), (2,200)");
- s.close();
- c.setAutoCommit(false);
- }
- }
- class DbContext {
- public static Connection connectionA;
- public static Connection connectionB;
- public static Connection getConnectionA() throws SQLException {
- if (connectionA == null) {
- connectionA = DriverManager.getConnection(
- "jdbc:postgresql://db.dai.fmph.uniba.sk/playground",
- "gajdosech2@uniba.sk",
- "datakyseliny");
- connectionA.setAutoCommit(false);
- }
- return connectionA;
- }
- public static Connection getConnectionB() throws SQLException {
- if (connectionB == null) {
- connectionB = DriverManager.getConnection(
- "jdbc:postgresql://db.dai.fmph.uniba.sk/playground",
- "gajdosech2@uniba.sk",
- "datakyseliny");
- connectionB.setAutoCommit(false);
- }
- return connectionB;
- }
- }
- class TranB extends Thread {
- int count = 0;
- int countInserts = 0;
- int serializationErrorCounts = 0;
- @Override
- public void run() {
- while (countInserts < 1000) {
- count++;
- System.out.println("B");
- try {
- Connection c = DbContext.getConnectionB();
- c.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
- Statement s = c.createStatement();
- ResultSet r = s.executeQuery("SELECT SUM(value)::NUMERIC AS y FROM datas WHERE type = 2");
- r.next();
- BigDecimal y = r.getBigDecimal("y");
- s.execute("INSERT INTO datas (type,value) VALUES (1," + y + ")");
- c.commit();
- countInserts++;
- } catch (SQLException e) {
- if (e.getSQLState().equals("40001"))
- serializationErrorCounts++;
- e.printStackTrace();
- }
- }
- System.out.println("Transaction B has run totally: " + count + " times");
- System.out.println("Transaction B had " + serializationErrorCounts + " serialization errors");
- }
- }
- class TranA extends Thread {
- int count = 0;
- int countInserts = 0;
- int serializationErrorCounts = 0;
- @Override
- public void run() {
- while (countInserts < 1000) {
- count++;
- System.out.println("A");
- try {
- Connection c = DbContext.getConnectionA();
- c.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
- Statement s = c.createStatement();
- ResultSet r = s.executeQuery("SELECT SUM(value)::NUMERIC AS x FROM datas WHERE type = 1");
- r.next();
- BigDecimal x = r.getBigDecimal("x");
- s.execute("INSERT INTO datas (type,value) VALUES (2," + x + ")");
- c.commit();
- countInserts++;
- } catch (SQLException e) {
- if (e.getSQLState().equals("40001"))
- serializationErrorCounts++;
- e.printStackTrace();
- }
- }
- System.out.println("Transaction A has run totally: " + count + " times");
- System.out.println("Transaction A had " + serializationErrorCounts + " serialization errors");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement