From ebce4cd0f0add9bd8d28320014b5291f9aeddaa0 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 9 Apr 2019 18:37:23 +0200 Subject: [PATCH] + database now uses a pooled connection Signed-off-by: Johannes Theiner --- src/main/java/de/hsel/itech/db/Database.java | 22 +++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index f624cb6..f5267a1 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -2,8 +2,12 @@ package de.hsel.itech.db; import de.hsel.itech.config.Configuration; import org.jetbrains.annotations.Nullable; +import org.mariadb.jdbc.MariaDbPoolDataSource; -import java.sql.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; /** * @@ -14,16 +18,24 @@ import java.sql.*; */ public class Database { - public Database() { + private MariaDbPoolDataSource dataSource; + public Database() { + Configuration config = Configuration.get(); + dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname() + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase()); + try { + dataSource.setUser(config.getDatabase().getUsername()); + dataSource.setPassword(config.getDatabase().getPassword()); + dataSource.initialize(); + } catch (SQLException e) { + e.printStackTrace(); + } } @Nullable private Connection getConnection() { - Configuration config = Configuration.get(); try { - return DriverManager.getConnection("jdbc:mariadb://" + config.getDatabase().getHostname() + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase(), config.getDatabase().getUsername(), config.getDatabase().getPassword()); - + return dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); }