Finish database stuff

This commit is contained in:
Len 2022-08-13 16:10:23 +02:00
parent 819cbdcd93
commit 0f3ec2c575
3 changed files with 37 additions and 8 deletions

View File

@ -8,6 +8,7 @@ import com.alttd.playershops.listener.BlockListener;
import com.alttd.playershops.listener.PlayerListener; import com.alttd.playershops.listener.PlayerListener;
import com.alttd.playershops.listener.ShopListener; import com.alttd.playershops.listener.ShopListener;
import com.alttd.playershops.shop.ShopType; import com.alttd.playershops.shop.ShopType;
import com.alttd.playershops.storage.DatabaseManager;
import lombok.Getter; import lombok.Getter;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -22,6 +23,8 @@ public class PlayerShops extends JavaPlugin {
private Economy econ = null; private Economy econ = null;
@Getter @Getter
private ShopHandler shopHandler; private ShopHandler shopHandler;
@Getter
private DatabaseManager databaseManager;
private ShopListener shopListener; private ShopListener shopListener;
private PlayerListener playerListener; private PlayerListener playerListener;
@ -36,7 +39,7 @@ public class PlayerShops extends JavaPlugin {
} }
Bukkit.getLogger().info("Hooked into Vault economy provided by " + econ.getName()); Bukkit.getLogger().info("Hooked into Vault economy provided by " + econ.getName());
reloadConfigs(); reloadConfigs();
databaseManager = new DatabaseManager(instance);
shopHandler = new ShopHandler(instance); shopHandler = new ShopHandler(instance);
registerListeners(); registerListeners();
@ -46,6 +49,8 @@ public class PlayerShops extends JavaPlugin {
public void onDisable() { public void onDisable() {
unRegisterListeners(); unRegisterListeners();
Bukkit.getScheduler().cancelTasks(this); Bukkit.getScheduler().cancelTasks(this);
databaseManager.unload();
} }
private boolean setupEconomy() { private boolean setupEconomy() {

View File

@ -8,7 +8,6 @@ import org.bukkit.scheduler.BukkitRunnable;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -52,6 +51,35 @@ public class DatabaseManager {
return connection; return connection;
} }
private void closeDatabaseConnections() {
for (DatabaseConnection connection : CONNECTIONPOOL) {
if (connection == null || connection.isValid())
continue;
if (!connection.isActive()) {
connection.close();
} else {
while (connection.isActive) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// This should not be interrupted as this is saving all the shops in the background for us.
e.printStackTrace();
}
}
connection.close();
}
}
}
public void unload() {
if (databaseQueue != null && !databaseQueue.isCancelled()) {
databaseQueue.cancel();
databaseQueue.runTaskQueue();
}
closeDatabaseConnections();
}
class DatabaseConnection implements AutoCloseable { class DatabaseConnection implements AutoCloseable {
private Connection connection; private Connection connection;
private volatile boolean isActive; private volatile boolean isActive;
@ -140,10 +168,10 @@ public class DatabaseManager {
@Override @Override
public void run() { public void run() {
runTask(); runTaskQueue();
} }
public synchronized void runTask() { public synchronized void runTaskQueue() {
if (databaseQueryQueue.isEmpty()) if (databaseQueryQueue.isEmpty())
return; return;

View File

@ -1,4 +0,0 @@
package com.alttd.playershops.storage;
public class StorageManager {
}