Fix database lock error

This commit is contained in:
Jesse Boyd 2016-11-13 22:17:17 +11:00
parent f8b1fcffa1
commit 2ec0b55482
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F

View File

@ -108,24 +108,37 @@ public class SQLManager implements AbstractDB {
this.plotTasks = new ConcurrentHashMap<>(); this.plotTasks = new ConcurrentHashMap<>();
this.playerTasks = new ConcurrentHashMap<>(); this.playerTasks = new ConcurrentHashMap<>();
this.clusterTasks = new ConcurrentHashMap<>(); this.clusterTasks = new ConcurrentHashMap<>();
this.prefix = p;
this.SET_OWNER = "UPDATE `" + this.prefix + "plot` SET `owner` = ? WHERE `plot_id_x` = ? AND `plot_id_z` = ? AND `world` = ?";
this.GET_ALL_PLOTS = "SELECT `id`, `plot_id_x`, `plot_id_z`, `world` FROM `" + this.prefix + "plot`";
this.CREATE_PLOTS = "INSERT INTO `" + this.prefix + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`, `timestamp`) values ";
this.CREATE_SETTINGS = "INSERT INTO `" + this.prefix + "plot_settings` (`plot_plot_id`) values ";
this.CREATE_TIERS = "INSERT INTO `" + this.prefix + "plot_%tier%` (`plot_plot_id`, `user_uuid`) values ";
this.CREATE_PLOT = "INSERT INTO `" + this.prefix + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`, `timestamp`) VALUES(?, ?, ?, ?, ?)";
this.CREATE_CLUSTER =
"INSERT INTO `" + this.prefix + "cluster`(`pos1_x`, `pos1_z`, `pos2_x`, `pos2_z`, `owner`, `world`) VALUES(?, ?, ?, ?, ?, ?)";
TaskManager.runTaskAsync(new Runnable() { TaskManager.runTaskAsync(new Runnable() {
@Override @Override
public void run() { public void run() {
try {
createTables();
} catch (SQLException e) {
e.printStackTrace();
}
long last = System.currentTimeMillis(); long last = System.currentTimeMillis();
while (true) { while (true) {
if (SQLManager.this.closed) { if (SQLManager.this.closed) {
break; break;
} }
// schedule reconnect boolean hasTask = !globalTasks.isEmpty() || !playerTasks.isEmpty() || !plotTasks.isEmpty() || !clusterTasks.isEmpty();
if (SQLManager.this.mySQL && System.currentTimeMillis() - last > 550000) { if (hasTask) {
last = System.currentTimeMillis();
try { try {
close(); if (SQLManager.this.mySQL && System.currentTimeMillis() - last > 550000 || !connection.isValid(10000)) {
SQLManager.this.closed = false; last = System.currentTimeMillis();
SQLManager.this.connection = database.forceConnection(); reconnect();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
} }
} catch (SQLException impossible) {
impossible.printStackTrace();
} }
if (!sendBatch()) { if (!sendBatch()) {
try { try {
@ -140,22 +153,26 @@ public class SQLManager implements AbstractDB {
e.printStackTrace(); e.printStackTrace();
} }
} }
} else {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
} }
}); });
this.prefix = p; }
// Set timout
// setTimout(); public void reconnect() {
// Public final try {
this.SET_OWNER = "UPDATE `" + this.prefix + "plot` SET `owner` = ? WHERE `plot_id_x` = ? AND `plot_id_z` = ? AND `world` = ?"; close();
this.GET_ALL_PLOTS = "SELECT `id`, `plot_id_x`, `plot_id_z`, `world` FROM `" + this.prefix + "plot`"; SQLManager.this.closed = false;
this.CREATE_PLOTS = "INSERT INTO `" + this.prefix + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`, `timestamp`) values "; SQLManager.this.connection = database.forceConnection();
this.CREATE_SETTINGS = "INSERT INTO `" + this.prefix + "plot_settings` (`plot_plot_id`) values "; } catch (SQLException | ClassNotFoundException e) {
this.CREATE_TIERS = "INSERT INTO `" + this.prefix + "plot_%tier%` (`plot_plot_id`, `user_uuid`) values "; e.printStackTrace();
this.CREATE_PLOT = "INSERT INTO `" + this.prefix + "plot`(`plot_id_x`, `plot_id_z`, `owner`, `world`, `timestamp`) VALUES(?, ?, ?, ?, ?)"; }
this.CREATE_CLUSTER =
"INSERT INTO `" + this.prefix + "cluster`(`pos1_x`, `pos1_z`, `pos2_x`, `pos2_z`, `owner`, `world`) VALUES(?, ?, ?, ?, ?, ?)";
createTables();
} }
public synchronized Queue<Runnable> getGlobalTasks() { public synchronized Queue<Runnable> getGlobalTasks() {