Added PreparedStatement queuing.

This commit is contained in:
Brianna 2020-12-09 11:09:31 -06:00
parent a7e55a9557
commit 66c43f06d7

View File

@ -4,6 +4,7 @@ import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@ -19,6 +20,7 @@ public class DataManagerAbstract {
protected final Plugin plugin;
private static Map<String, LinkedList<Runnable>> queues = new HashMap<>();
private static Map<String, LinkedList<PreparedStatement>> queuedStatements = new HashMap<>();
public DataManagerAbstract(DatabaseConnector databaseConnector, Plugin plugin) {
this.databaseConnector = databaseConnector;
@ -78,6 +80,29 @@ public class DataManagerAbstract {
Bukkit.getScheduler().runTask(this.plugin, runnable);
}
/**
* Queue PreparedStatements to be executed in a single database connection.
*
* @param statement statement to queue.
* @param queueKey the queue key to add the statement to.
* @param delay the delay between each queue execution.
*/
public void queueAsync(PreparedStatement statement, String queueKey, long delay) {
if (queues.get(queueKey) == null || queues.get(queueKey).isEmpty())
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
databaseConnector.connect(connection -> {
List<PreparedStatement> queue = queuedStatements.get(queueKey);
for (PreparedStatement stmt : queue)
stmt.executeBatch();
connection.commit();
queue.clear();
});
}, delay);
List<PreparedStatement> queue = queuedStatements.computeIfAbsent(queueKey, t -> new LinkedList<>());
queue.add(statement);
}
/**
* Queue tasks to be ran asynchronously.
*