Finish database stuff
This commit is contained in:
parent
819cbdcd93
commit
0f3ec2c575
|
|
@ -8,6 +8,7 @@ import com.alttd.playershops.listener.BlockListener;
|
|||
import com.alttd.playershops.listener.PlayerListener;
|
||||
import com.alttd.playershops.listener.ShopListener;
|
||||
import com.alttd.playershops.shop.ShopType;
|
||||
import com.alttd.playershops.storage.DatabaseManager;
|
||||
import lombok.Getter;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
|
|
@ -22,6 +23,8 @@ public class PlayerShops extends JavaPlugin {
|
|||
private Economy econ = null;
|
||||
@Getter
|
||||
private ShopHandler shopHandler;
|
||||
@Getter
|
||||
private DatabaseManager databaseManager;
|
||||
|
||||
private ShopListener shopListener;
|
||||
private PlayerListener playerListener;
|
||||
|
|
@ -36,7 +39,7 @@ public class PlayerShops extends JavaPlugin {
|
|||
}
|
||||
Bukkit.getLogger().info("Hooked into Vault economy provided by " + econ.getName());
|
||||
reloadConfigs();
|
||||
|
||||
databaseManager = new DatabaseManager(instance);
|
||||
shopHandler = new ShopHandler(instance);
|
||||
|
||||
registerListeners();
|
||||
|
|
@ -46,6 +49,8 @@ public class PlayerShops extends JavaPlugin {
|
|||
public void onDisable() {
|
||||
unRegisterListeners();
|
||||
Bukkit.getScheduler().cancelTasks(this);
|
||||
|
||||
databaseManager.unload();
|
||||
}
|
||||
|
||||
private boolean setupEconomy() {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import org.bukkit.scheduler.BukkitRunnable;
|
|||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
|
@ -52,6 +51,35 @@ public class DatabaseManager {
|
|||
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 {
|
||||
private Connection connection;
|
||||
private volatile boolean isActive;
|
||||
|
|
@ -140,10 +168,10 @@ public class DatabaseManager {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
runTask();
|
||||
runTaskQueue();
|
||||
}
|
||||
|
||||
public synchronized void runTask() {
|
||||
public synchronized void runTaskQueue() {
|
||||
if (databaseQueryQueue.isEmpty())
|
||||
return;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +0,0 @@
|
|||
package com.alttd.playershops.storage;
|
||||
|
||||
public class StorageManager {
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user