Introduce SQL query timeouts

This commit is contained in:
AppleDash 2017-01-20 00:27:52 -05:00
parent cf60c7bbbe
commit 8ab8290b77
5 changed files with 24 additions and 10 deletions

View File

@ -2,8 +2,8 @@ package org.appledash.saneeconomy.economy.backend.type;
import org.appledash.saneeconomy.SaneEconomy;
import org.appledash.saneeconomy.economy.economable.Economable;
import org.appledash.saneeconomy.utils.DatabaseCredentials;
import org.appledash.saneeconomy.utils.MySQLConnection;
import org.appledash.saneeconomy.utils.database.DatabaseCredentials;
import org.appledash.saneeconomy.utils.database.MySQLConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -101,7 +101,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
waitUntilFlushed();
createTables();
try (Connection conn = dbConn.openConnection()) {
PreparedStatement ps = conn.prepareStatement(String.format("SELECT * FROM `%s`", dbConn.getTable("saneeconomy_balances")));
PreparedStatement ps = dbConn.prepareStatement(conn, String.format("SELECT * FROM `%s`", dbConn.getTable("saneeconomy_balances")));
ResultSet rs = ps.executeQuery();
balances.clear();
@ -122,7 +122,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
dbConn.executeAsyncOperation((conn) -> {
try {
ensureAccountExists(economable, conn);
PreparedStatement statement = conn.prepareStatement(String.format("UPDATE `%s` SET balance = ? WHERE `unique_identifier` = ?", dbConn.getTable("saneeconomy_balances")));
PreparedStatement statement = dbConn.prepareStatement(conn, String.format("UPDATE `%s` SET balance = ? WHERE `unique_identifier` = ?", dbConn.getTable("saneeconomy_balances")));
statement.setDouble(1, newBalance);
statement.setString(2, economable.getUniqueIdentifier());
statement.executeUpdate();
@ -135,14 +135,14 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
private synchronized void ensureAccountExists(Economable economable, Connection conn) throws SQLException {
if (!accountExists(economable, conn)) {
PreparedStatement statement = conn.prepareStatement(String.format("INSERT INTO `%s` (unique_identifier, balance) VALUES (?, 0.0)", dbConn.getTable("saneeconomy_balances")));
PreparedStatement statement = dbConn.prepareStatement(conn, String.format("INSERT INTO `%s` (unique_identifier, balance) VALUES (?, 0.0)", dbConn.getTable("saneeconomy_balances")));
statement.setString(1, economable.getUniqueIdentifier());
statement.executeUpdate();
}
}
private synchronized boolean accountExists(Economable economable, Connection conn) throws SQLException {
PreparedStatement statement = conn.prepareStatement(String.format("SELECT 1 FROM `%s` WHERE `unique_identifier` = ?", dbConn.getTable("saneeconomy_balances")));
PreparedStatement statement = dbConn.prepareStatement(conn, String.format("SELECT 1 FROM `%s` WHERE `unique_identifier` = ?", dbConn.getTable("saneeconomy_balances")));
statement.setString(1, economable.getUniqueIdentifier());
ResultSet rs = statement.executeQuery();

View File

@ -2,8 +2,8 @@ package org.appledash.saneeconomy.economy.logger;
import org.appledash.saneeconomy.economy.transaction.Transaction;
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
import org.appledash.saneeconomy.utils.DatabaseCredentials;
import org.appledash.saneeconomy.utils.MySQLConnection;
import org.appledash.saneeconomy.utils.database.DatabaseCredentials;
import org.appledash.saneeconomy.utils.database.MySQLConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;

View File

@ -10,6 +10,7 @@ import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendMySQL
import org.appledash.saneeconomy.economy.economable.EconomableGeneric;
import org.appledash.saneeconomy.economy.logger.TransactionLogger;
import org.appledash.saneeconomy.economy.logger.TransactionLoggerMySQL;
import org.appledash.saneeconomy.utils.database.DatabaseCredentials;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection;

View File

@ -1,4 +1,4 @@
package org.appledash.saneeconomy.utils;
package org.appledash.saneeconomy.utils.database;
/**
* Created by appledash on 9/18/16.

View File

@ -1,10 +1,11 @@
package org.appledash.saneeconomy.utils;
package org.appledash.saneeconomy.utils.database;
import org.appledash.saneeconomy.SaneEconomy;
import org.bukkit.Bukkit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
@ -36,6 +37,18 @@ public class MySQLConnection {
}
}
public PreparedStatement prepareStatement(Connection conn, String sql) throws SQLException {
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setQueryTimeout(5000); // 5 second timeout
return preparedStatement;
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
return prepareStatement(openConnection(), sql);
}
public boolean testConnection() {
try (Connection ignored = openConnection()) {
return true;