Database restructure

Kiafumi now uses a connection pool, and doesn't drop the connection.
This commit is contained in:
Anya 2022-11-13 04:14:07 -08:00
parent ad09706b77
commit 5c7f2991ec
7 changed files with 74 additions and 39 deletions

22
pom.xml
View file

@ -12,12 +12,12 @@
<url>https://oko.moe/kiafumi.htm</url>
<properties>
<project.jdk.version>16</project.jdk.version>
<project.jdk.version>17</project.jdk.version>
<mainclass>moe.oko.Kiafumi.Kiafumi</mainclass>
</properties>
<!-- JDA (https://mvnrepository.com/artifact/net.dv8tion/JDA) -->
<dependencies>
<!-- JDA (https://mvnrepository.com/artifact/net.dv8tion/JDA) -->
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
@ -30,25 +30,20 @@
<artifactId>Simple-Yaml</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.2</version>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>com.sedmelluq</groupId>
@ -58,7 +53,12 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
</dependency>
<!-- DDG api (https://github.com/nstrydom2/duckduckgo-api) -->
<dependency>

View file

@ -4,7 +4,7 @@ import moe.oko.Kiafumi.command.CommandClass;
import moe.oko.Kiafumi.command.CommandRegistrar;
import moe.oko.Kiafumi.listener.MainListener;
import moe.oko.Kiafumi.listener.SkynetListener;
import moe.oko.Kiafumi.model.KiafumiDB;
import moe.oko.Kiafumi.model.db.KiafumiDB;
import moe.oko.Kiafumi.model.ServerManager;
import moe.oko.Kiafumi.util.CommandInfo;
import moe.oko.Kiafumi.util.EmbedUI;

View file

@ -1,6 +1,7 @@
package moe.oko.Kiafumi;
import moe.oko.Kiafumi.model.KiafumiDB;
import moe.oko.Kiafumi.model.db.DBCredentials;
import moe.oko.Kiafumi.model.db.KiafumiDB;
import org.simpleyaml.configuration.file.YamlConfiguration;
import java.util.List;
@ -30,12 +31,10 @@ public class KiafumiConfig {
/*
Kia Variable Section
*/
private boolean sharded;
private String activityType;
private String activityMsg;
private String statusType;
private List<String> pingResponses;
private int gameCheckTime;
/*
SQL Variable Section
@ -45,6 +44,10 @@ public class KiafumiConfig {
private String username;
private String password;
private String database;
private int poolSize;
private long connTimeout;
private long idleTimeout;
private long maxLifetime;
/**
* Constructor for the config.
@ -84,12 +87,10 @@ public class KiafumiConfig {
// Kiafumi loaders
var main = configuration.getConfigurationSection("main");
sharded = main.getBoolean("sharded");
activityType = main.getString("activityType");
activityMsg = main.getString("activityMsg");
statusType = main.getString("statusType");
pingResponses = main.getStringList("pingResponses");
gameCheckTime = main.getInt("gameCheckTime");
// SQL loaders
var sql = configuration.getConfigurationSection("sql");
@ -98,6 +99,10 @@ public class KiafumiConfig {
username = sql.getString("username");
password = sql.getString("password");
database = sql.getString("database");
poolSize = sql.getInt("poolSize");
connTimeout = sql.getLong("connectionTimeout");
idleTimeout = sql.getLong("idleTimeout");
maxLifetime = sql.getLong("maxLifetime");
} catch(Exception ex) {
ex.printStackTrace();
@ -125,9 +130,11 @@ public class KiafumiConfig {
public String getLogChannel() { return logChannel; }
public int getGameCheckTime() { return gameCheckTime; }
public List<String> getPingResponses() { return pingResponses; }
public KiafumiDB createDb() { return new KiafumiDB(username, password, host, port, database); }
public DBCredentials getCredentials () { return new DBCredentials(
username, password, host, port, database, poolSize, connTimeout, idleTimeout, maxLifetime);
}
public KiafumiDB createDb() { return new KiafumiDB(getCredentials()); }
}

View file

@ -40,7 +40,7 @@ public class InviteCommand extends CommandClass {
.setAuthor("Kiafumi", "https://oko.moe/kiafumi.htm", Kiafumi.JDA.getSelfUser().getAvatarUrl())
.setTitle("Invite me to your server!", Kiafumi.instance.config.assembleDefaultInvite())
.setDescription("Alternatively, you can host me yourself!")
.appendDescription("https://fem.mint.lgbt/oko/kiafumi")
.appendDescription(" https://fem.mint.lgbt/oko/kiafumi")
.setFooter(EmbedUI.BRAND)
.setTimestamp(ZonedDateTime.now());
e.getHook().sendMessageEmbeds(eb.build()).queue();

View file

@ -0,0 +1,13 @@
package moe.oko.Kiafumi.model.db;
public record DBCredentials(
String username,
String password,
String host,
int port,
String db,
int poolSize,
long connTimeout,
long idleTimeout,
long maxLifetime
) {}

View file

@ -1,6 +1,10 @@
package moe.oko.Kiafumi.model;
package moe.oko.Kiafumi.model.db;
import com.google.common.base.Strings;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import moe.oko.Kiafumi.Kiafumi;
import moe.oko.Kiafumi.model.Server;
import net.dv8tion.jda.api.entities.Guild;
import java.sql.*;
@ -14,10 +18,10 @@ import static moe.oko.Kiafumi.util.LoggingManager.*;
* Kiafumi DB Class
* Basically our helpful MySQL functions that pertain to data persistence.
* @author Kay, with moral support from Yodabird
* @apiNote I HATE SQL I HATE SQL AAAAAAAAAAAAAA
*/
public class KiafumiDB {
// Our actual MySQL Connection, this is created on class construction.
private HikariDataSource dataSource;
private Connection connection;
// The prepared statement strings
@ -30,20 +34,29 @@ public class KiafumiDB {
private final String SERVER_INFO_MODIFY = "UPDATE `serverInfo` SET welcomeChannel=?, modChannel=?, joinRole=? WHERE id = ?";
/**
* KiafumiDB Config Constructor
* @param username - username to access database
* @param password - password to access database
* @param host - host for server
* @param port - port for server (3306 is default)
* @param db - database name
* KiafumiDB Connection Constructor
*/
public KiafumiDB(String username, String password, String host, int port, String db) {
public KiafumiDB(DBCredentials credentials) {
var config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://%s:%s/%s".formatted(credentials.host(), credentials.port(), credentials.db()));
config.setConnectionTimeout(credentials.connTimeout());
config.setIdleTimeout(credentials.idleTimeout());
config.setMaxLifetime(credentials.maxLifetime());
config.setMaximumPoolSize(credentials.poolSize());
config.setUsername(credentials.username());
if (!Strings.isNullOrEmpty(credentials.password()))
config.setPassword(credentials.password());
this.dataSource = new HikariDataSource(config);
try {
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db, username, password);
} catch (Exception ex) {
error("Failed to initialize the MySQL instance.");
ex.printStackTrace();
return;
connection = dataSource.getConnection();
info("Connected to database.");
} catch (SQLException e) {
error("Unable to connect to database.");
e.printStackTrace();
this.dataSource = null;
}
initializeTables();

View file

@ -24,8 +24,6 @@ discord:
invitePermissionLevel: 139855252544
# MAIN CONFIG STACK
main:
# Currently, unimplemented, maybe in the future if performance becomes and issue.
sharded: false
# Activity Type (STREAMING|PLAYING|WATCHING|COMPETING|DEFAULT)
activityType: "PLAYING"
# Activity Message
@ -49,4 +47,8 @@ sql:
port: 3306
database: "kiafumi"
username: "alpine"
password: "squidlover42069"
password: "squidlover42069"
poolSize: 2
connectionTimeout: 10000
idleTimeout: 600000
maxLifetime: 7200000