Version bump & make some changes to the way table locking is used in MySQL.

This commit is contained in:
AppleDash 2020-03-20 11:02:45 -04:00
parent afa4e9d36e
commit c47524f863
7 changed files with 37 additions and 6 deletions

View File

@ -9,7 +9,7 @@
<version>0</version>
</parent>
<artifactId>SaneEconomyCore</artifactId>
<version>0.17.1-SNAPSHOT</version>
<version>0.17.2-SNAPSHOT</version>
<dependencies>
<dependency>

View File

@ -121,13 +121,13 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
this.dbConn.executeAsyncOperation("set_balance_" + economable.getUniqueIdentifier(), (conn) -> {
try {
this.ensureAccountExists(economable, conn);
conn.prepareStatement("LOCK TABLE " + this.dbConn.getTable(SANEECONOMY_BALANCES) + " WRITE").execute();
this.dbConn.lockTable(conn, SANEECONOMY_BALANCES);
PreparedStatement statement = this.dbConn.prepareStatement(conn, String.format("UPDATE `%s` SET balance = ?, last_name = ? WHERE `unique_identifier` = ?", this.dbConn.getTable(SANEECONOMY_BALANCES)));
statement.setString(1, newBalance.toString());
statement.setString(2, economable.getName());
statement.setString(3, economable.getUniqueIdentifier());
statement.executeUpdate();
conn.prepareStatement("UNLOCK TABLES").execute();
this.dbConn.unlockTables(conn);
} catch (Exception e) {
this.balances.put(economable.getUniqueIdentifier(), oldBalance);
throw new RuntimeException("SQL error has occurred.", e);

View File

@ -18,10 +18,17 @@ public class MySQLConnection {
public static final int FIVE_SECONDS = 5000;
private final DatabaseCredentials dbCredentials;
private final SaneDatabase saneDatabase;
private boolean canLockTables = true;
public MySQLConnection(DatabaseCredentials dbCredentials) {
this.dbCredentials = dbCredentials;
this.saneDatabase = new SaneDatabase(dbCredentials);
try (Connection conn = this.saneDatabase.getConnection()){
} catch (SQLException e) {
this.canLockTables = false;
}
}
public Connection openConnection() {
@ -49,6 +56,27 @@ public class MySQLConnection {
}
}
public void lockTable(Connection conn, String tableName) throws SQLException {
if (!this.canLockTables) {
return;
}
try {
conn.prepareStatement("LOCK TABLE " + this.getTable(tableName) + " WRITE").execute();
this.canLockTables = true;
} catch (SQLException e) {
this.canLockTables = false;
}
}
public void unlockTables(Connection conn) throws SQLException {
if (!this.canLockTables) {
return;
}
conn.prepareStatement("UNLOCK TABLES").execute();
}
public void executeAsyncOperation(String tag, Consumer<Connection> callback) {
this.saneDatabase.runDatabaseOperationAsync(tag, () -> this.doExecuteAsyncOperation(1, callback));
}

View File

@ -7,6 +7,9 @@
# The order of placeholders can be changed. Anything after the :, like the '02d' in {1:02d} is a Java String.format specifier. If you don't know what that is, I recommend leaving it as-is.
# IMPORTANT: If your translation has a colon ( : ) character inside of it, you must enclose the entire part after "translation: " in single quotes ( ' ).
# If this file doesn't work for some reason, check your console for errors with "SnakeYAML" included in them.
####################################################################################################
########## READ ABOVE IF YOU INTEND TO EDIT THIS FILE, BEFORE ASKING FOR HELP! #####################
####################################################################################################
messages:
- message: "You don't have permission to check the balance of {1}."
- message: "That player is not online."

View File

@ -16,7 +16,7 @@
<dependency>
<groupId>org.appledash</groupId>
<artifactId>SaneEconomyCore</artifactId>
<version>0.17.1-SNAPSHOT</version>
<version>0.17.2-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -17,7 +17,7 @@
<dependency>
<groupId>org.appledash</groupId>
<artifactId>SaneEconomyCore</artifactId>
<version>0.17.1-SNAPSHOT</version>
<version>0.17.2-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -16,7 +16,7 @@
<dependency>
<groupId>org.appledash</groupId>
<artifactId>SaneEconomyCore</artifactId>
<version>0.17.1-SNAPSHOT</version>
<version>0.17.2-SNAPSHOT</version>
</dependency>
</dependencies>