First try on Connection Pool

This commit is contained in:
Fuzzlemann 2017-08-22 18:26:59 +02:00
parent 6f807c123d
commit 82da226edb
26 changed files with 289 additions and 305 deletions

View File

@ -31,6 +31,22 @@
<version>2.0.4</version>
<scope>compile</scope>
</dependency>
<!-- Connection Pool-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- SoftDepended Plugins-->
<dependency>
<groupId>com.djrapitops</groupId>

View File

@ -4,12 +4,9 @@
*/
package main.java.com.djrapitops.plan.data.handling;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.queue.processing.Processor;
import java.sql.SQLException;
/**
* Processor for queueing a Database Commit after changes.
*
@ -23,10 +20,10 @@ public class DBCommitProcessor extends Processor<Database> {
@Override
public void process() {
// TODO Prevent Commit during batch operations.
try {
object.commit();
/*try {
TODO FIX object.commit();
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}*/
}
}

View File

@ -1,10 +1,10 @@
package main.java.com.djrapitops.plan.data.listeners;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.handling.KillHandling;
import main.java.com.djrapitops.plan.data.handling.info.DeathInfo;
import main.java.com.djrapitops.plan.data.handling.info.KillInfo;
import main.java.com.djrapitops.plan.data.handling.player.DeathProcessor;
import main.java.com.djrapitops.plan.data.handling.player.KillProcessor;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.bukkit.Material;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
@ -45,7 +45,7 @@ public class PlanDeathEventListener implements Listener {
LivingEntity dead = event.getEntity();
if (dead instanceof Player) {
plugin.addToProcessQueue(new DeathInfo(dead.getUniqueId()));
plugin.addToProcessQueue(new DeathProcessor(dead.getUniqueId()));
}
EntityDamageEvent entityDamageEvent = dead.getLastDamageCause();
@ -69,7 +69,7 @@ public class PlanDeathEventListener implements Listener {
}
}
plugin.addToProcessQueue(new KillInfo(killer.getUniqueId(), time, dead, KillHandling.normalizeMaterialName(itemInHand)));
plugin.addToProcessQueue(new KillProcessor(killer.getUniqueId(), time, dead, normalizeMaterialName(itemInHand)));
return;
}
@ -86,7 +86,7 @@ public class PlanDeathEventListener implements Listener {
return;
}
plugin.addToProcessQueue(new KillInfo(owner.getUniqueId(), time, dead, "Wolf"));
plugin.addToProcessQueue(new KillProcessor(owner.getUniqueId(), time, dead, "Wolf"));
}
if (killerEntity instanceof Arrow) {
@ -100,8 +100,18 @@ public class PlanDeathEventListener implements Listener {
Player player = (Player) source;
plugin.addToProcessQueue(new KillInfo(player.getUniqueId(), time, dead, "Bow"));
plugin.addToProcessQueue(new KillProcessor(player.getUniqueId(), time, dead, "Bow"));
}
}
/**
* Normalizes a material name
*
* @param material The material
* @return The normalized material name
*/
private String normalizeMaterialName(Material material) {
return WordUtils.capitalizeFully(material.name(), '_').replace('_', ' ');
}
}

View File

@ -3,7 +3,9 @@ package main.java.com.djrapitops.plan.database;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.database.tables.*;
import org.apache.commons.dbcp2.BasicDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
@ -90,6 +92,8 @@ public abstract class Database {
*/
protected ServerTable serverTable;
protected BasicDataSource dataSource;
/**
* Super constructor.
*
@ -322,5 +326,9 @@ public abstract class Database {
return serverTable;
}
public abstract void commit() throws SQLException;
public BasicDataSource getDataSource() {
return dataSource;
}
public abstract void commit(Connection connection) throws SQLException;
}

View File

@ -1,15 +1,9 @@
package main.java.com.djrapitops.plan.database.databases;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg;
import org.apache.commons.dbcp2.BasicDataSource;
import org.bukkit.configuration.file.FileConfiguration;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author Rsl1122
*/
@ -25,30 +19,26 @@ public class MySQLDB extends SQLDB {
}
/**
* Creates a new connection to the database.
*
* @return the new Connection.
* Setups the {@link BasicDataSource}
*/
@Override
public Connection getNewConnection() {
public void setupDataSource() {
FileConfiguration config = plugin.getConfig();
try {
Class.forName("com.mysql.jdbc.Driver");
dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://" + config.getString("mysql.host") + ":" + config.getString("mysql.port") + "/"
+ config.getString("mysql.database")
+ "?rewriteBatchedStatements=true";
dataSource.setUrl("jdbc:mysql://" + config.getString("Database.MySQL.Host") + ":" + config.getString("Database.MySQL.Port") + "/"
+ config.getString("Database.MySQL.Database")
+ "?rewriteBatchedStatements=true");
return DriverManager.getConnection(url, config.getString("mysql.user"), config.getString("mysql.password"));
} catch (ClassNotFoundException | SQLException e) {
Log.error(Locale.get(Msg.ENABLE_FAIL_DB).parse(getConfigName(), e.getMessage()));
return null;
}
dataSource.setUsername(config.getString("Database.MySQL.User"));
dataSource.setPassword(config.getString("Database.MySQL.Password"));
dataSource.setMaxTotal(-1);
}
/**
* @return
* @return the name of the Database
*/
@Override
public String getName() {

View File

@ -1,13 +1,12 @@
package main.java.com.djrapitops.plan.database.databases;
import com.djrapitops.plugin.task.AbsRunnable;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.tables.*;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import org.apache.commons.dbcp2.BasicDataSource;
import java.sql.Connection;
import java.sql.SQLException;
@ -26,8 +25,6 @@ public abstract class SQLDB extends Database {
private final boolean supportsModification;
private final boolean usingMySQL;
private Connection connection;
/**
* @param plugin
* @param supportsModification
@ -37,7 +34,6 @@ public abstract class SQLDB extends Database {
this.supportsModification = supportsModification;
usingMySQL = getName().equals("MySQL");
serverTable = new ServerTable(this, usingMySQL);
usersTable = new UsersTable(this, usingMySQL);
sessionsTable = new SessionsTable(this, usingMySQL);
killsTable = new KillsTable(this, usingMySQL);
@ -49,31 +45,9 @@ public abstract class SQLDB extends Database {
securityTable = new SecurityTable(this, usingMySQL);
worldTable = new WorldTable(this, usingMySQL);
worldTimesTable = new WorldTimesTable(this, usingMySQL);
serverTable = new ServerTable(this, usingMySQL);
startConnectionPingTask();
}
/**
* Starts repeating Async task that maintains the Database connection.
*/
public void startConnectionPingTask() {
// Maintains Connection.
plugin.getRunnableFactory().createNew(new AbsRunnable("DBConnectionPingTask " + getName()) {
@Override
public void run() {
Statement statement = null;
try {
if (connection != null && !connection.isClosed()) {
statement = connection.createStatement();
statement.execute("/* ping */ SELECT 1");
}
} catch (SQLException e) {
connection = getNewConnection();
} finally {
MiscUtils.close(statement);
}
}
}).runTaskTimerAsynchronously(60L * 20L, 60L * 20L);
setupDataSource();
}
/**
@ -93,9 +67,11 @@ public abstract class SQLDB extends Database {
String benchName = "Init " + getConfigName();
Benchmark.start(benchName);
try {
if (!checkConnection()) {
System.out.println("SETUP DATABASES");
if (!setupDatabases()) {
return false;
}
System.out.println("CLEAN DATABASES");
clean();
return true;
} catch (SQLException e) {
@ -115,37 +91,29 @@ public abstract class SQLDB extends Database {
* @return Is the connection usable?
* @throws SQLException
*/
public boolean checkConnection() throws SQLException {
if (connection == null || connection.isClosed()) {
connection = getNewConnection();
public boolean setupDatabases() throws SQLException {
boolean newDatabase = isNewDatabase();
if (connection == null || connection.isClosed()) {
return false;
}
if (!versionTable.createTable()) {
Log.error("Failed to create table: " + versionTable.getTableName());
return false;
}
boolean newDatabase = isNewDatabase();
if (newDatabase) {
Log.info("New Database created.");
}
if (!versionTable.createTable()) {
Log.error("Failed to create table: " + versionTable.getTableName());
return false;
}
if (!createTables()) {
return false;
}
if (newDatabase) {
Log.info("New Database created.");
setVersion(8);
}
if (newDatabase || getVersion() < 8) {
setVersion(8);
}
if (!createTables()) {
return false;
}
if (!newDatabase && getVersion() < 8) {
setVersion(8);
}
try (Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE IF EXISTS plan_locations");
}
try (Statement statement = getConnection().createStatement()) {
statement.execute("DROP TABLE IF EXISTS plan_locations");
endTransaction(statement.getConnection());
}
return true;
}
@ -158,8 +126,10 @@ public abstract class SQLDB extends Database {
* @return true if successful.
*/
private boolean createTables() {
System.out.println("Create Tables");
Benchmark.start("Create tables");
for (Table table : getAllTables()) {
System.out.println("Create Table " + table.getTableName());
if (!table.createTable()) {
Log.error("Failed to create table: " + table.getTableName());
return false;
@ -188,7 +158,6 @@ public abstract class SQLDB extends Database {
commandUseTable, tpsTable, worldTable,
worldTimesTable, securityTable};
}
/**
* @return
*/
@ -201,18 +170,19 @@ public abstract class SQLDB extends Database {
}
/**
* @return
* Setups the {@link BasicDataSource}
*/
public abstract Connection getNewConnection();
public abstract void setupDataSource();
/**
* @throws SQLException
*/
@Override
public void close() throws SQLException {
if (connection != null) {
connection.close();
}
/*if (!dataSource.isClosed()) {
dataSource.close();
}*/
setStatus("Closed");
Log.logDebug("Database"); // Log remaining Debug info if present
}
@ -232,7 +202,6 @@ public abstract class SQLDB extends Database {
@Override
public void setVersion(int version) throws SQLException {
versionTable.setVersion(version);
commit();
}
/**
@ -268,25 +237,19 @@ public abstract class SQLDB extends Database {
Benchmark.start("Remove Account");
Log.debug("Database", "Removing Account: " + uuid);
try {
checkConnection();
setupDatabases();
} catch (Exception e) {
Log.toLog(this.getClass().getName(), e);
return false;
}
int userId = usersTable.getUserId(uuid);
boolean success = userId != -1
return userId != -1
&& ipsTable.removeUserIPs(userId)
&& nicknamesTable.removeUserNicknames(userId)
&& sessionsTable.removeUserSessions(userId)
&& killsTable.removeUserKillsAndVictims(userId)
&& worldTimesTable.removeUserWorldTimes(userId)
&& usersTable.removeUser(uuid);
if (success) {
commit();
} else {
rollback();
}
return success;
} finally {
Benchmark.stop("Database", "Remove Account");
setAvailable();
@ -300,7 +263,7 @@ public abstract class SQLDB extends Database {
public void clean() {
Log.info("Cleaning the database.");
try {
checkConnection();
setupDatabases();
tpsTable.clean();
Log.info("Clean complete.");
} catch (SQLException e) {
@ -313,25 +276,19 @@ public abstract class SQLDB extends Database {
*/
@Override
public boolean removeAllData() {
boolean success = true;
setStatus("Clearing all data");
try {
setStatus("Clearing all data");
for (Table table : getAllTablesInRemoveOrder()) {
if (!table.removeAllData()) {
success = false;
break;
return false;
}
}
if (success) {
commit();
} else {
rollback(); // TODO Tests for this case
}
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
return true;
} finally {
setAvailable();
}
setAvailable();
return success;
}
@Override
@ -354,6 +311,7 @@ public abstract class SQLDB extends Database {
if (data.isEmpty()) {
return data;
}
// TODO REWRITE
Benchmark.stop("Database", "Get UserData for " + uuidsCol.size());
@ -368,13 +326,6 @@ public abstract class SQLDB extends Database {
return supportsModification;
}
/**
* @return
*/
public Connection getConnection() {
return connection;
}
private void setStatus(String status) {
Log.debug("Database", status);
}
@ -383,14 +334,23 @@ public abstract class SQLDB extends Database {
Log.logDebug("Database");
}
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
/**
* Commits changes to the .db file when using SQLite Database.
* <p>
* MySQL has Auto Commit enabled.
*/
public void commit() throws SQLException {
if (!usingMySQL) {
getConnection().commit();
@Override
public void commit(Connection connection) throws SQLException {
try {
if (!usingMySQL) {
connection.commit();
}
} finally {
endTransaction(connection);
}
}
@ -399,9 +359,17 @@ public abstract class SQLDB extends Database {
* <p>
* MySQL has Auto Commit enabled.
*/
public void rollback() throws SQLException {
if (!usingMySQL) {
connection.rollback();
public void rollback(Connection connection) throws SQLException {
try {
if (!usingMySQL) {
connection.rollback();
}
} finally {
endTransaction(connection);
}
}
public void endTransaction(Connection connection) throws SQLException {
connection.close();
}
}

View File

@ -1,11 +1,10 @@
package main.java.com.djrapitops.plan.database.databases;
import main.java.com.djrapitops.plan.Plan;
import org.apache.commons.dbcp2.BasicDataSource;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collections;
/**
* @author Rsl1122
@ -33,36 +32,26 @@ public class SQLiteDB extends SQLDB {
}
/**
* Creates a new connection to the database.
*
* @return the new Connection.
* Setups the {@link BasicDataSource}
*/
@Override
public Connection getNewConnection() {
return getNewConnection(dbName);
public void setupDataSource() {
dataSource = new BasicDataSource();
dataSource.setUrl("jdbc:sqlite:" + new File(plugin.getDataFolder(), dbName + ".db").getAbsolutePath());
dataSource.setEnableAutoCommitOnReturn(false);
dataSource.setDefaultAutoCommit(false);
dataSource.setConnectionInitSqls(Collections.singletonList("PRAGMA JOURNAL_MODE=WAL"));
}
/**
* @param dbName
* @return
*/
public Connection getNewConnection(String dbName) {
try {
Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection("jdbc:sqlite:" + new File(plugin.getDataFolder(), dbName + ".db").getAbsolutePath());
connection.setAutoCommit(false);
return connection;
} catch (ClassNotFoundException | SQLException e) {
return null;
}
}
/**
* @return
* @return the name of the Database
*/
@Override
public String getName() {
return "SQLite";
}
}

View File

@ -87,6 +87,7 @@ public class ActionsTable extends UserIDTable {
statement.setString(5, action.getAdditionalInfo());
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -117,6 +118,7 @@ public class ActionsTable extends UserIDTable {
}
return actions;
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -101,8 +101,8 @@ public class CommandUseTable extends Table {
}
return commandUse;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
Benchmark.stop("Database", "Get CommandUse");
}
}
@ -136,7 +136,6 @@ public class CommandUseTable extends Table {
}
updateCommands(updateData);
commit();
Benchmark.stop("Database", "Save Commanduse");
db.setAvailable();
}
@ -149,7 +148,7 @@ public class CommandUseTable extends Table {
"WHERE (" + columnCommand + "=?) AND (" +
columnServerID + "=" + serverTable.statementSelectServerID + ")";
statement = prepareStatement(updateStatement);
boolean commitRequired = false;
for (Map.Entry<String, Integer> entrySet : data.entrySet()) {
String key = entrySet.getKey();
Integer amount = entrySet.getValue();
@ -162,13 +161,11 @@ public class CommandUseTable extends Table {
statement.setString(2, key);
statement.setString(3, Plan.getServerUUID().toString());
statement.addBatch();
commitRequired = true;
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -182,7 +179,6 @@ public class CommandUseTable extends Table {
+ columnServerID
+ ") VALUES (?, ?, " + serverTable.statementSelectServerID + ")";
statement = prepareStatement(insertStatement);
boolean addedRows = false;
for (Map.Entry<String, Integer> entrySet : data.entrySet()) {
String key = entrySet.getKey();
Integer amount = entrySet.getValue();
@ -195,20 +191,18 @@ public class CommandUseTable extends Table {
statement.setInt(2, amount);
statement.setString(3, Plan.getServerUUID().toString());
statement.addBatch();
addedRows = true;
}
if (addedRows) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}
public Optional<String> getCommandByID(int id) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
ResultSet set;
try {
statement = prepareStatement(Select.from(tableName, columnCommand).where(columnCommandId + "=?").toString());
statement.setInt(1, id);
@ -218,13 +212,14 @@ public class CommandUseTable extends Table {
}
return Optional.empty();
} finally {
endTransaction(statement);
close(statement);
}
}
public Optional<Integer> getCommandID(String command) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
ResultSet set;
try {
statement = prepareStatement(Select.from(tableName, columnCommandId).where(columnCommand + "=?").toString());
statement.setString(1, command);
@ -234,6 +229,7 @@ public class CommandUseTable extends Table {
}
return Optional.empty();
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -91,6 +91,7 @@ public class IPsTable extends UserIDTable {
return stringList;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -100,6 +101,7 @@ public class IPsTable extends UserIDTable {
if (ips.contains(ip)) {
return;
}
insertIp(uuid, ip, geolocation);
}
@ -118,6 +120,7 @@ public class IPsTable extends UserIDTable {
statement.setString(3, geolocation);
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -136,6 +139,7 @@ public class IPsTable extends UserIDTable {
}
return Optional.empty();
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -66,7 +66,9 @@ public class KillsTable extends Table {
statement = prepareStatement("DELETE FROM " + tableName + " WHERE " + columnKillerUserID + " = ? OR " + columnVictimUserID + " = ?");
statement.setInt(1, userId);
statement.setInt(2, userId);
statement.execute();
commit(statement.getConnection());
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
@ -96,8 +98,8 @@ public class KillsTable extends Table {
}
return killData;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}
@ -120,7 +122,6 @@ public class KillsTable extends Table {
+ columnWeapon + ", "
+ columnDate
+ ") VALUES (?, ?, ?, ?)");
boolean commitRequired = false;
for (KillData kill : kills) {
if (kill == null) {
continue;
@ -130,13 +131,11 @@ public class KillsTable extends Table {
statement.setString(3, kill.getWeapon());
statement.setLong(4, kill.getTime());
statement.addBatch();
commitRequired = true;
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
Benchmark.stop("Database", "Save Kills");
}
@ -172,8 +171,8 @@ public class KillsTable extends Table {
}
return kills;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
Benchmark.stop("Database", "Get Kills multiple");
}
}
@ -199,7 +198,6 @@ public class KillsTable extends Table {
+ columnWeapon + ", "
+ columnDate
+ ") VALUES (?, ?, ?, ?)");
boolean commitRequired = false;
for (Map.Entry<Integer, List<KillData>> entrySet : kills.entrySet()) {
Integer id = entrySet.getKey();
List<KillData> playerKills = entrySet.getValue();
@ -216,14 +214,12 @@ public class KillsTable extends Table {
statement.setString(3, kill.getWeapon());
statement.setLong(4, kill.getTime());
statement.addBatch();
commitRequired = true;
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
}
} finally {
endTransaction(statement);
close(statement);
Benchmark.stop("Database", "Save Kills multiple");
}

View File

@ -99,6 +99,7 @@ public class NicknamesTable extends UserIDTable {
return nicknames;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -136,6 +137,7 @@ public class NicknamesTable extends UserIDTable {
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -192,6 +194,7 @@ public class NicknamesTable extends UserIDTable {
return nicks;
} finally {
endTransaction(statement);
close(set, statement);
Benchmark.stop("Database", "Get Nicknames Multiple");
}
@ -212,7 +215,6 @@ public class NicknamesTable extends UserIDTable {
Map<Integer, List<String>> saved = getNicknames(nicknames.keySet());
PreparedStatement statement = null;
try {
boolean commitRequired = false;
statement = prepareStatement("INSERT INTO " + tableName + " ("
+ columnUserID + ", "
+ columnCurrent + ", "
@ -239,14 +241,12 @@ public class NicknamesTable extends UserIDTable {
statement.setInt(2, (name.equals(lastNick)) ? 1 : 0);
statement.setString(3, name);
statement.addBatch();
commitRequired = true;
}
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
Benchmark.stop("Database", "Save Nicknames Multiple");
}

View File

@ -60,6 +60,12 @@ public class SecurityTable extends Table {
Log.toLog(this.getClass().getName(), ex);
return false;
} finally {
try {
endTransaction(statement);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
close(statement);
}
}
@ -80,7 +86,8 @@ public class SecurityTable extends Table {
statement.setString(2, saltPassHash);
statement.setInt(3, permLevel);
statement.execute();
commit();
commit(statement.getConnection());
} finally {
close(statement);
}
@ -104,8 +111,8 @@ public class SecurityTable extends Table {
}
return null;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}
@ -125,8 +132,8 @@ public class SecurityTable extends Table {
}
return list;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}
}

View File

@ -92,6 +92,7 @@ public class ServerTable extends Table {
statement.setInt(5, info.getId());
statement.executeUpdate();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -122,6 +123,7 @@ public class ServerTable extends Table {
statement.setBoolean(4, true);
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -149,6 +151,7 @@ public class ServerTable extends Table {
return Optional.empty();
}
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -176,6 +179,7 @@ public class ServerTable extends Table {
return Optional.empty();
}
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -195,6 +199,7 @@ public class ServerTable extends Table {
}
return names;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -224,6 +229,7 @@ public class ServerTable extends Table {
return Optional.empty();
}
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -247,6 +253,7 @@ public class ServerTable extends Table {
}
return servers;
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -101,6 +101,7 @@ public class SessionsTable extends UserIDTable {
statement.setString(6, Plan.getServerUUID().toString());
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
@ -132,6 +133,7 @@ public class SessionsTable extends UserIDTable {
}
return sessionsByServer;
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -111,8 +111,8 @@ public class TPSTable extends Table {
}
return data;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
Benchmark.stop("Database", "Get TPS");
}
}
@ -131,7 +131,6 @@ public class TPSTable extends Table {
}
});
db.setAvailable();
commit();
}
private void saveTPSBatch(List<TPS> batch) throws SQLException {
@ -161,7 +160,9 @@ public class TPSTable extends Table {
statement.setDouble(7, tps.getChunksLoaded());
statement.addBatch();
}
statement.executeBatch();
commit(statement.getConnection());
} finally {
close(statement);
}
@ -179,6 +180,7 @@ public class TPSTable extends Table {
statement.setLong(1, MiscUtils.getTime() - fiveWeeks);
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -54,11 +54,7 @@ public abstract class Table {
* @return @throws SQLException
*/
protected Connection getConnection() throws SQLException {
Connection connection = db.getConnection();
if (connection == null || connection.isClosed()) {
connection = db.getNewConnection();
}
return connection;
return db.getConnection();
}
/**
@ -78,11 +74,11 @@ public abstract class Table {
Statement statement = null;
try {
statement = connection.createStatement();
return statement.execute(statementString);
boolean b = statement.execute(statementString);
commit(statement.getConnection());
return b;
} finally {
if (statement != null) {
statement.close();
}
close(statement);
}
}
@ -180,7 +176,19 @@ public abstract class Table {
*
* @throws SQLException If commit fails or there is nothing to commit.
*/
protected void commit() throws SQLException {
db.commit();
protected void commit(Connection connection) throws SQLException {
db.commit(connection);
}
protected void endTransaction(Connection connection) throws SQLException {
db.endTransaction(connection);
}
protected void endTransaction(Statement statement) throws SQLException {
if (statement == null) {
return;
}
endTransaction(statement.getConnection());
}
}

View File

@ -33,6 +33,11 @@ public abstract class UserIDTable extends Table {
Log.toLog(this.getClass().getName(), ex);
return false;
} finally {
try {
endTransaction(statement);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
close(statement);
}
}

View File

@ -183,8 +183,8 @@ public class UsersTable extends Table {
}
return userId;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}
@ -206,8 +206,8 @@ public class UsersTable extends Table {
}
return uuid;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}
@ -228,8 +228,8 @@ public class UsersTable extends Table {
}
return uuids;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
Benchmark.stop("Database", "Get Saved UUIDS");
}
}
@ -251,11 +251,17 @@ public class UsersTable extends Table {
try {
statement = prepareStatement("DELETE FROM " + tableName + " WHERE (" + columnUUID + "=?)");
statement.setString(1, uuid);
statement.execute();
return true;
} catch (SQLException ex) {
return false;
} finally {
try {
endTransaction(statement);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
close(statement);
}
}
@ -284,8 +290,8 @@ public class UsersTable extends Table {
}
}
} finally {
close(statement);
close(set);
endTransaction(statement);
close(set, statement);
}
return containsBukkitData;
}
@ -313,8 +319,8 @@ public class UsersTable extends Table {
}
return ids;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
Benchmark.stop("Database", "Get User IDS Multiple");
}
}
@ -337,8 +343,8 @@ public class UsersTable extends Table {
}
return ids;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
Benchmark.stop("Database", "Get User IDS ALL");
}
}
@ -368,8 +374,8 @@ public class UsersTable extends Table {
}
return null;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}

View File

@ -56,8 +56,8 @@ public class VersionTable extends Table {
Log.debug("Database", "DB Schema version: " + version);
return version;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}
@ -71,9 +71,9 @@ public class VersionTable extends Table {
try {
statement = prepareStatement("INSERT INTO " + tableName + " (version) VALUES (" + version + ")");
statement.executeUpdate();
commit(statement.getConnection());
} finally {
close(statement);
}
}
}

View File

@ -75,6 +75,7 @@ public class WorldTable extends Table {
}
return worldNames;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -101,16 +102,14 @@ public class WorldTable extends Table {
statement = prepareStatement("INSERT INTO " + tableName + " ("
+ columnWorldName
+ ") VALUES (?)");
boolean commitRequired = false;
for (String world : worlds) {
statement.setString(1, world);
statement.addBatch();
commitRequired = true;
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -95,8 +95,8 @@ public class WorldTimesTable extends UserIDTable {
}
return times;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}
@ -125,8 +125,8 @@ public class WorldTimesTable extends UserIDTable {
}
return times;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}
@ -167,7 +167,6 @@ public class WorldTimesTable extends UserIDTable {
" WHERE (" + selectWorldIDsql + "=" + columnWorldId + ")" +
" AND (" + columnUserID + "=?)"
);
boolean commitRequired = false;
for (Map.Entry<String, Long> entry : updateData.entrySet()) {
String worldName = entry.getKey();
long time = entry.getValue();
@ -175,12 +174,10 @@ public class WorldTimesTable extends UserIDTable {
statement.setString(2, worldName);
statement.setInt(3, userId);
statement.addBatch();
commitRequired = true;
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -198,7 +195,6 @@ public class WorldTimesTable extends UserIDTable {
+ columnPlaytime
+ ") VALUES (?, " + selectWorldIDsql + ", ?)"
);
boolean commitRequired = false;
for (Map.Entry<String, Long> entry : newData.entrySet()) {
String worldName = entry.getKey();
long time = entry.getValue();
@ -206,12 +202,10 @@ public class WorldTimesTable extends UserIDTable {
statement.setString(2, worldName);
statement.setLong(3, time);
statement.addBatch();
commitRequired = true;
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -259,7 +253,6 @@ public class WorldTimesTable extends UserIDTable {
" WHERE (" + selectWorldIDsql + "=" + columnWorldId + ")" +
" AND (" + columnUserID + "=?)"
);
boolean commitRequired = false;
for (Map.Entry<Integer, Map<String, Long>> entry : updateData.entrySet()) {
int userId = entry.getKey();
for (Map.Entry<String, Long> times : entry.getValue().entrySet()) {
@ -269,13 +262,11 @@ public class WorldTimesTable extends UserIDTable {
statement.setString(2, worldName);
statement.setInt(3, userId);
statement.addBatch();
commitRequired = true;
}
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -293,7 +284,6 @@ public class WorldTimesTable extends UserIDTable {
+ columnPlaytime
+ ") VALUES (?, " + selectWorldIDsql + ", ?)"
);
boolean commitRequired = false;
for (Map.Entry<Integer, Map<String, Long>> entry : newData.entrySet()) {
int userId = entry.getKey();
for (Map.Entry<String, Long> times : entry.getValue().entrySet()) {
@ -303,13 +293,11 @@ public class WorldTimesTable extends UserIDTable {
statement.setString(2, worldName);
statement.setLong(3, time);
statement.addBatch();
commitRequired = true;
}
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -44,11 +44,7 @@ public class QueueTest {
public void setUp() throws Exception {
TestInit t = TestInit.init();
Plan plan = t.getPlanMock();
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
@Override
public void startConnectionPingTask() {
}
};
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
db.init();
when(plan.getDB()).thenReturn(db);
dataCache = new DataCache(plan) {

View File

@ -3,7 +3,6 @@ package test.java.main.java.com.djrapitops.plan.database;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.TPS;
import main.java.com.djrapitops.plan.data.WebUser;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
@ -21,6 +20,7 @@ import test.java.utils.TestInit;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
@ -34,21 +34,18 @@ import static org.junit.Assert.assertTrue;
public class DatabaseCommitTest {
private Plan plan;
private Database db;
private SQLiteDB db;
private int rows;
@Before
public void setUp() throws Exception {
TestInit t = TestInit.init();
plan = t.getPlanMock();
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
@Override
public void startConnectionPingTask() {
}
};
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
File f = new File(plan.getDataFolder(), "Errors.txt");
rows = FileUtil.lines(f).size();
db.init();
}
/**
@ -72,9 +69,12 @@ public class DatabaseCommitTest {
@Test
public void testNoExceptionWhenCommitEmpty() throws SQLException {
db.init();
db.commit();
db.commit();
db.commit();
Connection con = db.getConnection();
db.commit(con);
db.commit(con);
db.commit(con);
}
@Ignore("//TODO")

View File

@ -61,14 +61,11 @@ public class DatabaseTest {
public void setUp() throws Exception {
TestInit t = TestInit.init();
plan = t.getPlanMock();
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
@Override
public void startConnectionPingTask() {
}
};
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
File f = new File(plan.getDataFolder(), "Errors.txt");
rows = FileUtil.lines(f).size();
db.init();
}
/**
@ -122,12 +119,7 @@ public class DatabaseTest {
*/
@Test
public void testMysqlGetConfigName() {
assertEquals("mysql", new MySQLDB(plan) {
@Override
public void startConnectionPingTask() {
}
}.getConfigName());
assertEquals("mysql", new MySQLDB(plan).getConfigName());
}
/**
@ -135,12 +127,7 @@ public class DatabaseTest {
*/
@Test
public void testMysqlGetName() {
assertEquals("MySQL", new MySQLDB(plan) {
@Override
public void startConnectionPingTask() {
}
}.getName());
assertEquals("MySQL", new MySQLDB(plan).getName());
}
/**
@ -149,9 +136,9 @@ public class DatabaseTest {
@Ignore("")
@Test // TODO Rewrite
public void testRemoveAll() throws SQLException {
db.init();
// UserData data = MockUtils.mockUser();
// db.saveUserData(data);
//db.init();
//UserData data = MockUtils.mockUser();
//db.saveUserData(data);
HashMap<String, Integer> c = new HashMap<>();
c.put("/plan", 1);
c.put("/tp", 4);
@ -170,7 +157,7 @@ public class DatabaseTest {
@Ignore("//TODO")
@Test
public void testSaveCommandUse() throws SQLException {
db.init();
//db.init();
HashMap<String, Integer> c = new HashMap<>();
c.put("/plan", 1);
c.put("/tp", 4);
@ -195,11 +182,13 @@ public class DatabaseTest {
*/
@Test // TODO Rewrite
public void testRemove() throws SQLException {
/*
db.init();
// UserData data = MockUtils.mockUser();
// db.saveUserData(data);
// assertTrue(db.removeAccount(data.getUuid().toString()));
// assertTrue("Contains the user", !db.wasSeenBefore(data.getUuid()));
UserData data = MockUtils.mockUser();
db.saveUserData(data);
assertTrue(db.removeAccount(data.getUuid().toString()));
assertTrue("Contains the user", !db.wasSeenBefore(data.getUuid()));
*/
}
/**
@ -222,7 +211,7 @@ public class DatabaseTest {
@Test
public void testTPSSaving() throws SQLException {
db.init();
//db.init();
TPSTable tpsTable = db.getTpsTable();
List<TPS> expected = new ArrayList<>();
Random r = new Random();

View File

@ -82,9 +82,9 @@ public class TestInit {
when(planMock.getConfig()).thenReturn(config);
File testFolder = getTestFolder();
if (clearOnStart) {
/*if (clearOnStart) {
clean(testFolder);
}
}*/
when(planMock.getDataFolder()).thenReturn(testFolder);
// Html Files
@ -119,7 +119,7 @@ public class TestInit {
}
private RunnableFactory<Plan> mockRunnableFactory() {
RunnableFactory<Plan> runnableFactory = new RunnableFactory<Plan>(planMock) {
return new RunnableFactory<Plan>(planMock) {
@Override
public IRunnable createNew(String name, final AbsRunnable runnable) {
return new IRunnable() {
@ -170,7 +170,6 @@ public class TestInit {
};
}
};
return runnableFactory;
}
private static File getTestFolder() {