Advertisement
Guest User

Untitled

a guest
Dec 7th, 2011
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. public class Main {
  2.  
  3.   private static final String JDBC_URL = "jdbc:mysql://localhost:3306/test?user=root&password=root";
  4.  
  5.   private static final class SQLTask implements Callable<Void> {
  6.  
  7.     private final Connection connection;
  8.  
  9.     private String data;
  10.     private String table;
  11.  
  12.     public SQLTask(Connection connection) {
  13.       this.connection = connection;
  14.     }
  15.  
  16.     public void setTable(String table) {
  17.       this.table = table;
  18.     }
  19.  
  20.     public void setData(String data) {
  21.       this.data = data;
  22.     }
  23.  
  24.     @Override
  25.     public Void call() throws SQLException {
  26.       try (Statement statement = connection.createStatement()) {
  27.         statement.executeUpdate(String.format("INSERT INTO `%s` (data) VALUES  ('%s');", table, data));
  28.       }
  29.       return null;
  30.     }
  31.   }
  32.  
  33.   public static void main(String[] args) throws SQLException, InterruptedException {
  34.     int threads = 3;
  35.     List<Connection> connections = getConnections(threads);
  36.     Map<String, String> tableData = getTableData(threads);
  37.     List<SQLTask> tasks = getTasks(threads, connections);
  38.     setData(tableData, tasks);
  39.     try {
  40.       runTasks(tasks);
  41.       commitConnections(connections);
  42.     } catch (ExecutionException ex) {
  43.       rollbackConnections(connections);
  44.     } finally {
  45.       closeConnections(connections);
  46.     }
  47.   }
  48.  
  49.   private static List<Connection> getConnections(int threads) throws SQLException {
  50.     List<Connection> connections = new ArrayList<>();
  51.     for (int i = 0; i < threads; i++) {
  52.       Connection connection = DriverManager.getConnection(JDBC_URL);
  53.       connection.setAutoCommit(false);
  54.       connections.add(connection);
  55.     }
  56.     return connections;
  57.   }
  58.  
  59.   private static Map<String, String> getTableData(int threads) {
  60.     Map<String, String> tableData = new HashMap<>();
  61.     for (int i = 1; i <= threads; i++)
  62.       tableData.put("test" + i, "a");
  63.     return tableData;
  64.   }
  65.  
  66.   private static List<SQLTask> getTasks(int threads, List<Connection> connections) {
  67.     List<SQLTask> tasks = new ArrayList<>();
  68.     for (int i = 0; i < threads; i++)
  69.       tasks.add(new SQLTask(connections.get(i)));
  70.     return tasks;
  71.   }
  72.  
  73.   private static void setData(Map<String, String> tableData, List<SQLTask> tasks) {
  74.     Iterator<Entry<String, String>> i = tableData.entrySet().iterator();
  75.     Iterator<SQLTask> j = tasks.iterator();
  76.     while (i.hasNext()) {
  77.       Entry<String, String> entry = i.next();
  78.       SQLTask task = j.next();
  79.       task.setTable(entry.getKey());
  80.       task.setData(entry.getValue());
  81.     }
  82.   }
  83.  
  84.   private static void runTasks(List<SQLTask> tasks) throws ExecutionException, InterruptedException {
  85.     ExecutorService executorService = Executors.newFixedThreadPool(tasks.size());
  86.     List<Future<Void>> futures = executorService.invokeAll(tasks);
  87.     executorService.shutdown();
  88.     for (Future<Void> future : futures)
  89.       future.get();
  90.   }
  91.  
  92.   private static void commitConnections(List<Connection> connections) throws SQLException {
  93.     for (Connection connection : connections)
  94.       connection.commit();
  95.   }
  96.  
  97.   private static void rollbackConnections(List<Connection> connections) throws SQLException {
  98.     for (Connection connection : connections)
  99.       connection.rollback();
  100.   }
  101.  
  102.   private static void closeConnections(List<Connection> connections) throws SQLException {
  103.     for (Connection connection : connections)
  104.       connection.close();
  105.   }
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement