Improve transaction handling

This commit is contained in:
Heliosares
2024-01-05 12:02:16 -05:00
parent ec3c4d01fe
commit debdff00d2
3 changed files with 27 additions and 11 deletions

View File

@@ -21,7 +21,6 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
@@ -252,7 +251,15 @@ public class ConnectionPool {
connection = newConnectionSupplier.get();
}
try {
return task.apply(connection);
var out = task.apply(connection);
if (!connection.getAutoCommit() && lock.getHoldCount() == 1) {
if (plugin.getAPConfig().getDebug() > 0) {
plugin.warning("Auto-commit not re-enabled following transaction.");
Thread.dumpStack();
}
connection.setAutoCommit(true);
}
return out;
} finally {
if (lock.getHoldCount() == 1) {
writeTimeIndex++;
@@ -303,13 +310,18 @@ public class ConnectionPool {
* @see PreparedStatement#executeUpdate()
*/
public int executeReturnRows(String stmt, Object... args) throws SQLException, BusyException {
return executeReturn(connection -> executeReturnRows(connection, stmt, args), 30000L, Integer.class);
}
/**
* @see PreparedStatement#executeUpdate()
*/
public int executeReturnRows(Connection connection, String stmt, Object... args) throws SQLException {
debugSQLStatement(stmt, args);
return executeReturn(connection -> {
try (PreparedStatement pstmt = connection.prepareStatement(stmt)) {
prepare(connection, pstmt, args);
return pstmt.executeUpdate();
}
}, 30000L, Integer.class);
try (PreparedStatement pstmt = connection.prepareStatement(stmt)) {
prepare(connection, pstmt, args);
return pstmt.executeUpdate();
}
}
public int executeReturnGenerated(String stmt, Object... args) throws SQLException, BusyException {

View File

@@ -307,13 +307,13 @@ public class SQLManager extends ConnectionPool {
setLast(LastKeys.LEGACY_POSITIONS, System.currentTimeMillis(), connection);
connection.commit();
plugin.debug("init done.");
plugin.debug("table init done.");
} catch (Throwable t) {
plugin.warning("An error occurred during initialization. Rolling back changes.");
connection.rollback();
throw t;
} finally {
connection.setAutoCommit(true);
if (!connection.getAutoCommit()) connection.setAutoCommit(true);
}
}
@@ -335,7 +335,7 @@ public class SQLManager extends ConnectionPool {
}
// Step 3: Delete the UIDs
int count_ = executeReturnRows("DELETE FROM auxprotect_uids WHERE uid IN (SELECT auxprotect_uids.uid FROM auxprotect_uids LEFT JOIN temp_uids AS temp ON auxprotect_uids.uid = temp.uid WHERE temp.uid IS NULL)");
int count_ = executeReturnRows(connection, "DELETE FROM auxprotect_uids WHERE uid IN (SELECT auxprotect_uids.uid FROM auxprotect_uids LEFT JOIN temp_uids AS temp ON auxprotect_uids.uid = temp.uid WHERE temp.uid IS NULL)");
// Step 4: Drop the Temporary Table
execute("DROP " + (isMySQL() ? "TEMPORARY " : "") + "TABLE IF EXISTS temp_uids", connection);
@@ -346,6 +346,8 @@ public class SQLManager extends ConnectionPool {
} catch (Throwable t) {
connection.rollback();
throw t;
} finally {
if (!connection.getAutoCommit()) connection.setAutoCommit(true);
}
}, 30000L, Integer.class);

View File

@@ -42,6 +42,7 @@ import dev.heliosares.auxprotect.utils.Telemetry;
import dev.heliosares.auxprotect.utils.UpdateChecker;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Container;
@@ -653,6 +654,7 @@ public final class AuxProtectSpigot extends JavaPlugin implements IAuxProtect {
@Override
public void info(String string) {
string = ChatColor.stripColor(string);
this.getLogger().info(string);
logToStackLog("[INFO] " + string);
}