Project

General

Profile

TestAsyncForEach.java

Artur Școlnic, 06/20/2025 07:46 AM

Download (3.47 KB)

 
1
import com.mchange.v2.c3p0.ComboPooledDataSource;
2

    
3
import java.sql.*;
4
import java.util.*;
5
import java.util.concurrent.*;
6

    
7
public class Test {
8

    
9
    private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();
10

    
11
    static {
12
        try {
13
            dataSource.setDriverClass("org.postgresql.Driver");
14
            dataSource.setJdbcUrl("jdbc:postgresql://localhost:5433/fwd");
15
            dataSource.setUser("fwd_admin");
16
            dataSource.setPassword("admin");
17
            dataSource.setMinPoolSize(10);
18
            dataSource.setMaxPoolSize(100);
19
        } catch (Exception e) {
20
            throw new RuntimeException("Error setting up datasource", e);
21
        }
22
    }
23
    private static void async(String query) throws SQLException, InterruptedException {
24
        ExecutorService executor = Executors.newSingleThreadExecutor();
25

    
26
        try (Connection conn = dataSource.getConnection()) {
27
            conn.setAutoCommit(false);
28
            try (Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
29
                stmt.setFetchSize(50);
30
                try (ResultSet rs = stmt.executeQuery(query)) {
31
                    ResultSetMetaData meta = rs.getMetaData();
32
                    int columnCount = meta.getColumnCount();
33
                    while (rs.next()) {
34
                        Map<String, Object> row = extractRow(rs, meta, columnCount);
35
                        // Submit business logic for execution in single thread (order preserved)
36
                        executor.submit(() -> {
37
                            try {
38
                                executeBusinessLogic(row);
39
                            } catch (InterruptedException e) {
40
                                throw new RuntimeException(e);
41
                            }
42
                        });
43
                    }
44
                }
45
            }
46
        }
47
        // Shut down and wait for all tasks to complete
48
        executor.shutdown();
49
        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
50

    
51
    }
52

    
53
    private static void sync(String query) throws SQLException, InterruptedException, ExecutionException {
54
        try (Connection conn = dataSource.getConnection();
55
             Statement stmt = conn.createStatement()) {
56

    
57
            try (ResultSet rs = stmt.executeQuery(query)) {
58
                ResultSetMetaData meta = rs.getMetaData();
59
                int columnCount = meta.getColumnCount();
60

    
61
                while (rs.next()) {
62
                    Map<String, Object> row = extractRow(rs, meta, columnCount);
63
                    executeBusinessLogic(row);
64
                }
65
            } catch (InterruptedException e) {
66
                throw new RuntimeException(e);
67
            }
68
        }
69
    }
70

    
71
    private static void executeBusinessLogic(Map<String, Object> row) throws InterruptedException {
72
        Thread.sleep(1);
73
    }
74

    
75
    private static Map<String, Object> extractRow(ResultSet rs, ResultSetMetaData meta, int columnCount) throws SQLException {
76
        Map<String, Object> row = new HashMap<>();
77
        for (int i = 1; i <= columnCount; i++) {
78
            row.put(meta.getColumnLabel(i), rs.getObject(i));
79
        }
80
        return row;
81
    }
82

    
83
    public static void main(String[] args) throws SQLException, ExecutionException, InterruptedException {
84
        double sync = System.currentTimeMillis();
85
        sync("SELECT *, pg_sleep(0.000003) FROM employees;");
86
        sync = System.currentTimeMillis() - sync;
87
        System.out.println(sync);
88

    
89
    }
90
}