1
0
mirror of https://github.com/Zrips/Jobs.git synced 2025-01-04 23:37:49 +01:00

Do connecting to database thread safe

This commit is contained in:
montlikadani 2020-09-12 20:46:05 +02:00
parent 78c6368c09
commit 274bc6e561
7 changed files with 58 additions and 92 deletions

View File

@ -1217,7 +1217,7 @@ public enum CMIMaterial {
public void updateMaterial() {
if (mat == null) {
for (Material one : Material.class.getEnumConstants()) {
if (!one.name().replace("LEGACY_", "").replace("_", "").equalsIgnoreCase(this.name().replace("_", "")))
if (!one.name().replaceAll("LEGACY_|_", "").equalsIgnoreCase(this.name().replace("_", "")))
continue;
mat = one;
break;
@ -1225,7 +1225,7 @@ public enum CMIMaterial {
}
if (mat == null) {
for (Material one : Material.class.getEnumConstants()) {
if (!one.name().replace("LEGACY_", "").replace("_", "").equalsIgnoreCase(this.getName().replace(" ", "")))
if (!one.name().replaceAll("LEGACY_|_", "").equalsIgnoreCase(this.getName().replace(" ", "")))
continue;
mat = one;
break;
@ -1234,7 +1234,7 @@ public enum CMIMaterial {
if (mat == null && !this.getLegacyNames().isEmpty()) {
main: for (Material one : Material.class.getEnumConstants()) {
for (String oneL : this.getLegacyNames()) {
if (!one.name().replace("LEGACY_", "").replace("_", "").equalsIgnoreCase(oneL.replace(" ", "").replace("_", "")))
if (!one.name().replaceAll("LEGACY_|_", "").equalsIgnoreCase(oneL.replace(" ", "").replace("_", "")))
continue main;
}
mat = one;

View File

@ -21,6 +21,7 @@ public enum Version {
v1_15_R1,
v1_16_R1,
v1_16_R2,
v1_16_R3,
v1_17_R1,
v1_17_R2,
v1_18_R1,

View File

@ -428,10 +428,9 @@ public class Jobs extends JavaPlugin {
public void startup() {
reload(true);
loadAllPlayersData();
// add all online players
for (Player online : Bukkit.getServer().getOnlinePlayers()) {
getPlayerManager().playerJoin(online);
}
Bukkit.getServer().getOnlinePlayers().forEach(getPlayerManager()::playerJoin);
}
public static void loadAllPlayersData() {

View File

@ -2,6 +2,7 @@ package com.gamingmesh.jobs.dao;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.concurrent.CompletableFuture;
public class JobsConnectionPool {
@ -24,20 +25,31 @@ public class JobsConnectionPool {
connection = null;
}
if (connection == null) {
connection = new JobsConnection(DriverManager.getConnection(url, username, password));
}
CompletableFuture.supplyAsync(() -> {
if (connection == null) {
try {
connection = new JobsConnection(DriverManager.getConnection(url, username, password));
} catch (SQLException e) {
}
}
return true;
});
return connection;
}
public synchronized void closeConnection() {
if (connection != null) {
try {
connection.closeConnection();
} catch (SQLException e) {
e.printStackTrace();
public void closeConnection() {
CompletableFuture.supplyAsync(() -> {
if (connection != null) {
try {
connection.closeConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return true;
});
}
}

View File

@ -12,6 +12,7 @@ import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
@ -472,24 +473,25 @@ public abstract class JobsDAO {
pool = new JobsConnectionPool(url, username, password);
}
public final synchronized void setUp() throws SQLException {
if (getConnection() == null)
return;
public final synchronized void setUp() {
CompletableFuture.supplyAsync(() -> {
if (getConnection() == null)
return null;
vacuum();
setupConfig();
vacuum();
try {
for (DBTables one : DBTables.values()) {
createDefaultTable(one);
try {
for (DBTables one : DBTables.values()) {
createDefaultTable(one);
}
checkDefaultCollumns();
} finally {
}
checkDefaultCollumns();
} finally {
}
}
@Deprecated
protected abstract void setupConfig() throws SQLException;
return null;
});
}
protected abstract void checkUpdate() throws SQLException;
@ -886,9 +888,6 @@ public abstract class JobsDAO {
}
}
int convertSchedId = -1;
boolean converted = true;
public void recordNewWorld(String worldName) {
JobsConnection conn = getConnection();
if (conn == null)
@ -944,13 +943,12 @@ public abstract class JobsDAO {
}
}
private boolean converted = true;
public void triggerTableIdUpdate() {
// Lets convert old fields
if (convertSchedId > 0)
Bukkit.getServer().getScheduler().cancelTask(convertSchedId);
if (!converted) {
convertSchedId = Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
Bukkit.getServer().getScheduler().runTaskLater(plugin, () -> {
Jobs.consoleMsg("&6[Jobs] Converting to new database format");
convertID();
Jobs.consoleMsg("&6[Jobs] Converted to new database format");
@ -2556,6 +2554,10 @@ public abstract class JobsDAO {
*/
public void executeSQL(String sql) throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
return;
}
Statement stmt = null;
try {
stmt = conn.createStatement();
@ -2565,11 +2567,6 @@ public abstract class JobsDAO {
}
}
/**
* Get a database connection
* @return DBConnection object
* @throws SQLException
*/
protected JobsConnection getConnection() {
try {
return isConnected() ? pool.getConnection() : null;

View File

@ -18,37 +18,15 @@ public class JobsMySQL extends JobsDAO {
}
public void initialize() {
try {
setUp();
} catch (SQLException e) {
e.printStackTrace();
}
setUp();
}
public JobsMySQL initialize(Jobs plugin, String hostname, String database, String username, String password, String prefix, boolean certificate, boolean ssl, boolean autoReconnect) {
JobsMySQL dao = new JobsMySQL(plugin, hostname, database, username, password, prefix, certificate, ssl, autoReconnect);
try {
dao.setUp();
} catch (SQLException e) {
e.printStackTrace();
}
dao.setUp();
return dao;
}
@Override
protected synchronized void setupConfig() throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
Jobs.consoleMsg("&cCould not run database updates! Could not connect to MySQL!");
return;
}
String name = getPrefix() + "config";
if (isTable(name)) {
drop(name);
}
}
@Override
protected synchronized void checkUpdate() throws SQLException {
JobsConnection conn = getConnection();
@ -162,7 +140,7 @@ public class JobsMySQL extends JobsDAO {
statement.executeQuery("SELECT `" + collumn + "` FROM `" + table + "`;");
return true;
} catch (SQLException e) {
Jobs.consoleMsg("Not a culumn |" + "SELECT " + collumn + " FROM " + table + "|");
Jobs.consoleMsg("Not a collumn |" + "SELECT " + collumn + " FROM " + table + "|");
return false;
} finally {
close(statement);
@ -179,7 +157,7 @@ public class JobsMySQL extends JobsDAO {
return false;
}
try {
Jobs.consoleMsg("Creating culumn |" + "ALTER TABLE `" + table + "` ADD COLUMN `" + collumn + "` " + type + ";" + "|");
Jobs.consoleMsg("Creating collumn |" + "ALTER TABLE `" + table + "` ADD COLUMN `" + collumn + "` " + type + ";" + "|");
statement.executeUpdate("ALTER TABLE `" + table + "` ADD COLUMN `" + collumn + "` " + type + ";");
return true;
} catch (SQLException e) {

View File

@ -13,21 +13,14 @@ import com.gamingmesh.jobs.dao.JobsManager.DataBaseType;
public class JobsSQLite extends JobsDAO {
public void initialize() {
try {
this.setUp();
} catch (SQLException e) {
e.printStackTrace();
}
setUp();
}
public JobsSQLite initialize(File dir) {
if (!dir.exists())
dir.mkdirs();
try {
this.setUp();
} catch (SQLException e) {
e.printStackTrace();
}
setUp();
return this;
}
@ -38,20 +31,6 @@ public class JobsSQLite extends JobsDAO {
this.setDbType(DataBaseType.SqLite);
}
@Override
protected synchronized void setupConfig() throws SQLException {
JobsConnection conn = getConnection();
if (conn == null) {
Jobs.consoleMsg("&cCould not run database updates! Could not connect to MySQL!");
return;
}
String name = getPrefix() + "config";
if (isTable(name)) {
drop(name);
}
}
@Override
protected synchronized void checkUpdate() throws SQLException {
JobsConnection conn = getConnection();