mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2024-11-22 18:16:11 +01:00
Introduce SQL query timeouts
This commit is contained in:
parent
cf60c7bbbe
commit
8ab8290b77
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.appledash.saneeconomy.utils;
|
||||
package org.appledash.saneeconomy.utils.database;
|
||||
|
||||
/**
|
||||
* Created by appledash on 9/18/16.
|
@ -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;
|
Loading…
Reference in New Issue
Block a user