Fixed #294 (PluginBridge needs same fix)

This commit is contained in:
Rsl1122 2017-09-28 14:40:14 +03:00
parent adb8018332
commit f8e585a246
17 changed files with 284 additions and 328 deletions

View File

@ -10,6 +10,7 @@ import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.tables.*;
import main.java.com.djrapitops.plan.database.tables.move.Version8TransferTable;
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;
@ -305,7 +306,7 @@ public abstract class SQLDB extends Database {
connection.commit();
}
} finally {
endTransaction(connection);
MiscUtils.close(connection);
}
}
@ -320,14 +321,10 @@ public abstract class SQLDB extends Database {
connection.rollback();
}
} finally {
endTransaction(connection);
MiscUtils.close(connection);
}
}
public void endTransaction(Connection connection) throws SQLException {
connection.close();
}
public boolean isOpen() {
return open;
}

View File

@ -13,6 +13,7 @@ import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -72,10 +73,11 @@ public class ActionsTable extends UserIDTable {
}
public void insertAction(UUID uuid, Action action) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
connection = getConnection();
statement = connection.prepareStatement(insertStatement);
statement.setString(1, uuid.toString());
statement.setString(2, Plan.getServerUUID().toString());
statement.setInt(3, action.getDoneAction().getId());
@ -83,9 +85,9 @@ public class ActionsTable extends UserIDTable {
statement.setString(5, action.getAdditionalInfo());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
close(statement, connection);
}
}
@ -98,10 +100,12 @@ public class ActionsTable extends UserIDTable {
*/
public List<Action> getActions(UUID uuid) throws SQLException {
List<Action> actions = new ArrayList<>();
Connection connection = null;
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, "*")
connection = getConnection();
statement = connection.prepareStatement(Select.from(tableName, "*")
.where(columnUserID + "=" + usersTable.statementSelectID)
.toString());
statement.setFetchSize(5000);
@ -116,12 +120,12 @@ public class ActionsTable extends UserIDTable {
}
return actions;
} finally {
endTransaction(statement);
close(set, statement);
close(set, statement, connection);
}
}
public Map<UUID, Map<UUID, List<Action>>> getAllActions() throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet set = null;
try {
@ -129,7 +133,8 @@ public class ActionsTable extends UserIDTable {
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
statement = prepareStatement("SELECT " +
connection = getConnection();
statement = connection.prepareStatement("SELECT " +
columnActionID + ", " +
columnDate + ", " +
columnAdditionalInfo + ", " +
@ -160,8 +165,7 @@ public class ActionsTable extends UserIDTable {
}
return map;
} finally {
endTransaction(statement);
close(set, statement);
close(set, statement, connection);
}
}
@ -169,9 +173,11 @@ public class ActionsTable extends UserIDTable {
if (Verify.isEmpty(allActions)) {
return;
}
Connection connection = null;
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
connection = getConnection();
statement = connection.prepareStatement(insertStatement);
// Every Server
for (UUID serverUUID : allActions.keySet()) {
@ -192,9 +198,9 @@ public class ActionsTable extends UserIDTable {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
close(statement, connection);
}
}
}

View File

@ -7,6 +7,7 @@ import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -78,10 +79,12 @@ public class CommandUseTable extends Table {
*/
public Map<String, Integer> getCommandUse(UUID serverUUID) throws SQLException {
Map<String, Integer> commandUse = new HashMap<>();
Connection connection = null;
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName,
connection = getConnection();
statement = connection.prepareStatement(Select.from(tableName,
columnCommand, columnTimesUsed)
.where(columnServerID + "=" + serverTable.statementSelectServerID)
.toString());
@ -95,8 +98,7 @@ public class CommandUseTable extends Table {
}
return commandUse;
} finally {
endTransaction(statement);
close(set, statement);
close(set, statement, connection);
}
}
@ -104,9 +106,11 @@ public class CommandUseTable extends Table {
if (command.length() > 20) {
return;
}
Connection connection = null;
PreparedStatement statement = null;
try {
statement = prepareStatement("UPDATE " + tableName + " SET "
connection = getConnection();
statement = connection.prepareStatement("UPDATE " + tableName + " SET "
+ columnTimesUsed + "=" + columnTimesUsed + "+ 1" +
" WHERE " + columnServerID + "=" + serverTable.statementSelectServerID +
" AND " + columnCommand + "=?");
@ -114,36 +118,40 @@ public class CommandUseTable extends Table {
statement.setString(2, command);
int success = statement.executeUpdate();
commit(statement.getConnection());
connection.commit();
if (success == 0) {
insertCommand(command);
}
} finally {
close(statement);
close(statement, connection);
}
}
private void insertCommand(String command) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
connection = getConnection();
statement = connection.prepareStatement(insertStatement);
statement.setString(1, command);
statement.setInt(2, 1);
statement.setString(3, Plan.getServerUUID().toString());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
close(statement, connection);
}
}
public Optional<String> getCommandByID(int id) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnCommand).where(columnCommandId + "=?").toString());
connection = getConnection();
statement = connection.prepareStatement(Select.from(tableName, columnCommand).where(columnCommandId + "=?").toString());
statement.setInt(1, id);
set = statement.executeQuery();
if (set.next()) {
@ -151,16 +159,17 @@ public class CommandUseTable extends Table {
}
return Optional.empty();
} finally {
endTransaction(statement);
close(set, statement);
close(set, statement, connection);
}
}
public Optional<Integer> getCommandID(String command) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnCommandId).where(columnCommand + "=?").toString());
connection = getConnection();
statement = connection.prepareStatement(Select.from(tableName, columnCommandId).where(columnCommand + "=?").toString());
statement.setString(1, command);
set = statement.executeQuery();
if (set.next()) {
@ -168,18 +177,19 @@ public class CommandUseTable extends Table {
}
return Optional.empty();
} finally {
endTransaction(statement);
close(set, statement);
close(set, statement, connection);
}
}
public Map<UUID, Map<String, Integer>> getAllCommandUsages() throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet set = null;
try {
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
statement = prepareStatement("SELECT " +
connection = getConnection();
statement = connection.prepareStatement("SELECT " +
columnCommand + ", " +
columnTimesUsed + ", " +
serverUUIDColumn +
@ -202,8 +212,7 @@ public class CommandUseTable extends Table {
}
return map;
} finally {
endTransaction(statement);
close(set, statement);
close(set, statement, connection);
}
}
@ -212,8 +221,8 @@ public class CommandUseTable extends Table {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
// Every Server
for (UUID serverUUID : allCommandUsages.keySet()) {
@ -230,7 +239,7 @@ public class CommandUseTable extends Table {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}

View File

@ -7,6 +7,7 @@ import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -66,10 +67,10 @@ public class IPsTable extends UserIDTable {
private List<String> getStringList(UUID uuid, String column) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()) {
List<String> stringList = new ArrayList<>();
statement = prepareStatement(Select.from(tableName, column)
statement = connection.prepareStatement(Select.from(tableName, column)
.where(columnUserID + "=" + usersTable.statementSelectID)
.toString());
statement.setFetchSize(50);
@ -81,7 +82,6 @@ public class IPsTable extends UserIDTable {
return stringList;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -96,15 +96,15 @@ public class IPsTable extends UserIDTable {
private void insertIp(UUID uuid, String ip, String geolocation) throws SQLException {
PreparedStatement statement = null;
try {
try (Connection connection = getConnection()) {
statement = prepareStatement(insertStatement);
statement = connection.prepareStatement(insertStatement);
statement.setString(1, uuid.toString());
statement.setString(2, ip);
statement.setString(3, geolocation);
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -113,8 +113,8 @@ public class IPsTable extends UserIDTable {
public Optional<String> getGeolocation(String ip) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnGeolocation)
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnGeolocation)
.where(columnIP + "=?")
.toString());
statement.setString(1, ip);
@ -124,7 +124,6 @@ public class IPsTable extends UserIDTable {
}
return Optional.empty();
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -132,13 +131,13 @@ public class IPsTable extends UserIDTable {
public Map<UUID, List<String>> getAllGeolocations() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()) {
Map<UUID, List<String>> geoLocations = new HashMap<>();
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
columnGeolocation + ", " +
usersUUIDColumn +
" FROM " + tableName +
@ -155,7 +154,6 @@ public class IPsTable extends UserIDTable {
}
return geoLocations;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -163,11 +161,11 @@ public class IPsTable extends UserIDTable {
public Map<UUID, Map<String, String>> getAllIPsAndGeolocations() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
columnGeolocation + ", " +
columnIP + ", " +
usersUUIDColumn +
@ -190,7 +188,6 @@ public class IPsTable extends UserIDTable {
}
return map;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -200,8 +197,8 @@ public class IPsTable extends UserIDTable {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
// Every User
for (UUID uuid : allIPsAndGeolocations.keySet()) {
@ -219,7 +216,7 @@ public class IPsTable extends UserIDTable {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}

View File

@ -10,6 +10,7 @@ import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -69,15 +70,15 @@ public class KillsTable extends UserIDTable {
@Override
public void removeUser(UUID uuid) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement("DELETE FROM " + tableName +
try (Connection connection = getConnection()) {
statement = connection.prepareStatement("DELETE FROM " + tableName +
" WHERE " + columnKillerUserID + " = " + usersTable.statementSelectID +
" OR " + columnVictimUserID + " = " + usersTable.statementSelectID);
statement.setString(1, uuid.toString());
statement.setString(2, uuid.toString());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -88,8 +89,8 @@ public class KillsTable extends UserIDTable {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(insertStatement);
for (PlayerKill kill : playerKills) {
UUID victim = kill.getVictim();
long date = kill.getTime();
@ -103,7 +104,7 @@ public class KillsTable extends UserIDTable {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -112,10 +113,10 @@ public class KillsTable extends UserIDTable {
public void addKillsToSessions(UUID uuid, Map<Integer, Session> sessions) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()) {
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as victim_uuid";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
columnSessionID + ", " +
columnDate + ", " +
columnWeapon + ", " +
@ -153,12 +154,12 @@ public class KillsTable extends UserIDTable {
public Map<UUID, List<PlayerKill>> getPlayerKills(UUID serverUUID) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()){
String usersVictimIDColumn = usersTable + "." + usersTable.getColumnID();
String usersKillerIDColumn = "a." + usersTable.getColumnID();
String usersVictimUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as victim_uuid";
String usersKillerUUIDColumn = "a." + usersTable.getColumnUUID() + " as killer_uuid";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
columnDate + ", " +
columnWeapon + ", " +
usersVictimUUIDColumn + ", " +
@ -205,8 +206,8 @@ public class KillsTable extends UserIDTable {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
String[] gms = GMTimes.getGMKeyArray();
for (UUID serverUUID : allSessions.keySet()) {
for (Map.Entry<UUID, List<Session>> entry : allSessions.get(serverUUID).entrySet()) {
@ -230,7 +231,7 @@ public class KillsTable extends UserIDTable {
}
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -239,10 +240,10 @@ public class KillsTable extends UserIDTable {
public Map<Integer, List<PlayerKill>> getAllPlayerKillsBySessionID() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as victim_uuid";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
columnSessionID + ", " +
columnDate + ", " +
columnWeapon + ", " +

View File

@ -7,6 +7,7 @@ import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -67,8 +68,8 @@ public class NicknamesTable extends UserIDTable {
public List<String> getAllNicknames(UUID uuid) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT " + columnNick + " FROM " + tableName +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT " + columnNick + " FROM " + tableName +
" WHERE (" + columnUserID + "=" + usersTable.statementSelectID + ")");
statement.setString(1, uuid.toString());
set = statement.executeQuery();
@ -85,7 +86,6 @@ public class NicknamesTable extends UserIDTable {
}
return nicknames;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -116,8 +116,8 @@ public class NicknamesTable extends UserIDTable {
public List<String> getNicknames(UUID uuid, UUID serverUUID) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT " + columnNick + " FROM " + tableName +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT " + columnNick + " FROM " + tableName +
" WHERE (" + columnUserID + "=" + usersTable.statementSelectID + ")" +
" AND " + columnServerID + "=" + serverTable.statementSelectServerID
);
@ -137,7 +137,6 @@ public class NicknamesTable extends UserIDTable {
}
return nicknames;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -149,14 +148,14 @@ public class NicknamesTable extends UserIDTable {
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
statement.setString(1, uuid.toString());
statement.setString(2, Plan.getServerUUID().toString());
statement.setString(3, displayName);
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -165,12 +164,12 @@ public class NicknamesTable extends UserIDTable {
public Map<UUID, Map<UUID, List<String>>> getAllNicknames() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
columnNick + ", " +
usersUUIDColumn + ", " +
serverUUIDColumn +
@ -195,7 +194,6 @@ public class NicknamesTable extends UserIDTable {
}
return map;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -205,8 +203,8 @@ public class NicknamesTable extends UserIDTable {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
// Every Server
for (UUID serverUUID : allNicknames.keySet()) {
@ -225,7 +223,7 @@ public class NicknamesTable extends UserIDTable {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}

View File

@ -15,6 +15,7 @@ import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -51,12 +52,12 @@ public class SecurityTable extends Table {
public boolean removeUser(String user) {
PreparedStatement statement = null;
try {
statement = prepareStatement("DELETE FROM " + tableName + " WHERE (" + columnUser + "=?)");
try (Connection connection = getConnection()){
statement = connection.prepareStatement("DELETE FROM " + tableName + " WHERE (" + columnUser + "=?)");
statement.setString(1, user);
statement.execute();
commit(statement.getConnection());
connection.commit();
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
@ -72,14 +73,14 @@ public class SecurityTable extends Table {
public void addNewUser(String user, String saltPassHash, int permLevel) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
statement.setString(1, user);
statement.setString(2, saltPassHash);
statement.setInt(3, permLevel);
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -92,8 +93,8 @@ public class SecurityTable extends Table {
public WebUser getWebUser(String user) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.all(tableName).where(columnUser + "=?").toString());
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.all(tableName).where(columnUser + "=?").toString());
statement.setString(1, user);
set = statement.executeQuery();
if (set.next()) {
@ -103,7 +104,6 @@ public class SecurityTable extends Table {
}
return null;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -111,8 +111,8 @@ public class SecurityTable extends Table {
public List<WebUser> getUsers() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.all(tableName).toString());
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.all(tableName).toString());
statement.setFetchSize(5000);
set = statement.executeQuery();
List<WebUser> list = new ArrayList<>();
@ -125,7 +125,6 @@ public class SecurityTable extends Table {
}
return list;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -135,8 +134,8 @@ public class SecurityTable extends Table {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
for (WebUser user : users) {
String userName = user.getName();
String pass = user.getSaltedPassHash();
@ -149,7 +148,7 @@ public class SecurityTable extends Table {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}

View File

@ -10,6 +10,7 @@ import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.*;
import main.java.com.djrapitops.plan.systems.info.server.ServerInfo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -78,8 +79,8 @@ public class ServerTable extends Table {
private void updateServerInfo(ServerInfo info) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement(Update.values(tableName,
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Update.values(tableName,
columnServerUUID,
columnServerName,
columnWebserverAddress,
@ -96,7 +97,7 @@ public class ServerTable extends Table {
statement.setInt(6, info.getId());
statement.executeUpdate();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -115,8 +116,8 @@ public class ServerTable extends Table {
String webAddress = info.getWebAddress();
Verify.nullCheck(uuid, name, webAddress);
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(insertStatement);
statement.setString(1, uuid.toString());
statement.setString(2, name);
@ -125,7 +126,7 @@ public class ServerTable extends Table {
statement.setInt(5, info.getMaxPlayers());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -141,8 +142,8 @@ public class ServerTable extends Table {
public Optional<Integer> getServerID(UUID serverUUID) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName,
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName,
columnServerID)
.where(columnServerUUID + "=?")
.toString());
@ -154,7 +155,6 @@ public class ServerTable extends Table {
return Optional.empty();
}
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -169,8 +169,8 @@ public class ServerTable extends Table {
public Optional<String> getServerName(UUID serverUUID) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName,
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.from(tableName,
columnServerName)
.where(columnServerUUID + "=?")
.toString());
@ -182,7 +182,6 @@ public class ServerTable extends Table {
return Optional.empty();
}
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -191,8 +190,8 @@ public class ServerTable extends Table {
Map<Integer, String> names = new HashMap<>();
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName,
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.from(tableName,
columnServerID, columnServerName)
.toString());
set = statement.executeQuery();
@ -202,7 +201,6 @@ public class ServerTable extends Table {
}
return names;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -216,8 +214,8 @@ public class ServerTable extends Table {
public Optional<ServerInfo> getBungeeInfo() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, "*")
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.from(tableName, "*")
.where(columnServerName + "=?")
.toString());
statement.setString(1, "BungeeCord");
@ -233,7 +231,6 @@ public class ServerTable extends Table {
return Optional.empty();
}
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -241,8 +238,8 @@ public class ServerTable extends Table {
public List<ServerInfo> getBukkitServers() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, "*")
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.from(tableName, "*")
.where(columnServerName + "!=?")
.toString());
statement.setString(1, "BungeeCord");
@ -258,7 +255,6 @@ public class ServerTable extends Table {
}
return servers;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -276,8 +272,8 @@ public class ServerTable extends Table {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
for (ServerInfo info : allServerInfo) {
UUID uuid = info.getUuid();
@ -297,7 +293,7 @@ public class ServerTable extends Table {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -306,8 +302,8 @@ public class ServerTable extends Table {
public List<UUID> getServerUUIDs() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnServerUUID)
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.from(tableName, columnServerUUID)
.toString());
set = statement.executeQuery();
List<UUID> uuids = new ArrayList<>();
@ -316,7 +312,6 @@ public class ServerTable extends Table {
}
return uuids;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -324,8 +319,8 @@ public class ServerTable extends Table {
public Optional<UUID> getServerUUID(String serverName) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName,
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.from(tableName,
columnServerUUID)
.where(columnServerName + "=?")
.toString());
@ -337,7 +332,6 @@ public class ServerTable extends Table {
return Optional.empty();
}
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -345,8 +339,8 @@ public class ServerTable extends Table {
public Optional<ServerInfo> getServerInfo(UUID serverUUID) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, "*")
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.from(tableName, "*")
.where(columnServerUUID + "=?")
.toString());
statement.setString(1, serverUUID.toString());
@ -361,7 +355,6 @@ public class ServerTable extends Table {
}
return Optional.empty();
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -369,8 +362,8 @@ public class ServerTable extends Table {
public int getMaxPlayers() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT SUM(" + columnMaxPlayers + ") AS max FROM " + tableName);
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT SUM(" + columnMaxPlayers + ") AS max FROM " + tableName);
statement.setFetchSize(5000);
set = statement.executeQuery();
@ -379,7 +372,6 @@ public class ServerTable extends Table {
}
return 0;
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -9,6 +9,7 @@ import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -102,8 +103,8 @@ public class SessionsTable extends UserIDTable {
*/
private void saveSessionInformation(UUID uuid, Session session) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
statement.setString(1, uuid.toString());
statement.setLong(2, session.getSessionStart());
@ -113,7 +114,7 @@ public class SessionsTable extends UserIDTable {
statement.setString(6, Plan.getServerUUID().toString());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -129,8 +130,8 @@ public class SessionsTable extends UserIDTable {
private int getSessionID(UUID uuid, Session session) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT " + columnID + " FROM " + tableName +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT " + columnID + " FROM " + tableName +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID +
" AND " + columnSessionStart + "=?" +
" AND " + columnSessionEnd + "=?");
@ -143,7 +144,6 @@ public class SessionsTable extends UserIDTable {
}
return -1;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -163,8 +163,8 @@ public class SessionsTable extends UserIDTable {
Map<String, List<Session>> sessionsByServer = new HashMap<>();
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, "*")
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.from(tableName, "*")
.where(columnUserID + "=" + usersTable.statementSelectID)
.toString());
statement.setString(1, uuid.toString());
@ -187,7 +187,6 @@ public class SessionsTable extends UserIDTable {
}
return sessionsByServer;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -248,8 +247,8 @@ public class SessionsTable extends UserIDTable {
public long getPlaytime(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT" +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT" +
" (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
" FROM " + tableName +
" WHERE " + columnSessionStart + ">?" +
@ -264,7 +263,6 @@ public class SessionsTable extends UserIDTable {
}
return 0;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -293,8 +291,8 @@ public class SessionsTable extends UserIDTable {
Map<String, Long> playtimes = new HashMap<>();
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT " +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT " +
"(SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime, " +
columnServerID +
" FROM " + tableName +
@ -311,7 +309,6 @@ public class SessionsTable extends UserIDTable {
}
return playtimes;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -359,8 +356,8 @@ public class SessionsTable extends UserIDTable {
public long getPlaytimeOfServer(UUID serverUUID, long afterDate) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT" +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT" +
" (SUM(" + columnSessionEnd + ") - SUM(" + columnSessionStart + ")) as playtime" +
" FROM " + tableName +
" WHERE " + columnSessionStart + ">?" +
@ -374,7 +371,6 @@ public class SessionsTable extends UserIDTable {
}
return 0;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -426,8 +422,8 @@ public class SessionsTable extends UserIDTable {
public int getSessionCount(UUID uuid, UUID serverUUID, long afterDate) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT" +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT" +
" COUNT(*) as logintimes" +
" FROM " + tableName +
" WHERE (" + columnSessionStart + " >= ?)" +
@ -442,7 +438,6 @@ public class SessionsTable extends UserIDTable {
}
return 0;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -464,11 +459,11 @@ public class SessionsTable extends UserIDTable {
Map<UUID, List<Session>> sessionsByUser = new HashMap<>();
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
columnSessionStart + ", " +
columnSessionEnd + ", " +
columnDeaths + ", " +
@ -494,7 +489,6 @@ public class SessionsTable extends UserIDTable {
}
return sessionsByUser;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -503,8 +497,8 @@ public class SessionsTable extends UserIDTable {
public long getLastSeen(UUID uuid) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT" +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT" +
" MAX(" + columnSessionEnd + ") as last_seen" +
" FROM " + tableName +
" WHERE " + columnUserID + "=" + usersTable.statementSelectID);
@ -515,7 +509,6 @@ public class SessionsTable extends UserIDTable {
}
return 0;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -524,11 +517,11 @@ public class SessionsTable extends UserIDTable {
public Map<UUID, Long> getLastSeenForAllPlayers() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
statement = prepareStatement("SELECT" +
statement = connection.prepareStatement("SELECT" +
" MAX(" + columnSessionEnd + ") as last_seen, " +
usersUUIDColumn +
" FROM " + tableName +
@ -544,7 +537,6 @@ public class SessionsTable extends UserIDTable {
}
return lastSeenMap;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -552,13 +544,13 @@ public class SessionsTable extends UserIDTable {
public Map<UUID, Map<UUID, List<Session>>> getAllSessions(boolean getKillsAndWorldTimes) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
tableName + "." + columnID + ", " +
columnSessionStart + ", " +
columnSessionEnd + ", " +
@ -600,7 +592,6 @@ public class SessionsTable extends UserIDTable {
}
return map;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -610,8 +601,8 @@ public class SessionsTable extends UserIDTable {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
for (UUID serverUUID : allSessions.keySet()) {
for (Map.Entry<UUID, List<Session>> entry : allSessions.get(serverUUID).entrySet()) {
UUID uuid = entry.getKey();
@ -630,7 +621,7 @@ public class SessionsTable extends UserIDTable {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}

View File

@ -12,6 +12,7 @@ import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -82,8 +83,8 @@ public class TPSTable extends Table {
List<TPS> data = new ArrayList<>();
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.all(tableName)
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.all(tableName)
.where(columnServerID + "=" + serverTable.statementSelectServerID)
.toString());
statement.setFetchSize(50000);
@ -101,7 +102,6 @@ public class TPSTable extends Table {
}
return data;
} finally {
endTransaction(statement);
close(set, statement);
Benchmark.stop("Database", "Get TPS");
}
@ -109,8 +109,8 @@ public class TPSTable extends Table {
public void insertTPS(TPS tps) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
statement.setString(1, Plan.getServerUUID().toString());
statement.setLong(2, tps.getDate());
@ -122,7 +122,7 @@ public class TPSTable extends Table {
statement.setDouble(8, tps.getChunksLoaded());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -138,8 +138,8 @@ public class TPSTable extends Table {
p = allTimePeak.get().getPlayers();
}
PreparedStatement statement = null;
try {
statement = prepareStatement("DELETE FROM " + tableName +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("DELETE FROM " + tableName +
" WHERE (" + columnDate + "<?)" +
" AND (" + columnPlayers + "" +
" != ?)");
@ -149,7 +149,7 @@ public class TPSTable extends Table {
statement.setLong(2, MiscUtils.getTime() - fiveWeeks);
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -170,8 +170,8 @@ public class TPSTable extends Table {
public Optional<TPS> getPeakPlayerCount(UUID serverUUID, long afterDate) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.all(tableName)
try (Connection connection = getConnection()){
statement = connection.prepareStatement(Select.all(tableName)
.where(columnServerID + "=" + serverTable.statementSelectServerID)
.and(columnPlayers + "= (SELECT MAX(" + columnPlayers + ") FROM " + tableName + ")")
.and(columnDate + ">= ?")
@ -192,7 +192,6 @@ public class TPSTable extends Table {
}
return Optional.empty();
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -200,10 +199,10 @@ public class TPSTable extends Table {
public Map<UUID, List<TPS>> getAllTPS() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()){
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
columnDate + ", " +
columnTPS + ", " +
columnPlayers + ", " +
@ -236,7 +235,6 @@ public class TPSTable extends Table {
}
return serverMap;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -246,8 +244,8 @@ public class TPSTable extends Table {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()){
statement = connection.prepareStatement(insertStatement);
// Every Server
for (Map.Entry<UUID, List<TPS>> entry : allTPS.entrySet()) {
@ -268,7 +266,7 @@ public class TPSTable extends Table {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -277,8 +275,8 @@ public class TPSTable extends Table {
public List<TPS> getNetworkOnlineData() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT " +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT " +
columnDate + ", " +
"SUM(" + columnPlayers + ") as players" +
" FROM " + tableName +
@ -295,7 +293,6 @@ public class TPSTable extends Table {
}
return tpsList;
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -81,12 +81,11 @@ public abstract class Table {
* @throws SQLException
*/
protected boolean execute(String statementString) throws SQLException {
Connection connection = getConnection();
Statement statement = null;
try {
try (Connection connection = getConnection()){
statement = connection.createStatement();
boolean b = statement.execute(statementString);
commit(statement.getConnection());
connection.commit();
return b;
} finally {
close(statement);
@ -114,6 +113,7 @@ public abstract class Table {
* @return
* @throws SQLException
*/
@Deprecated
protected PreparedStatement prepareStatement(String sql) throws SQLException {
return getConnection().prepareStatement(sql);
}
@ -174,21 +174,11 @@ public abstract class Table {
return tableName;
}
/**
* Commits changes to .db file when using SQLite databse.
* <p>
* Auto Commit enabled when using MySQL
*
* @throws SQLException If commit fails or there is nothing to commit.
*/
protected void commit(Connection connection) throws SQLException {
db.commit(connection);
}
@Deprecated
protected void endTransaction(Connection connection) throws SQLException {
db.endTransaction(connection);
}
@Deprecated
protected void endTransaction(Statement statement) throws SQLException {
if (statement == null) {
return;

View File

@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;
@ -24,13 +25,13 @@ public abstract class UserIDTable extends Table {
public void removeUser(UUID uuid) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement("DELETE FROM " + tableName +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("DELETE FROM " + tableName +
" WHERE (" + columnUserID + "=" + usersTable.statementSelectID + ")");
statement.setString(1, uuid.toString());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}

View File

@ -14,6 +14,7 @@ import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import main.java.com.djrapitops.plan.database.sql.Update;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -59,8 +60,8 @@ public class UserInfoTable extends UserIDTable {
}
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " (" +
try (Connection connection = getConnection()) {
statement = connection.prepareStatement("INSERT INTO " + tableName + " (" +
columnUserID + ", " +
columnRegistered + ", " +
columnServerID +
@ -73,7 +74,7 @@ public class UserInfoTable extends UserIDTable {
statement.setString(3, Plan.getServerUUID().toString());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -82,23 +83,22 @@ public class UserInfoTable extends UserIDTable {
public boolean isRegistered(UUID uuid) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnUserID)
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnUserID)
.where(columnUserID + "=" + usersTable.statementSelectID)
.toString());
statement.setString(1, uuid.toString());
set = statement.executeQuery();
return set.next();
} finally {
endTransaction(statement);
close(set, statement);
}
}
public void updateOpAndBanStatus(UUID uuid, boolean opped, boolean banned) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement(Update.values(tableName, columnOP, columnBanned)
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Update.values(tableName, columnOP, columnBanned)
.where(columnUserID + "=" + usersTable.statementSelectID)
.toString());
statement.setBoolean(1, opped);
@ -106,7 +106,7 @@ public class UserInfoTable extends UserIDTable {
statement.setString(3, uuid.toString());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -119,10 +119,10 @@ public class UserInfoTable extends UserIDTable {
public UserInfo getUserInfo(UUID uuid, UUID serverUUID) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()) {
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersNameColumn = usersTable + "." + usersTable.getColumnName() + " as name";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
tableName + "." + columnRegistered + ", " +
columnOP + ", " +
columnBanned + ", " +
@ -145,7 +145,6 @@ public class UserInfoTable extends UserIDTable {
}
return null;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -162,12 +161,12 @@ public class UserInfoTable extends UserIDTable {
public List<UserInfo> getServerUserInfo(UUID serverUUID) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()){
List<UserInfo> userInfo = new ArrayList<>();
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String usersNameColumn = usersTable + "." + usersTable.getColumnName() + " as name";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
tableName + "." + columnRegistered + ", " +
columnOP + ", " +
columnBanned + ", " +
@ -200,12 +199,12 @@ public class UserInfoTable extends UserIDTable {
public Map<UUID, List<UserInfo>> getAllUserInfo() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
tableName + "." + columnRegistered + ", " +
columnBanned + ", " +
columnOP + ", " +
@ -234,7 +233,6 @@ public class UserInfoTable extends UserIDTable {
}
return serverMap;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -244,8 +242,8 @@ public class UserInfoTable extends UserIDTable {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " (" +
try (Connection connection = getConnection()){
statement = connection.prepareStatement("INSERT INTO " + tableName + " (" +
columnUserID + ", " +
columnRegistered + ", " +
columnServerID + ", " +
@ -271,7 +269,7 @@ public class UserInfoTable extends UserIDTable {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -280,12 +278,12 @@ public class UserInfoTable extends UserIDTable {
public Map<UUID, Set<UUID>> getSavedUUIDs() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()){
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
usersUUIDColumn + ", " +
serverUUIDColumn +
" FROM " + tableName +
@ -306,7 +304,6 @@ public class UserInfoTable extends UserIDTable {
}
return serverMap;
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -6,6 +6,7 @@ import main.java.com.djrapitops.plan.data.UserInfo;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -62,8 +63,8 @@ public class UsersTable extends UserIDTable {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnUUID).toString());
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnUUID).toString());
statement.setFetchSize(5000);
set = statement.executeQuery();
@ -73,7 +74,6 @@ public class UsersTable extends UserIDTable {
}
return uuids;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -85,12 +85,12 @@ public class UsersTable extends UserIDTable {
@Override
public void removeUser(UUID uuid) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement("DELETE FROM " + tableName + " WHERE (" + columnUUID + "=?)");
try (Connection connection = getConnection()) {
statement = connection.prepareStatement("DELETE FROM " + tableName + " WHERE (" + columnUUID + "=?)");
statement.setString(1, uuid.toString());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -118,8 +118,8 @@ public class UsersTable extends UserIDTable {
public UUID getUuidOf(String playername) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnUUID)
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnUUID)
.where("UPPER(" + columnName + ")=UPPER(?)")
.toString());
statement.setString(1, playername);
@ -130,7 +130,6 @@ public class UsersTable extends UserIDTable {
}
return null;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -138,8 +137,8 @@ public class UsersTable extends UserIDTable {
public List<Long> getRegisterDates() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnRegistered).toString());
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnRegistered).toString());
set = statement.executeQuery();
List<Long> registerDates = new ArrayList<>();
while (set.next()) {
@ -147,7 +146,6 @@ public class UsersTable extends UserIDTable {
}
return registerDates;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -165,14 +163,14 @@ public class UsersTable extends UserIDTable {
Verify.nullCheck(uuid, name);
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(insertStatement);
statement.setString(1, uuid.toString());
statement.setLong(2, registered);
statement.setString(3, name);
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -181,30 +179,29 @@ public class UsersTable extends UserIDTable {
public boolean isRegistered(UUID uuid) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnID)
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnID)
.where(columnUUID + "=?")
.toString());
statement.setString(1, uuid.toString());
set = statement.executeQuery();
return set.next();
} finally {
endTransaction(statement);
close(set, statement);
}
}
public void updateName(UUID uuid, String name) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement(Update.values(tableName, columnName)
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Update.values(tableName, columnName)
.where(columnUUID + "=?")
.toString());
statement.setString(1, name);
statement.setString(2, uuid.toString());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -213,8 +210,8 @@ public class UsersTable extends UserIDTable {
public int getTimesKicked(UUID uuid) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnTimesKicked)
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnTimesKicked)
.where(columnUUID + "=?")
.toString());
statement.setString(1, uuid.toString());
@ -224,21 +221,20 @@ public class UsersTable extends UserIDTable {
}
return 0;
} finally {
endTransaction(statement);
close(set, statement);
}
}
public void kicked(UUID uuid) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement("UPDATE " + tableName + " SET "
try (Connection connection = getConnection()) {
statement = connection.prepareStatement("UPDATE " + tableName + " SET "
+ columnTimesKicked + "=" + columnTimesKicked + "+ 1" +
" WHERE " + columnUUID + "=?");
statement.setString(1, uuid.toString());
statement.execute();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -247,8 +243,8 @@ public class UsersTable extends UserIDTable {
public String getPlayerName(UUID uuid) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnName)
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnName)
.where(columnUUID + "=?")
.toString());
statement.setString(1, uuid.toString());
@ -258,7 +254,6 @@ public class UsersTable extends UserIDTable {
}
return null;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -276,9 +271,9 @@ public class UsersTable extends UserIDTable {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()) {
NicknamesTable nicknamesTable = db.getNicknamesTable();
statement = prepareStatement(
statement = connection.prepareStatement(
"SELECT " + columnName + " FROM " + tableName +
" WHERE " + columnName + " LIKE LOWER(?)" +
" UNION SELECT " + columnName + " FROM " + tableName +
@ -296,7 +291,6 @@ public class UsersTable extends UserIDTable {
}
return matchingNames;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -319,8 +313,8 @@ public class UsersTable extends UserIDTable {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(insertStatement);
for (Map.Entry<UUID, UserInfo> entry : users.entrySet()) {
UUID uuid = entry.getKey();
UserInfo info = entry.getValue();
@ -334,7 +328,7 @@ public class UsersTable extends UserIDTable {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -343,9 +337,8 @@ public class UsersTable extends UserIDTable {
public Map<UUID, UserInfo> getUsers() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.all(tableName)
.toString());
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.all(tableName).toString());
statement.setFetchSize(20000);
set = statement.executeQuery();
Map<UUID, UserInfo> users = new HashMap<>();
@ -358,7 +351,6 @@ public class UsersTable extends UserIDTable {
}
return users;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -368,8 +360,8 @@ public class UsersTable extends UserIDTable {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement("UPDATE " + tableName + " SET "
try (Connection connection = getConnection()) {
statement = connection.prepareStatement("UPDATE " + tableName + " SET "
+ columnTimesKicked + "=?" +
" WHERE " + columnUUID + "=?");
for (Map.Entry<UUID, Integer> entry : timesKicked.entrySet()) {
@ -381,7 +373,7 @@ public class UsersTable extends UserIDTable {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -390,8 +382,8 @@ public class UsersTable extends UserIDTable {
public Map<UUID, Integer> getAllTimesKicked() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnUUID, columnTimesKicked)
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnUUID, columnTimesKicked)
.toString());
statement.setFetchSize(20000);
set = statement.executeQuery();
@ -404,7 +396,6 @@ public class UsersTable extends UserIDTable {
}
return timesKicked;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -412,9 +403,8 @@ public class UsersTable extends UserIDTable {
public Map<UUID, String> getPlayerNames() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement(Select.from(tableName, columnUUID, columnName)
.toString());
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(Select.from(tableName, columnUUID, columnName).toString());
statement.setFetchSize(20000);
set = statement.executeQuery();
Map<UUID, String> names = new HashMap<>();
@ -426,7 +416,6 @@ public class UsersTable extends UserIDTable {
}
return names;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -434,8 +423,8 @@ public class UsersTable extends UserIDTable {
public int getPlayerCount() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT COUNT(*) AS player_count FROM " + tableName);
try (Connection connection = getConnection()){
statement = connection.prepareStatement("SELECT COUNT(*) AS player_count FROM " + tableName);
statement.setFetchSize(5000);
set = statement.executeQuery();
@ -444,7 +433,6 @@ public class UsersTable extends UserIDTable {
}
return 0;
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -6,6 +6,7 @@ import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -37,20 +38,16 @@ public class VersionTable extends Table {
public boolean isNewDatabase() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
if (usingMySQL) {
statement = prepareStatement("SHOW TABLES LIKE ?");
} else {
statement = prepareStatement("SELECT tbl_name FROM sqlite_master WHERE tbl_name=?");
}
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(usingMySQL ?
"SHOW TABLES LIKE ?" :
"SELECT tbl_name FROM sqlite_master WHERE tbl_name=?");
statement.setString(1, tableName);
set = statement.executeQuery();
return !set.next();
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -62,9 +59,9 @@ public class VersionTable extends Table {
public int getVersion() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()) {
int version = 0;
statement = prepareStatement("SELECT * FROM " + tableName);
statement = connection.prepareStatement("SELECT * FROM " + tableName);
set = statement.executeQuery();
if (set.next()) {
version = set.getInt("version");
@ -73,7 +70,6 @@ public class VersionTable extends Table {
} catch (Exception exc) {
Log.toLog("VersionsTable.getVersion", exc);
} finally {
endTransaction(statement);
close(set, statement);
}
@ -87,11 +83,11 @@ public class VersionTable extends Table {
public void setVersion(int version) throws SQLException {
removeAllData();
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " (version) VALUES (" + version + ")");
try (Connection connection = getConnection()) {
statement = connection.prepareStatement("INSERT INTO " + tableName + " (version) VALUES (" + version + ")");
statement.executeUpdate();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}

View File

@ -6,6 +6,7 @@ import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -56,8 +57,8 @@ public class WorldTable extends Table {
public List<String> getWorlds() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT * FROM " + tableName);
try (Connection connection = getConnection()) {
statement = connection.prepareStatement("SELECT * FROM " + tableName);
set = statement.executeQuery();
List<String> worldNames = new ArrayList<>();
while (set.next()) {
@ -66,7 +67,6 @@ public class WorldTable extends Table {
}
return worldNames;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -90,8 +90,8 @@ public class WorldTable extends Table {
}
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " ("
try (Connection connection = getConnection()) {
statement = connection.prepareStatement("INSERT INTO " + tableName + " ("
+ columnWorldName
+ ") VALUES (?)");
for (String world : worldsToSave) {
@ -100,7 +100,7 @@ public class WorldTable extends Table {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}

View File

@ -10,6 +10,7 @@ import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -86,8 +87,8 @@ public class WorldTimesTable extends UserIDTable {
db.getWorldTable().saveWorlds(worldNames);
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(insertStatement);
for (Map.Entry<String, GMTimes> entry : worldTimesMap.entrySet()) {
String worldName = entry.getKey();
@ -105,7 +106,7 @@ public class WorldTimesTable extends UserIDTable {
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -114,10 +115,10 @@ public class WorldTimesTable extends UserIDTable {
public void addWorldTimesToSessions(UUID uuid, Map<Integer, Session> sessions) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
columnSessionID + ", " +
columnSurvival + ", " +
columnCreative + ", " +
@ -153,7 +154,6 @@ public class WorldTimesTable extends UserIDTable {
session.getWorldTimes().setGMTimesForWorld(worldName, gmTimes);
}
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -165,12 +165,12 @@ public class WorldTimesTable extends UserIDTable {
public WorldTimes getWorldTimesOfServer(UUID serverUUID) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
String sessionIDColumn = sessionsTable + "." + sessionsTable.getColumnID();
String sessionServerIDColumn = sessionsTable + ".server_id";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
"SUM(" + columnSurvival + ") as survival, " +
"SUM(" + columnCreative + ") as creative, " +
"SUM(" + columnAdventure + ") as adventure, " +
@ -202,7 +202,6 @@ public class WorldTimesTable extends UserIDTable {
}
return worldTimes;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -210,10 +209,10 @@ public class WorldTimesTable extends UserIDTable {
public WorldTimes getWorldTimesOfUser(UUID uuid) throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
"SUM(" + columnSurvival + ") as survival, " +
"SUM(" + columnCreative + ") as creative, " +
"SUM(" + columnAdventure + ") as adventure, " +
@ -243,7 +242,6 @@ public class WorldTimesTable extends UserIDTable {
}
return worldTimes;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -280,8 +278,8 @@ public class WorldTimesTable extends UserIDTable {
db.getWorldTable().saveWorlds(worldNames);
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
try (Connection connection = getConnection()) {
statement = connection.prepareStatement(insertStatement);
String[] gms = GMTimes.getGMKeyArray();
for (Map<UUID, List<Session>> serverSessions : allSessions.values()) {
for (Map.Entry<UUID, List<Session>> entry : serverSessions.entrySet()) {
@ -305,7 +303,7 @@ public class WorldTimesTable extends UserIDTable {
}
}
statement.executeBatch();
commit(statement.getConnection());
connection.commit();
} finally {
close(statement);
}
@ -314,10 +312,10 @@ public class WorldTimesTable extends UserIDTable {
public Map<Integer, WorldTimes> getAllWorldTimesBySessionID() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
try (Connection connection = getConnection()) {
String worldIDColumn = worldTable + "." + worldTable.getColumnID();
String worldNameColumn = worldTable + "." + worldTable.getColumnWorldName() + " as world_name";
statement = prepareStatement("SELECT " +
statement = connection.prepareStatement("SELECT " +
columnSessionID + ", " +
columnSurvival + ", " +
columnCreative + ", " +
@ -350,7 +348,6 @@ public class WorldTimesTable extends UserIDTable {
}
return worldTimes;
} finally {
endTransaction(statement);
close(set, statement);
}
}