mirror of
https://github.com/EarthMC/QuickShop-Reremake.git
synced 2026-11-24 17:39:52 -05:00
Fix database statement never close bugs
Former-commit-id: c8fa79f37121bcc1a9fd6c94967b5260d19a9c4f
This commit is contained in:
1 parent
376e1e1c2f
commit
5dc534a4dc
3 files changed
+83
-80
No files matched your search
@@ -19,6 +19,7 @@
|
||||
|
||||
package org.maxgamer.quickshop.Database;
|
||||
|
||||
import lombok.Cleanup;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.maxgamer.quickshop.QuickShop;
|
||||
import org.maxgamer.quickshop.Util.Util;
|
||||
@@ -27,10 +28,6 @@ import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class Database {
|
||||
|
||||
@@ -69,80 +66,80 @@ public class Database {
|
||||
this.core.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the contents of this database into the given database. Does not
|
||||
* delete the contents of this database, or change any settings. This may
|
||||
* take a long time, and will print out progress reports to System.out
|
||||
* <p>
|
||||
* This method does not create the tables in the new database. You need to
|
||||
* do that yourself.
|
||||
*
|
||||
* @param db The database to copy data to
|
||||
* @throws SQLException if an error occurs.
|
||||
*/
|
||||
@Deprecated /* Buggy, owner pls use Database Tools to migrate */
|
||||
public void copyTo(@NotNull Database db) throws SQLException {
|
||||
ResultSet rs = getConnection().getMetaData().getTables(null, null, "%", null);
|
||||
List<String> tables = new LinkedList<>();
|
||||
while (rs.next()) {
|
||||
tables.add(rs.getString("TABLE_NAME"));
|
||||
}
|
||||
rs.close();
|
||||
core.flush();
|
||||
// For each table
|
||||
String prefix = plugin.getConfig().getString("database.prefix");
|
||||
for (String table : tables) {
|
||||
if (table.contains("schedule")) {
|
||||
return; // go way!
|
||||
}
|
||||
String finalTable;
|
||||
if (table.startsWith(Objects.requireNonNull(prefix))) {
|
||||
finalTable = table;
|
||||
} else {
|
||||
finalTable = prefix + table;
|
||||
plugin.getLogger().info("CovertHelper: Fixed table name from SQLite " + table + " to MySQL " + finalTable);
|
||||
}
|
||||
|
||||
if (table.toLowerCase().startsWith("sqlite_autoindex_")) {
|
||||
continue;
|
||||
}
|
||||
plugin.getLogger().log(Level.WARNING, "Copying " + table + " to " + finalTable);
|
||||
// Wipe the old records
|
||||
db.getConnection().prepareStatement("DELETE FROM " + finalTable).execute();
|
||||
// Fetch all the data from the existing database
|
||||
rs = getConnection().prepareStatement("SELECT * FROM " + table).executeQuery();
|
||||
int n = 0;
|
||||
// Build the query
|
||||
StringBuilder query = new StringBuilder("INSERT INTO " + finalTable + " VALUES (");
|
||||
// Append another placeholder for the value
|
||||
query.append("?");
|
||||
for (int i = 2; i <= rs.getMetaData().getColumnCount(); i++) {
|
||||
// Add the rest of the placeholders and values. This is so we
|
||||
// have (?, ?, ?) and not (?, ?, ?, ).
|
||||
query.append(", ?");
|
||||
}
|
||||
// End the query
|
||||
query.append(")");
|
||||
PreparedStatement ps = db.getConnection().prepareStatement(query.toString());
|
||||
while (rs.next()) {
|
||||
n++;
|
||||
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
|
||||
ps.setObject(i, rs.getObject(i));
|
||||
}
|
||||
ps.addBatch();
|
||||
if (n % 100 == 0) {
|
||||
ps.executeBatch();
|
||||
plugin.getLogger().log(Level.WARNING, n + " records copied...");
|
||||
}
|
||||
}
|
||||
ps.executeBatch();
|
||||
// Close the resultset of that table
|
||||
rs.close();
|
||||
}
|
||||
// Success!
|
||||
db.getConnection().close();
|
||||
this.getConnection().close();
|
||||
}
|
||||
// /**
|
||||
// * Copies the contents of this database into the given database. Does not
|
||||
// * delete the contents of this database, or change any settings. This may
|
||||
// * take a long time, and will print out progress reports to System.out
|
||||
// * <p>
|
||||
// * This method does not create the tables in the new database. You need to
|
||||
// * do that yourself.
|
||||
// *
|
||||
// * @param db The database to copy data to
|
||||
// * @throws SQLException if an error occurs.
|
||||
// */
|
||||
// @Deprecated /* Buggy, owner pls use Database Tools to migrate */
|
||||
// public void copyTo(@NotNull Database db) throws SQLException {
|
||||
// ResultSet rs = getConnection().getMetaData().getTables(null, null, "%", null);
|
||||
// List<String> tables = new LinkedList<>();
|
||||
// while (rs.next()) {
|
||||
// tables.add(rs.getString("TABLE_NAME"));
|
||||
// }
|
||||
// rs.close();
|
||||
// core.flush();
|
||||
// // For each table
|
||||
// String prefix = plugin.getConfig().getString("database.prefix");
|
||||
// for (String table : tables) {
|
||||
// if (table.contains("schedule")) {
|
||||
// return; // go way!
|
||||
// }
|
||||
// String finalTable;
|
||||
// if (table.startsWith(Objects.requireNonNull(prefix))) {
|
||||
// finalTable = table;
|
||||
// } else {
|
||||
// finalTable = prefix + table;
|
||||
// plugin.getLogger().info("CovertHelper: Fixed table name from SQLite " + table + " to MySQL " + finalTable);
|
||||
// }
|
||||
//
|
||||
// if (table.toLowerCase().startsWith("sqlite_autoindex_")) {
|
||||
// continue;
|
||||
// }
|
||||
// plugin.getLogger().log(Level.WARNING, "Copying " + table + " to " + finalTable);
|
||||
// // Wipe the old records
|
||||
// db.getConnection().prepareStatement("DELETE FROM " + finalTable).execute();
|
||||
// // Fetch all the data from the existing database
|
||||
// rs = getConnection().prepareStatement("SELECT * FROM " + table).executeQuery();
|
||||
// int n = 0;
|
||||
// // Build the query
|
||||
// StringBuilder query = new StringBuilder("INSERT INTO " + finalTable + " VALUES (");
|
||||
// // Append another placeholder for the value
|
||||
// query.append("?");
|
||||
// for (int i = 2; i <= rs.getMetaData().getColumnCount(); i++) {
|
||||
// // Add the rest of the placeholders and values. This is so we
|
||||
// // have (?, ?, ?) and not (?, ?, ?, ).
|
||||
// query.append(", ?");
|
||||
// }
|
||||
// // End the query
|
||||
// query.append(")");
|
||||
// PreparedStatement ps = db.getConnection().prepareStatement(query.toString());
|
||||
// while (rs.next()) {
|
||||
// n++;
|
||||
// for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
|
||||
// ps.setObject(i, rs.getObject(i));
|
||||
// }
|
||||
// ps.addBatch();
|
||||
// if (n % 100 == 0) {
|
||||
// ps.executeBatch();
|
||||
// plugin.getLogger().log(Level.WARNING, n + " records copied...");
|
||||
// }
|
||||
// }
|
||||
// ps.executeBatch();
|
||||
// // Close the resultset of that table
|
||||
// rs.close();
|
||||
// }
|
||||
// // Success!
|
||||
// db.getConnection().close();
|
||||
// this.getConnection().close();
|
||||
// }
|
||||
|
||||
/**
|
||||
* Executes the given statement either immediately, or soon.
|
||||
@@ -170,6 +167,7 @@ public class Database {
|
||||
}
|
||||
String query = "SELECT * FROM " + table + " LIMIT 0,1";
|
||||
try {
|
||||
@Cleanup
|
||||
PreparedStatement ps = this.getConnection().prepareStatement(query);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
while (rs.next()) {
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
package org.maxgamer.quickshop.Database;
|
||||
|
||||
import lombok.Cleanup;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.maxgamer.quickshop.QuickShop;
|
||||
@@ -121,6 +122,7 @@ public class DatabaseHelper {
|
||||
* @throws SQLException If the connection is invalid
|
||||
*/
|
||||
private boolean createMessagesTable() throws SQLException {
|
||||
@Cleanup
|
||||
Statement st = db.getConnection().createStatement();
|
||||
String createTable = "CREATE TABLE " + QuickShop.instance.getDbPrefix()
|
||||
+ "messages (owner VARCHAR(255) NOT NULL, message TEXT(25) NOT NULL, time BIGINT(32) NOT NULL );";
|
||||
@@ -156,6 +158,7 @@ public class DatabaseHelper {
|
||||
* @throws SQLException If the connection is invalid.
|
||||
*/
|
||||
private void createShopsTable() throws SQLException {
|
||||
@Cleanup
|
||||
Statement st = db.getConnection().createStatement();
|
||||
String createTable = "CREATE TABLE " + QuickShop.instance
|
||||
.getDbPrefix() + "shops (owner VARCHAR(255) NOT NULL, price double(32, 2) NOT NULL, itemConfig TEXT CHARSET utf8 NOT NULL, x INTEGER(32) NOT NULL, y INTEGER(32) NOT NULL, z INTEGER(32) NOT NULL, world VARCHAR(32) NOT NULL, unlimited boolean, type boolean, PRIMARY KEY (x, y, z, world) );";
|
||||
@@ -171,7 +174,7 @@ public class DatabaseHelper {
|
||||
.getDbPrefix() + "shops WHERE x = ? AND y = ? AND z = ? AND world = ?" + (db.getCore() instanceof MySQLCore ?
|
||||
" LIMIT 1" :
|
||||
"");
|
||||
|
||||
@Cleanup
|
||||
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
|
||||
ps.setInt(1, x);
|
||||
ps.setInt(2, y);
|
||||
@@ -181,12 +184,14 @@ public class DatabaseHelper {
|
||||
}
|
||||
|
||||
public ResultSet selectAllMessages() throws SQLException {
|
||||
@Cleanup
|
||||
Statement st = db.getConnection().createStatement();
|
||||
String selectAllShops = "SELECT * FROM " + QuickShop.instance.getDbPrefix() + "messages";
|
||||
return st.executeQuery(selectAllShops);
|
||||
}
|
||||
|
||||
public ResultSet selectAllShops() throws SQLException {
|
||||
@Cleanup
|
||||
Statement st = db.getConnection().createStatement();
|
||||
String selectAllShops = "SELECT * FROM " + QuickShop.instance.getDbPrefix() + "shops";
|
||||
return st.executeQuery(selectAllShops);
|
||||
|
||||
@@ -41,7 +41,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.Locale;
|
||||
|
||||
public class GameLanguage extends Thread {
|
||||
private @Nullable JsonObject lang = null;
|
||||
private @Nullable JsonObject lang;
|
||||
private GameLanguageLoadThread loadThread;
|
||||
|
||||
@SneakyThrows
|
||||
|
||||
Reference in new issue
Block a user