diff --git a/src/main/java/org/maxgamer/quickshop/database/AbstractDatabaseCore.java b/src/main/java/org/maxgamer/quickshop/database/AbstractDatabaseCore.java index 44f5546a0..06fca842f 100644 --- a/src/main/java/org/maxgamer/quickshop/database/AbstractDatabaseCore.java +++ b/src/main/java/org/maxgamer/quickshop/database/AbstractDatabaseCore.java @@ -23,7 +23,23 @@ package org.maxgamer.quickshop.database; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + public abstract class AbstractDatabaseCore { + private final ReentrantLock lock = new ReentrantLock(true); + private final Condition conditionLock = lock.newCondition(); + + public void waitForConnection() { + try { + conditionLock.await(); + } catch (InterruptedException ignored) { + } + } + + public void signalForNewConnection() { + conditionLock.signal(); + } abstract void close(); diff --git a/src/main/java/org/maxgamer/quickshop/database/DatabaseConnection.java b/src/main/java/org/maxgamer/quickshop/database/DatabaseConnection.java index 5833ca65a..e901ae886 100644 --- a/src/main/java/org/maxgamer/quickshop/database/DatabaseConnection.java +++ b/src/main/java/org/maxgamer/quickshop/database/DatabaseConnection.java @@ -1,5 +1,7 @@ package org.maxgamer.quickshop.database; +import org.maxgamer.quickshop.QuickShop; + import java.sql.Connection; import java.sql.SQLException; @@ -47,7 +49,10 @@ public class DatabaseConnection implements AutoCloseable { } public void release() { - using = false; + if (using) { + using = false; + QuickShop.getInstance().getDatabaseManager().getDatabase().signalForNewConnection(); + } } public boolean isUsing() { diff --git a/src/main/java/org/maxgamer/quickshop/database/DatabaseManager.java b/src/main/java/org/maxgamer/quickshop/database/DatabaseManager.java index 1acf5ad1d..f572d10c3 100644 --- a/src/main/java/org/maxgamer/quickshop/database/DatabaseManager.java +++ b/src/main/java/org/maxgamer/quickshop/database/DatabaseManager.java @@ -148,59 +148,62 @@ public class DatabaseManager { /** * Internal method, runTasks in queue. */ - private synchronized void runTask() { // synchronized for QUICKSHOP-WX - if (sqlQueue.isEmpty()) { - return; - } - DatabaseConnection dbconnection = this.database.getConnection(); - Connection connection = dbconnection.get(); - try { - //start our commit - connection.setAutoCommit(false); - Timer ctimer = new Timer(true); - while (true) { - if (!dbconnection.isValid()) { - warningSender.sendWarn("Database connection may lost, we are trying reconnecting, if this message appear too many times, you should check your database file(sqlite) and internet connection(mysql)."); - //connection isn't stable, let autocommit on - dbconnection.release(); - dbconnection.close(); - return; // Waiting next crycle and hope it success reconnected. - } + private void runTask() { // synchronized for QUICKSHOP-WX + synchronized (sqlQueue) { + if (sqlQueue.isEmpty()) { + return; + } + DatabaseConnection dbconnection = this.database.getConnection(); + Connection connection = dbconnection.get(); + try { + //start our commit + connection.setAutoCommit(false); + Timer ctimer = new Timer(true); + while (true) { + if (!dbconnection.isValid()) { + warningSender.sendWarn("Database connection may lost, we are trying reconnecting, if this message appear too many times, you should check your database file(sqlite) and internet connection(mysql)."); + //connection isn't stable, let autocommit on + dbconnection.release(); + dbconnection.close(); + return; // Waiting next crycle and hope it success reconnected. + } - Timer timer = new Timer(true); - DatabaseTask task = sqlQueue.poll(); - if (task == null) { - break; - } - Util.debugLog("Executing the SQL task: " + task); + Timer timer = new Timer(true); + DatabaseTask task = sqlQueue.poll(); + if (task == null) { + break; + } + Util.debugLog("Executing the SQL task: " + task); - task.run(connection); - long tookTime = timer.endTimer(); - if (tookTime > 300) { + task.run(connection); + long tookTime = timer.endTimer(); + if (tookTime > 300) { + warningSender.sendWarn( + "Database performance warning: It took too long time (" + + tookTime + + "ms) to execute the task, it may cause the network connection with MySQL server or just MySQL server too slow, change to a better MySQL server or switch to a local SQLite database!"); + } + } + if (!connection.getAutoCommit()) { + connection.commit(); + connection.setAutoCommit(true); + } + long tookTime = ctimer.endTimer(); + if (tookTime > 5500) { warningSender.sendWarn( "Database performance warning: It took too long time (" + tookTime + "ms) to execute the task, it may cause the network connection with MySQL server or just MySQL server too slow, change to a better MySQL server or switch to a local SQLite database!"); } + + } catch (SQLException sqle) { + plugin.getSentryErrorReporter().ignoreThrow(); + this.plugin + .getLogger() + .log(Level.WARNING, "Database connection may lost, we are trying reconnecting, if this message appear too many times, you should check your database file(sqlite) and internet connection(mysql).", sqle); + } finally { + dbconnection.release(); } - if (!connection.getAutoCommit()) { - connection.commit(); - connection.setAutoCommit(true); - } - long tookTime = ctimer.endTimer(); - if (tookTime > 5500) { - warningSender.sendWarn( - "Database performance warning: It took too long time (" - + tookTime - + "ms) to execute the task, it may cause the network connection with MySQL server or just MySQL server too slow, change to a better MySQL server or switch to a local SQLite database!"); - } - } catch (SQLException sqle) { - plugin.getSentryErrorReporter().ignoreThrow(); - this.plugin - .getLogger() - .log(Level.WARNING, "Database connection may lost, we are trying reconnecting, if this message appear too many times, you should check your database file(sqlite) and internet connection(mysql).", sqle); - } finally { - dbconnection.release(); } // try { @@ -231,7 +234,9 @@ public class DatabaseManager { */ public void addDelayTask(DatabaseTask task) { if (useQueue) { - sqlQueue.offer(task); + synchronized (sqlQueue) { + sqlQueue.offer(task); + } } else { runInstantTask(task); } @@ -240,7 +245,7 @@ public class DatabaseManager { /** * Unload the DatabaseManager, run at onDisable() */ - public void unInit() { + public synchronized void unInit() { if (task != null && !task.isCancelled()) { task.cancel(); } diff --git a/src/main/java/org/maxgamer/quickshop/database/MySQLCore.java b/src/main/java/org/maxgamer/quickshop/database/MySQLCore.java index 7c3bfd391..c106d7fc2 100644 --- a/src/main/java/org/maxgamer/quickshop/database/MySQLCore.java +++ b/src/main/java/org/maxgamer/quickshop/database/MySQLCore.java @@ -123,9 +123,9 @@ public class MySQLCore extends AbstractDatabaseCore { } } - //If all connection is unusable, return null - return null; - + //If all connection is unusable, wait a moment + waitForConnection(); + return getConnection(); } @Override diff --git a/src/main/java/org/maxgamer/quickshop/database/SQLiteCore.java b/src/main/java/org/maxgamer/quickshop/database/SQLiteCore.java index 8fec98019..da7475493 100644 --- a/src/main/java/org/maxgamer/quickshop/database/SQLiteCore.java +++ b/src/main/java/org/maxgamer/quickshop/database/SQLiteCore.java @@ -80,7 +80,9 @@ public class SQLiteCore extends AbstractDatabaseCore { return connection = genConnection(); } } - return null; + //If all connection is unusable, wait a moment + waitForConnection(); + return getConnection(); } @Nullable