Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- private static final String JDBC_URL = "jdbc:mysql://localhost:3306/test?user=root&password=root";
- private static final class SQLTask implements Callable<Void> {
- private final Connection connection;
- private String data;
- private String table;
- public SQLTask(Connection connection) {
- this.connection = connection;
- }
- public void setTable(String table) {
- this.table = table;
- }
- public void setData(String data) {
- this.data = data;
- }
- @Override
- public Void call() throws SQLException {
- try (Statement statement = connection.createStatement()) {
- statement.executeUpdate(String.format("INSERT INTO `%s` (data) VALUES ('%s');", table, data));
- }
- return null;
- }
- }
- public static void main(String[] args) throws SQLException, InterruptedException {
- int threads = 3;
- List<Connection> connections = getConnections(threads);
- Map<String, String> tableData = getTableData(threads);
- List<SQLTask> tasks = getTasks(threads, connections);
- setData(tableData, tasks);
- try {
- runTasks(tasks);
- commitConnections(connections);
- } catch (ExecutionException ex) {
- rollbackConnections(connections);
- } finally {
- closeConnections(connections);
- }
- }
- private static List<Connection> getConnections(int threads) throws SQLException {
- List<Connection> connections = new ArrayList<>();
- for (int i = 0; i < threads; i++) {
- Connection connection = DriverManager.getConnection(JDBC_URL);
- connection.setAutoCommit(false);
- connections.add(connection);
- }
- return connections;
- }
- private static Map<String, String> getTableData(int threads) {
- Map<String, String> tableData = new HashMap<>();
- for (int i = 1; i <= threads; i++)
- tableData.put("test" + i, "a");
- return tableData;
- }
- private static List<SQLTask> getTasks(int threads, List<Connection> connections) {
- List<SQLTask> tasks = new ArrayList<>();
- for (int i = 0; i < threads; i++)
- tasks.add(new SQLTask(connections.get(i)));
- return tasks;
- }
- private static void setData(Map<String, String> tableData, List<SQLTask> tasks) {
- Iterator<Entry<String, String>> i = tableData.entrySet().iterator();
- Iterator<SQLTask> j = tasks.iterator();
- while (i.hasNext()) {
- Entry<String, String> entry = i.next();
- SQLTask task = j.next();
- task.setTable(entry.getKey());
- task.setData(entry.getValue());
- }
- }
- private static void runTasks(List<SQLTask> tasks) throws ExecutionException, InterruptedException {
- ExecutorService executorService = Executors.newFixedThreadPool(tasks.size());
- List<Future<Void>> futures = executorService.invokeAll(tasks);
- executorService.shutdown();
- for (Future<Void> future : futures)
- future.get();
- }
- private static void commitConnections(List<Connection> connections) throws SQLException {
- for (Connection connection : connections)
- connection.commit();
- }
- private static void rollbackConnections(List<Connection> connections) throws SQLException {
- for (Connection connection : connections)
- connection.rollback();
- }
- private static void closeConnections(List<Connection> connections) throws SQLException {
- for (Connection connection : connections)
- connection.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement