Refactored new code for #23

This commit is contained in:
Butzlabben 2019-04-22 16:49:50 +02:00
parent 0358a634b2
commit 85a6c0a7c9
5 changed files with 5 additions and 15 deletions

View File

@ -88,7 +88,7 @@ public class PlayerPositions {
} }
} }
public void checkTables() { private void checkTables() {
String tableName = PluginConfig.getTableName(); String tableName = PluginConfig.getTableName();
try { try {
PreparedStatement ps = util.prepareStatement("CREATE TABLE IF NOT EXISTS " + tableName + PreparedStatement ps = util.prepareStatement("CREATE TABLE IF NOT EXISTS " + tableName +

View File

@ -3,8 +3,8 @@ package de.butzlabben.world.util.database;
import java.sql.*; import java.sql.*;
public abstract class DatabaseConnection implements DatabaseUtil { public abstract class DatabaseConnection implements DatabaseUtil {
protected Connection connection; Connection connection;
protected Object lock = new Object(); final Object lock = new Object();
public void close() { public void close() {
synchronized (lock) { synchronized (lock) {
@ -44,12 +44,4 @@ public abstract class DatabaseConnection implements DatabaseUtil {
return ps.executeUpdate(); return ps.executeUpdate();
} }
} }
public Connection getConnection() throws SQLException {
synchronized (lock) {
if (connection == null || connection.isClosed())
connect();
return connection;
}
}
} }

View File

@ -1,6 +1,5 @@
package de.butzlabben.world.util.database; package de.butzlabben.world.util.database;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -9,7 +8,6 @@ public interface DatabaseUtil {
ResultSet executeQuery(PreparedStatement preparedStatement) throws SQLException; ResultSet executeQuery(PreparedStatement preparedStatement) throws SQLException;
int executeUpdate(PreparedStatement preparedStatement) throws SQLException; int executeUpdate(PreparedStatement preparedStatement) throws SQLException;
public Connection getConnection() throws SQLException;
PreparedStatement prepareStatement(String sql) throws SQLException; PreparedStatement prepareStatement(String sql) throws SQLException;
void close(); void close();
void connect(); void connect();

View File

@ -6,7 +6,7 @@ import java.sql.*;
public class MysqlConnection extends DatabaseConnection { public class MysqlConnection extends DatabaseConnection {
public void connect(String host, String database, String port, String user, String password) { private void connect(String host, String database, String port, String user, String password) {
synchronized (lock) { synchronized (lock) {
try { try {
Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver");

View File

@ -6,7 +6,7 @@ import java.sql.*;
public class SqliteConnection extends DatabaseConnection { public class SqliteConnection extends DatabaseConnection {
public void connect(String file) { private void connect(String file) {
synchronized (lock) { synchronized (lock) {
try { try {
Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver");