Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/java/org/example/logging/LoggingConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

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" );
Expand All @@ -23,7 +27,37 @@ public class LoggingConnection {

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();
}
}