|
1
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
|
2
|
|
|
3
|
import java.sql.Connection;
|
|
4
|
import java.sql.DriverManager;
|
|
5
|
import java.sql.PreparedStatement;
|
|
6
|
import java.util.concurrent.*;
|
|
7
|
|
|
8
|
public class Test {
|
|
9
|
|
|
10
|
private static final int THREADS = 500;
|
|
11
|
private static final int ITERATIONS = 100;
|
|
12
|
|
|
13
|
public static void main(String[] args) throws Exception {
|
|
14
|
System.out.println("Starting H2 in-memory database tests...");
|
|
15
|
|
|
16
|
System.out.println("\n--- Test 1: Without Connection Pool ---");
|
|
17
|
testWithoutPool();
|
|
18
|
|
|
19
|
System.out.println("\n--- Test 2: With C3P0 Connection Pool ---");
|
|
20
|
testWithC3P0Pool();
|
|
21
|
}
|
|
22
|
|
|
23
|
private static void testWithoutPool() throws Exception {
|
|
24
|
|
|
25
|
String url = "jdbc:h2:mem:test1;DB_CLOSE_DELAY=-1";
|
|
26
|
long start = System.currentTimeMillis();
|
|
27
|
|
|
28
|
ExecutorService executor = Executors.newFixedThreadPool(THREADS);
|
|
29
|
CountDownLatch latch = new CountDownLatch(THREADS * ITERATIONS);
|
|
30
|
|
|
31
|
for (int it = 0; it < ITERATIONS; it++) {
|
|
32
|
for (int t = 0; t < THREADS; t++) {
|
|
33
|
|
|
34
|
executor.submit(() -> {
|
|
35
|
try (Connection conn = DriverManager.getConnection(url);
|
|
36
|
PreparedStatement ps = conn.prepareStatement("SELECT 1")) {
|
|
37
|
|
|
38
|
ps.execute();
|
|
39
|
|
|
40
|
} catch (Exception e) {
|
|
41
|
e.printStackTrace();
|
|
42
|
} finally {
|
|
43
|
latch.countDown();
|
|
44
|
}
|
|
45
|
});
|
|
46
|
|
|
47
|
}
|
|
48
|
}
|
|
49
|
|
|
50
|
latch.await();
|
|
51
|
executor.shutdown();
|
|
52
|
|
|
53
|
long duration = System.currentTimeMillis() - start;
|
|
54
|
System.out.println("Test 1 (No Pool) completed in " + duration + " ms");
|
|
55
|
}
|
|
56
|
|
|
57
|
private static void testWithC3P0Pool() throws Exception {
|
|
58
|
|
|
59
|
String url = "jdbc:h2:mem:test2;DB_CLOSE_DELAY=-1";
|
|
60
|
long start = System.currentTimeMillis();
|
|
61
|
|
|
62
|
ComboPooledDataSource cpds = new ComboPooledDataSource();
|
|
63
|
cpds.setDriverClass("org.h2.Driver");
|
|
64
|
cpds.setJdbcUrl(url);
|
|
65
|
cpds.setUser("sa");
|
|
66
|
cpds.setPassword("");
|
|
67
|
|
|
68
|
cpds.setMaxPoolSize(THREADS);
|
|
69
|
cpds.setMinPoolSize(2);
|
|
70
|
cpds.setAcquireIncrement(2);
|
|
71
|
cpds.setCheckoutTimeout(5000);
|
|
72
|
|
|
73
|
ExecutorService executor = Executors.newFixedThreadPool(THREADS);
|
|
74
|
CountDownLatch latch = new CountDownLatch(THREADS * ITERATIONS);
|
|
75
|
|
|
76
|
for (int it = 0; it < ITERATIONS; it++) {
|
|
77
|
for (int t = 0; t < THREADS; t++) {
|
|
78
|
|
|
79
|
executor.submit(() -> {
|
|
80
|
try (Connection conn = cpds.getConnection();
|
|
81
|
PreparedStatement ps = conn.prepareStatement("SELECT 1")) {
|
|
82
|
|
|
83
|
ps.execute();
|
|
84
|
|
|
85
|
} catch (Exception e) {
|
|
86
|
e.printStackTrace();
|
|
87
|
} finally {
|
|
88
|
latch.countDown();
|
|
89
|
}
|
|
90
|
});
|
|
91
|
|
|
92
|
}
|
|
93
|
}
|
|
94
|
|
|
95
|
latch.await();
|
|
96
|
executor.shutdown();
|
|
97
|
cpds.close();
|
|
98
|
|
|
99
|
long duration = System.currentTimeMillis() - start;
|
|
100
|
System.out.println("Test 2 (C3P0 Pool) completed in " + duration + " ms");
|
|
101
|
}
|
|
102
|
}
|