Initial jdbi setup
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -32,6 +32,7 @@
|
||||
<jcip-annotations.version>1.0</jcip-annotations.version> <!-- Annotations for concurrency documentation -->
|
||||
<HikariCP.version>5.0.1</HikariCP.version> <!-- Database connection pool -->
|
||||
<mysql-connector-java.version>8.0.30</mysql-connector-java.version> <!-- MySQL JDBC driver -->
|
||||
<jdbi-version>3.35.0</jdbi-version> <!-- Convenience wrapper around JDBC -->
|
||||
<junit.version>5.9.0</junit.version> <!-- Unit test -->
|
||||
<mockito.version>4.7.0</mockito.version> <!-- Unit test -->
|
||||
</properties>
|
||||
@@ -59,6 +60,12 @@
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql-connector-java.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jdbi</groupId>
|
||||
<artifactId>jdbi3-core</artifactId>
|
||||
<version>${jdbi-version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Networking -->
|
||||
<dependency>
|
||||
|
||||
@@ -3,9 +3,12 @@ package tools;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import config.YamlConfig;
|
||||
import org.jdbi.v3.core.Handle;
|
||||
import org.jdbi.v3.core.Jdbi;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Duration;
|
||||
@@ -21,15 +24,24 @@ import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
public class DatabaseConnection {
|
||||
private static final Logger log = LoggerFactory.getLogger(DatabaseConnection.class);
|
||||
private static HikariDataSource dataSource;
|
||||
private static Jdbi jdbi;
|
||||
|
||||
public static Connection getConnection() throws SQLException {
|
||||
if (dataSource == null) {
|
||||
throw new IllegalStateException("Unable to get connection from uninitialized connection pool");
|
||||
throw new IllegalStateException("Unable to get connection - connection pool is uninitialized");
|
||||
}
|
||||
|
||||
return dataSource.getConnection();
|
||||
}
|
||||
|
||||
public static Handle getHandle() {
|
||||
if (jdbi == null) {
|
||||
throw new IllegalStateException("Unable to get handle - connection pool is uninitialized");
|
||||
}
|
||||
|
||||
return jdbi.open();
|
||||
}
|
||||
|
||||
private static String getDbUrl() {
|
||||
// Environment variables override what's defined in the config file
|
||||
// This feature is used for the Docker support
|
||||
@@ -73,6 +85,7 @@ public class DatabaseConnection {
|
||||
Instant initStart = Instant.now();
|
||||
try {
|
||||
dataSource = new HikariDataSource(config);
|
||||
initializeJdbi(dataSource);
|
||||
long initDuration = Duration.between(initStart, Instant.now()).toMillis();
|
||||
log.info("Connection pool initialized in {} ms", initDuration);
|
||||
return true;
|
||||
@@ -84,4 +97,9 @@ public class DatabaseConnection {
|
||||
// Timed out - failed to initialize
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void initializeJdbi(DataSource dataSource) {
|
||||
// TODO: configure row mappers
|
||||
jdbi = Jdbi.create(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user