From 7944751cd530a79041d8f95472fbb9875454a165 Mon Sep 17 00:00:00 2001 From: Jesper Larsson Date: Thu, 15 Jan 2026 22:42:28 +0100 Subject: [PATCH 1/2] native sql statement to setup table Signed-off-by: Jesper Larsson --- src/main/java/org/example/App.java | 1 + src/main/java/org/example/MyPod.java | 1 + .../example/logging/LoggingConnection.java | 29 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/main/java/org/example/App.java b/src/main/java/org/example/App.java index b1ff66f3..a9a75af1 100644 --- a/src/main/java/org/example/App.java +++ b/src/main/java/org/example/App.java @@ -1,6 +1,7 @@ package org.example; import javafx.application.Application; +import org.example.logging.LoggingConnection; public class App { public static void main(String[] args) { diff --git a/src/main/java/org/example/MyPod.java b/src/main/java/org/example/MyPod.java index 202fe057..92b0b646 100644 --- a/src/main/java/org/example/MyPod.java +++ b/src/main/java/org/example/MyPod.java @@ -32,6 +32,7 @@ import org.example.entity.DBObject; import org.example.entity.Playlist; import org.example.entity.Song; +import org.example.logging.LoggingConnection; import org.example.repo.SongRepository; import org.example.repo.AlbumRepository; import org.example.repo.ArtistRepository; diff --git a/src/main/java/org/example/logging/LoggingConnection.java b/src/main/java/org/example/logging/LoggingConnection.java index 5f937cf4..47305489 100644 --- a/src/main/java/org/example/logging/LoggingConnection.java +++ b/src/main/java/org/example/logging/LoggingConnection.java @@ -5,12 +5,15 @@ 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 boolean started = false; + static { config.setJdbcUrl( "jdbc:mysql://localhost:3306/myPodDB" ); config.setUsername( "user" ); @@ -23,7 +26,33 @@ public class LoggingConnection { private LoggingConnection() {} + public 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 { + Statement stmt = ds.getConnection().createStatement(); + + stmt.executeUpdate(createTableSQL); + + started = true; + } catch (SQLException e) { + throw new RuntimeException(e); + } + + + } + public static Connection getConnection() throws SQLException { + if(!started){ + setupLoggingTable(); + } return ds.getConnection(); } } From e10447670a6839ce37a74b6d74835fa1fceb9e6c Mon Sep 17 00:00:00 2001 From: Jesper Larsson Date: Fri, 16 Jan 2026 11:38:03 +0100 Subject: [PATCH 2/2] rabbit feedback Signed-off-by: Jesper Larsson --- src/main/java/org/example/App.java | 1 - src/main/java/org/example/MyPod.java | 1 - .../org/example/logging/LoggingConnection.java | 15 ++++++++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/example/App.java b/src/main/java/org/example/App.java index a9a75af1..b1ff66f3 100644 --- a/src/main/java/org/example/App.java +++ b/src/main/java/org/example/App.java @@ -1,7 +1,6 @@ package org.example; import javafx.application.Application; -import org.example.logging.LoggingConnection; public class App { public static void main(String[] args) { diff --git a/src/main/java/org/example/MyPod.java b/src/main/java/org/example/MyPod.java index 92b0b646..202fe057 100644 --- a/src/main/java/org/example/MyPod.java +++ b/src/main/java/org/example/MyPod.java @@ -32,7 +32,6 @@ import org.example.entity.DBObject; import org.example.entity.Playlist; import org.example.entity.Song; -import org.example.logging.LoggingConnection; import org.example.repo.SongRepository; import org.example.repo.AlbumRepository; import org.example.repo.ArtistRepository; diff --git a/src/main/java/org/example/logging/LoggingConnection.java b/src/main/java/org/example/logging/LoggingConnection.java index 47305489..de64b268 100644 --- a/src/main/java/org/example/logging/LoggingConnection.java +++ b/src/main/java/org/example/logging/LoggingConnection.java @@ -12,7 +12,8 @@ public class LoggingConnection { private static HikariConfig config = new HikariConfig(); private static HikariDataSource ds; - private static boolean started = false; + private static volatile boolean started = false; + private static final Object lock = new Object(); static { config.setJdbcUrl( "jdbc:mysql://localhost:3306/myPodDB" ); @@ -26,7 +27,7 @@ public class LoggingConnection { private LoggingConnection() {} - public static void setupLoggingTable() { + 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," + @@ -36,8 +37,8 @@ public static void setupLoggingTable() { "PRIMARY KEY (id)" + ")"; - try { - Statement stmt = ds.getConnection().createStatement(); + try (Connection conn = ds.getConnection(); + Statement stmt = conn.createStatement()){ stmt.executeUpdate(createTableSQL); @@ -51,7 +52,11 @@ public static void setupLoggingTable() { public static Connection getConnection() throws SQLException { if(!started){ - setupLoggingTable(); + synchronized (lock){ + if(!started){ + setupLoggingTable(); + } + } } return ds.getConnection(); }