Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.83 KB | None | 0 0
  1. package test;
  2.  
  3. import java.sql.*;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;
  8.  
  9. public class App implements Runnable {
  10.  
  11.     private final String DB_URL  = "jdbc:mysql://localhost:3306/test?user=root&password=dev";
  12.  
  13.     private final ExecutorService executorService = Executors.newFixedThreadPool(2);
  14.  
  15.     public static void main(String[] args) {
  16.         new App().run();
  17.     }
  18.  
  19.     protected Connection createConnection() throws SQLException {
  20.         return DriverManager.getConnection(DB_URL);
  21.     }
  22.  
  23.     public void run() {
  24.         Connection connection = null;
  25.         try {
  26.             connection = createConnection();
  27.  
  28.             System.out.println("--------------------------------------");
  29.             System.out.println("connection.getAutoCommit() = " + connection.getAutoCommit());
  30.             System.out.println("connection.getClientInfo() = " + connection.getClientInfo());
  31.             System.out.println("connection.getMetaData() = " + connection.getMetaData());
  32.             System.out.println("connection.getCatalog() = " + connection.getCatalog());
  33.             System.out.println("connection.getTransactionIsolation() = " + connection.getTransactionIsolation());
  34.             System.out.println("--------------------------------------");
  35.  
  36.             Integer[] isolationTypes = checkTransactionIsolation(connection);
  37.             checkSavePoints(connection);
  38.  
  39.             for (Integer transactionType: isolationTypes) {
  40.                 transactionTest(transactionType);
  41.             }
  42.             executorService.shutdown();
  43.         } catch (SQLException e) {
  44.             System.err.println(e);
  45.         } finally {
  46.             try {
  47.                 if (connection != null)
  48.                     connection.close();
  49.             } catch (SQLException e) {
  50.                 System.err.println(e);
  51.             }
  52.         }
  53.     }
  54.  
  55.     private void checkSavePoints(Connection connection) {
  56.         try {
  57.             connection.setAutoCommit(false);
  58.             Statement statement = connection.createStatement();
  59.             Savepoint savepoint = connection.setSavepoint();
  60.             try {
  61.                 statement.execute("insert into users(id, firstName, lastName) values(0, 'maxim', 'borkunov')");
  62.                 statement.execute("insert into users(id, firstName, lastName) values(0, 'alexey', 'voroshin')");
  63.             } catch (SQLException e) { // duplicate id exception
  64.                 System.err.println(e);
  65.                 connection.rollback(savepoint);
  66.                 connection.releaseSavepoint(savepoint);
  67.             } finally {
  68.                 connection.commit();
  69.             }
  70.         } catch (SQLException e) {
  71.             System.err.println(e);
  72.         }
  73.     }
  74.  
  75.     private Integer[] checkTransactionIsolation(Connection connection) {
  76.         Set<Integer> available = new HashSet<Integer>();
  77.         try {
  78.             int defaultTransactionIsolation = connection.getTransactionIsolation();
  79.             for (int i : new int[] {0, 1, 2, 4, 8}) {
  80.                 try {
  81.                     connection.setTransactionIsolation(i);
  82.                     available.add(i);
  83.                 } catch (SQLException e) {
  84.                     System.out.println(e);
  85.                 }
  86.             }
  87.             System.out.println("available types = " + available);
  88.             connection.setTransactionIsolation(defaultTransactionIsolation);
  89.             return available.toArray(new Integer[] {available.size()});
  90.         } catch (SQLException e) {
  91.             System.err.println(e);
  92.         }
  93.         return new Integer[0];
  94.     }
  95.  
  96.  
  97.     private void transactionTest(final int transactionIsolation) {
  98.         System.out.println("transactionIsolation = " + transactionIsolation);
  99.  
  100.         int clients = 2;
  101.         final boolean[] finished = new boolean[clients];
  102.  
  103.         for (int i = 0; i < 2; i++) {
  104.             executorService.execute(new Runnable() {
  105.                 private int number;
  106.                 Runnable setClientNumber(int number) {
  107.                     this.number = number;
  108.                     return this;
  109.                 }
  110.                 public void run() {
  111.                     Connection connection = null;
  112.                     try {
  113.                         connection = createConnection();
  114.                         connection.setTransactionIsolation(transactionIsolation);
  115.                         connection.setAutoCommit(false);
  116.  
  117.                         Statement statement = connection.createStatement();
  118.                         showUsers(statement);
  119.                         System.out.println("#" + number + " ----- ");
  120.                         statement.execute("insert into users(firstName, lastName) values('hello', 'bye')");
  121.                         try {
  122.                             Thread.sleep(Math.round(Math.random() * 1000));
  123.                         } catch (InterruptedException e) {
  124.                             System.err.println(e);
  125.                         }
  126.                         showUsers(statement);
  127.                         statement.execute("update users set firstName = '" + this.number + "'");
  128.                         connection.commit();
  129.                     } catch (SQLException e) {
  130.                         System.err.println(transactionIsolation);
  131.                         e.printStackTrace();
  132.                     } finally {
  133.                         finished[number] = true;
  134.                         try {
  135.                             if (connection != null)
  136.                                 connection.close();
  137.                         } catch (SQLException e) {
  138.                             System.err.println(e);
  139.                         }
  140.                     }
  141.                 }
  142.  
  143.                 private void showUsers(Statement statement) throws SQLException {
  144.                     if (statement.execute("select * from users")) {
  145.                         ResultSet result = statement.getResultSet();
  146.                         while (result.next()) {
  147.                             int id = result.getInt("id");
  148.                             String firstName = result.getString("firstName");
  149.                             System.out.println("Client #" + this.number + " id:" + id + ", firstName: " + firstName);
  150.                         }
  151.                     }
  152.                 }
  153.             }.setClientNumber(i));
  154.         }
  155.  
  156.         boolean exit;
  157.  
  158.         do  {
  159.             exit = true;
  160.             for (int i = 0; i < clients; i++) {
  161.                 if (!finished[i]) {
  162.                     exit = false;
  163.                 }
  164.             }
  165.             try {
  166.                 Thread.sleep(10);
  167.             } catch (InterruptedException ignored) {}
  168.         } while(!exit);
  169.  
  170.         System.out.println("test finished, transaction isolation: " + transactionIsolation);
  171.         System.out.println("\n\n");
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement