-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLoggingConnection.java
More file actions
63 lines (49 loc) · 1.95 KB
/
LoggingConnection.java
File metadata and controls
63 lines (49 loc) · 1.95 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package org.example.logging;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class LoggingConnection {
private static HikariConfig config = new HikariConfig();
private static HikariDataSource ds;
private static volatile boolean started = false;
private static final Object lock = new Object();
static {
config.setJdbcUrl( "jdbc:mysql://localhost:3306/myPodDB" );
config.setUsername( "user" );
config.setPassword( "pass" );
config.addDataSourceProperty( "cachePrepStmts" , "true" );
config.addDataSourceProperty( "prepStmtCacheSize" , "250" );
config.addDataSourceProperty( "prepStmtCacheSqlLimit" , "2048" );
ds = new HikariDataSource( config );
}
private LoggingConnection() {}
private static void setupLoggingTable() {
String createTableSQL = "CREATE TABLE IF NOT EXISTS app_logs (" +
"id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT," +
"level VARCHAR(50) NOT NULL," +
"message TEXT NOT NULL," +
"error_details TEXT NULL," +
"timestamp DATETIME NOT NULL," +
"PRIMARY KEY (id)" +
")";
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement()){
stmt.executeUpdate(createTableSQL);
started = true;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws SQLException {
if(!started){
synchronized (lock){
if(!started){
setupLoggingTable();
}
}
}
return ds.getConnection();
}
}