mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-21 20:56:45 +01:00
feature: connection pooling & SQL refactoring
This commit is contained in:
parent
16a4ee835c
commit
a5c1f1a74b
@ -89,6 +89,8 @@ tasks.named<ShadowJar>("shadowJar") {
|
||||
relocate("net.jcip", "com.plotsquared.core.annotations.jcip")
|
||||
relocate("edu.umd.cs.findbugs", "com.plotsquared.core.annotations.findbugs")
|
||||
relocate("com.intellectualsites.annotations", "com.plotsquared.core.annotations.informative")
|
||||
relocate("org.jdbi.v3", "com.plotsquared.core.jdbi")
|
||||
relocate("com.zaxxer.hikari", "com.plotsquared.core.hikari")
|
||||
|
||||
// Get rid of all the libs which are 100% unused.
|
||||
minimize()
|
||||
|
@ -82,6 +82,8 @@ import com.plotsquared.core.inject.annotations.DefaultGenerator;
|
||||
import com.plotsquared.core.inject.annotations.ImpromptuPipeline;
|
||||
import com.plotsquared.core.inject.annotations.WorldConfig;
|
||||
import com.plotsquared.core.inject.annotations.WorldFile;
|
||||
import com.plotsquared.core.inject.modules.DatabaseModule;
|
||||
import com.plotsquared.core.inject.modules.JdbiModule;
|
||||
import com.plotsquared.core.inject.modules.PlotSquaredModule;
|
||||
import com.plotsquared.core.listener.PlotListener;
|
||||
import com.plotsquared.core.listener.WESubscriber;
|
||||
@ -140,6 +142,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.incendo.serverlib.ServerLib;
|
||||
|
||||
import javax.xml.crypto.Data;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
@ -292,7 +295,9 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl
|
||||
new WorldManagerModule(),
|
||||
new PlotSquaredModule(),
|
||||
new BukkitModule(this),
|
||||
new BackupModule()
|
||||
new BackupModule(),
|
||||
new JdbiModule(),
|
||||
new DatabaseModule()
|
||||
);
|
||||
this.injector.injectMembers(this);
|
||||
|
||||
|
@ -37,6 +37,11 @@ dependencies {
|
||||
// Logging
|
||||
compileOnlyApi(libs.log4j)
|
||||
|
||||
// Database
|
||||
api(libs.hikaricp)
|
||||
api(libs.jdbiCore)
|
||||
api(libs.jdbiGuice)
|
||||
|
||||
// Other libraries
|
||||
api(libs.prtree)
|
||||
api(libs.aopalliance)
|
||||
|
@ -18,6 +18,7 @@
|
||||
*/
|
||||
package com.plotsquared.core;
|
||||
|
||||
import com.google.inject.Key;
|
||||
import com.plotsquared.core.configuration.ConfigurationSection;
|
||||
import com.plotsquared.core.configuration.ConfigurationUtil;
|
||||
import com.plotsquared.core.configuration.MemorySection;
|
||||
@ -31,14 +32,12 @@ import com.plotsquared.core.configuration.caption.load.DefaultCaptionProvider;
|
||||
import com.plotsquared.core.configuration.file.YamlConfiguration;
|
||||
import com.plotsquared.core.configuration.serialization.ConfigurationSerialization;
|
||||
import com.plotsquared.core.database.DBFunc;
|
||||
import com.plotsquared.core.database.Database;
|
||||
import com.plotsquared.core.database.MySQL;
|
||||
import com.plotsquared.core.database.SQLManager;
|
||||
import com.plotsquared.core.database.SQLite;
|
||||
import com.plotsquared.core.generator.GeneratorWrapper;
|
||||
import com.plotsquared.core.generator.HybridPlotWorld;
|
||||
import com.plotsquared.core.generator.HybridUtils;
|
||||
import com.plotsquared.core.generator.IndependentPlotGenerator;
|
||||
import com.plotsquared.core.inject.annotations.PlotDatabase;
|
||||
import com.plotsquared.core.inject.factory.HybridPlotWorldFactory;
|
||||
import com.plotsquared.core.listener.PlotListener;
|
||||
import com.plotsquared.core.location.Location;
|
||||
@ -75,7 +74,9 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
@ -1236,8 +1237,11 @@ public class PlotSquared {
|
||||
DBFunc.validatePlots(plots);
|
||||
|
||||
// Close the connection
|
||||
DBFunc.close();
|
||||
} catch (NullPointerException throwable) {
|
||||
final DataSource dataSource = platform().injector().getInstance(Key.get(DataSource.class, PlotDatabase.class));
|
||||
if (dataSource instanceof Closeable closeable) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (IOException | NullPointerException throwable) {
|
||||
LOGGER.error("Could not close database connection", throwable);
|
||||
throwable.printStackTrace();
|
||||
}
|
||||
@ -1289,26 +1293,7 @@ public class PlotSquared {
|
||||
if (DBFunc.dbManager != null) {
|
||||
DBFunc.dbManager.close();
|
||||
}
|
||||
Database database;
|
||||
if (Storage.MySQL.USE) {
|
||||
database = new MySQL(Storage.MySQL.HOST, Storage.MySQL.PORT, Storage.MySQL.DATABASE,
|
||||
Storage.MySQL.USER, Storage.MySQL.PASSWORD
|
||||
);
|
||||
} else if (Storage.SQLite.USE) {
|
||||
File file = FileUtils.getFile(platform.getDirectory(), Storage.SQLite.DB + ".db");
|
||||
database = new SQLite(file);
|
||||
} else {
|
||||
LOGGER.error("No storage type is set. Disabling PlotSquared");
|
||||
this.platform.shutdown(); //shutdown used instead of disable because no database is set
|
||||
return;
|
||||
}
|
||||
DBFunc.dbManager = new SQLManager(
|
||||
database,
|
||||
Storage.PREFIX,
|
||||
this.eventDispatcher,
|
||||
this.plotListener,
|
||||
this.worldConfiguration
|
||||
);
|
||||
DBFunc.dbManager = platform().injector().getInstance(SQLManager.class);
|
||||
this.plots_tmp = DBFunc.getPlots();
|
||||
if (getPlotAreaManager() instanceof SinglePlotAreaManager) {
|
||||
SinglePlotArea area = ((SinglePlotAreaManager) getPlotAreaManager()).getArea();
|
||||
@ -1322,13 +1307,13 @@ public class PlotSquared {
|
||||
}
|
||||
this.clustersTmp = DBFunc.getClusters();
|
||||
LOGGER.info("Connection to database established. Type: {}", Storage.MySQL.USE ? "MySQL" : "SQLite");
|
||||
} catch (ClassNotFoundException | SQLException e) {
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(
|
||||
"Failed to open database connection ({}). Disabling PlotSquared",
|
||||
Storage.MySQL.USE ? "MySQL" : "SQLite"
|
||||
);
|
||||
LOGGER.error("==== Here is an ugly stacktrace, if you are interested in those things ===");
|
||||
e.printStackTrace();
|
||||
LOGGER.error("", e);
|
||||
LOGGER.error("==== End of stacktrace ====");
|
||||
LOGGER.error(
|
||||
"Please go to the {} 'storage.yml' and configure the database correctly",
|
||||
|
@ -29,6 +29,7 @@ import java.sql.Statement;
|
||||
* @author -_Husky_-
|
||||
* @author tips48
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public abstract class Database {
|
||||
|
||||
public abstract Connection forceConnection() throws SQLException, ClassNotFoundException;
|
||||
|
@ -33,6 +33,7 @@ import java.sql.Statement;
|
||||
* @author -_Husky_-
|
||||
* @author tips48
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public class MySQL extends Database {
|
||||
|
||||
private final String user;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -33,6 +33,7 @@ import java.sql.Statement;
|
||||
/**
|
||||
* Connects to and uses a SQLite database.
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public class SQLite extends Database {
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + SQLite.class.getSimpleName());
|
||||
|
@ -0,0 +1,12 @@
|
||||
package com.plotsquared.core.inject.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface PlotDatabase {
|
||||
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package com.plotsquared.core.inject.modules;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.plotsquared.core.PlotSquared;
|
||||
import com.plotsquared.core.configuration.Storage;
|
||||
import com.plotsquared.core.inject.annotations.PlotDatabase;
|
||||
import com.plotsquared.core.util.FileUtils;
|
||||
import com.plotsquared.core.util.StringMan;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.File;
|
||||
|
||||
public class DatabaseModule extends AbstractModule {
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + PlotSquared.class.getSimpleName());
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
try {
|
||||
if (Storage.MySQL.USE) {
|
||||
this.configureMySQL();
|
||||
} else if (Storage.SQLite.USE) {
|
||||
this.configureSQLite();
|
||||
} else {
|
||||
LOGGER.error("No storage type is set. Disabling PlotSquared");
|
||||
PlotSquared.platform().shutdown();
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
LOGGER.error("Unable to initialize database", e);
|
||||
PlotSquared.platform().shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
private void configureSQLite() throws Exception {
|
||||
final File file = FileUtils.getFile(PlotSquared.platform().getDirectory(), Storage.SQLite.DB + ".db");
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
|
||||
final HikariConfig config = new HikariConfig();
|
||||
config.setJdbcUrl("jdbc:sqlite:" + file);
|
||||
config.setDriverClassName("org.sqlite.JDBC");
|
||||
final DataSource dataSource = new HikariDataSource();
|
||||
|
||||
binder().bind(DataSource.class).annotatedWith(PlotDatabase.class).toInstance(dataSource);
|
||||
}
|
||||
|
||||
private void configureMySQL() {
|
||||
final HikariConfig hikariConfig = new HikariConfig();
|
||||
hikariConfig.setJdbcUrl(
|
||||
String.format(
|
||||
"jdbc:mysql://%s:%s/%s?%s",
|
||||
Storage.MySQL.HOST,
|
||||
Storage.MySQL.PORT,
|
||||
Storage.MySQL.DATABASE,
|
||||
StringMan.join(Storage.MySQL.PROPERTIES, "&")
|
||||
));
|
||||
hikariConfig.setUsername(Storage.MySQL.USER);
|
||||
hikariConfig.setPassword(Storage.MySQL.PASSWORD);
|
||||
hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
|
||||
hikariConfig.addDataSourceProperty("prepStmtCacheSize", "512");
|
||||
hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||
final DataSource dataSource = new HikariDataSource(hikariConfig);
|
||||
|
||||
binder().bind(DataSource.class).annotatedWith(PlotDatabase.class).toInstance(dataSource);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.plotsquared.core.inject.modules;
|
||||
|
||||
import com.plotsquared.core.inject.annotations.PlotDatabase;
|
||||
import org.jdbi.v3.guice.AbstractJdbiDefinitionModule;
|
||||
|
||||
public class JdbiModule extends AbstractJdbiDefinitionModule {
|
||||
|
||||
public JdbiModule() {
|
||||
super(PlotDatabase.class);
|
||||
}
|
||||
@Override
|
||||
public void configureJdbi() {
|
||||
|
||||
}
|
||||
}
|
@ -19,6 +19,10 @@ luckperms = "5.4"
|
||||
essentialsx = "2.20.1"
|
||||
mvdwapi = "3.1.1"
|
||||
|
||||
# Datebase
|
||||
hikaricp = "5.0.1"
|
||||
jdbi = "3.41.3"
|
||||
|
||||
# Third party
|
||||
prtree = "2.0.1"
|
||||
aopalliance = "1.0"
|
||||
@ -63,6 +67,11 @@ essentialsx = { group = "net.essentialsx", name = "EssentialsX", version.ref = "
|
||||
faweCore = { group = "com.fastasyncworldedit", name = "FastAsyncWorldEdit-Core", version.ref = "fawe" }
|
||||
faweBukkit = { group = "com.fastasyncworldedit", name = "FastAsyncWorldEdit-Bukkit", version.ref = "fawe" }
|
||||
|
||||
# Database
|
||||
hikaricp = { group = "com.zaxxer", name = "HikariCP", version.ref = "hikaricp" }
|
||||
jdbiCore = { group = "org.jdbi", name = "jdbi3-core", version.ref = "jdbi" }
|
||||
jdbiGuice = { group = "org.jdbi", name = "jdbi3-guice", version.ref = "jdbi" }
|
||||
|
||||
# Third party
|
||||
prtree = { group = "com.intellectualsites.prtree", name = "PRTree", version.ref = "prtree" }
|
||||
aopalliance = { group = "aopalliance", name = "aopalliance", version.ref = "aopalliance" }
|
||||
|
Loading…
Reference in New Issue
Block a user