Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: connection pooling & SQL refactoring #4214

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
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;
Expand Down Expand Up @@ -140,6 +142,7 @@
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;
Expand Down Expand Up @@ -292,7 +295,9 @@ public void onEnable() {
new WorldManagerModule(),
new PlotSquaredModule(),
new BukkitModule(this),
new BackupModule()
new BackupModule(),
new JdbiModule(),
new DatabaseModule()
);
this.injector.injectMembers(this);

Expand Down
5 changes: 5 additions & 0 deletions Core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 12 additions & 27 deletions Core/src/main/java/com/plotsquared/core/PlotSquared.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,14 +32,12 @@
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;
Expand Down Expand Up @@ -75,7 +74,9 @@
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;
Expand Down Expand Up @@ -1236,8 +1237,11 @@ public void disable() {
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();
}
Expand Down Expand Up @@ -1289,26 +1293,7 @@ public void setupDatabase() {
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();
Expand All @@ -1322,13 +1307,13 @@ public void setupDatabase() {
}
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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @author -_Husky_-
* @author tips48
*/
@Deprecated(forRemoval = true)
public abstract class Database {

public abstract Connection forceConnection() throws SQLException, ClassNotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @author -_Husky_-
* @author tips48
*/
@Deprecated(forRemoval = true)
public class MySQL extends Database {

private final String user;
Expand Down
Loading