+ database now uses a pooled connection

Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
Johannes Theiner 2019-04-09 18:37:23 +02:00
parent 4b6510d230
commit ebce4cd0f0
1 changed files with 17 additions and 5 deletions

View File

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