-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathDemoApp.java
More file actions
35 lines (31 loc) · 1.16 KB
/
DemoApp.java
File metadata and controls
35 lines (31 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.bobocode;
import com.bobocode.pool.PooledDataSource;
import lombok.SneakyThrows;
import javax.sql.DataSource;
import java.sql.SQLException;
public class DemoApp {
@SneakyThrows
public static void main(String[] args) {
var dataSource = initializePooledDataSource();
var total = 0.0;
var start = System.nanoTime();
for (int i = 0; i < 500; i++) {
try (var connection = dataSource.getConnection()) {
connection.setAutoCommit(false);
try (var statement = connection.createStatement()) {
var rs = statement.executeQuery("select random() from products");
rs.next();
total += rs.getDouble(1);
}
connection.rollback();
}
}
System.out.println((System.nanoTime() - start) / 1000_000 + " ms");
System.out.println(total);
}
private static DataSource initializePooledDataSource() throws SQLException, InterruptedException {
return new PooledDataSource("jdbc:postgresql://localhost:5432/postgres",
"postgres",
"root");
}
}