Database restructure
Kiafumi now uses a connection pool, and doesn't drop the connection.
This commit is contained in:
parent
ad09706b77
commit
5c7f2991ec
22
pom.xml
22
pom.xml
|
|
@ -12,12 +12,12 @@
|
||||||
<url>https://oko.moe/kiafumi.htm</url>
|
<url>https://oko.moe/kiafumi.htm</url>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.jdk.version>16</project.jdk.version>
|
<project.jdk.version>17</project.jdk.version>
|
||||||
<mainclass>moe.oko.Kiafumi.Kiafumi</mainclass>
|
<mainclass>moe.oko.Kiafumi.Kiafumi</mainclass>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<!-- JDA (https://mvnrepository.com/artifact/net.dv8tion/JDA) -->
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- JDA (https://mvnrepository.com/artifact/net.dv8tion/JDA) -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.dv8tion</groupId>
|
<groupId>net.dv8tion</groupId>
|
||||||
<artifactId>JDA</artifactId>
|
<artifactId>JDA</artifactId>
|
||||||
|
|
@ -30,25 +30,20 @@
|
||||||
<artifactId>Simple-Yaml</artifactId>
|
<artifactId>Simple-Yaml</artifactId>
|
||||||
<version>1.7.2</version>
|
<version>1.7.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.httpcomponents</groupId>
|
|
||||||
<artifactId>httpclient</artifactId>
|
|
||||||
<version>4.5.13</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
<artifactId>log4j-api</artifactId>
|
<artifactId>log4j-api</artifactId>
|
||||||
<version>2.17.2</version>
|
<version>2.19.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
<artifactId>log4j-core</artifactId>
|
<artifactId>log4j-core</artifactId>
|
||||||
<version>2.17.2</version>
|
<version>2.19.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.logging.log4j</groupId>
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
<artifactId>log4j-slf4j-impl</artifactId>
|
<artifactId>log4j-slf4j-impl</artifactId>
|
||||||
<version>2.17.2</version>
|
<version>2.19.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.sedmelluq</groupId>
|
<groupId>com.sedmelluq</groupId>
|
||||||
|
|
@ -58,7 +53,12 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>mysql</groupId>
|
<groupId>mysql</groupId>
|
||||||
<artifactId>mysql-connector-java</artifactId>
|
<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>
|
</dependency>
|
||||||
<!-- DDG api (https://github.com/nstrydom2/duckduckgo-api) -->
|
<!-- DDG api (https://github.com/nstrydom2/duckduckgo-api) -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import moe.oko.Kiafumi.command.CommandClass;
|
||||||
import moe.oko.Kiafumi.command.CommandRegistrar;
|
import moe.oko.Kiafumi.command.CommandRegistrar;
|
||||||
import moe.oko.Kiafumi.listener.MainListener;
|
import moe.oko.Kiafumi.listener.MainListener;
|
||||||
import moe.oko.Kiafumi.listener.SkynetListener;
|
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.model.ServerManager;
|
||||||
import moe.oko.Kiafumi.util.CommandInfo;
|
import moe.oko.Kiafumi.util.CommandInfo;
|
||||||
import moe.oko.Kiafumi.util.EmbedUI;
|
import moe.oko.Kiafumi.util.EmbedUI;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package moe.oko.Kiafumi;
|
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 org.simpleyaml.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -30,12 +31,10 @@ public class KiafumiConfig {
|
||||||
/*
|
/*
|
||||||
Kia Variable Section
|
Kia Variable Section
|
||||||
*/
|
*/
|
||||||
private boolean sharded;
|
|
||||||
private String activityType;
|
private String activityType;
|
||||||
private String activityMsg;
|
private String activityMsg;
|
||||||
private String statusType;
|
private String statusType;
|
||||||
private List<String> pingResponses;
|
private List<String> pingResponses;
|
||||||
private int gameCheckTime;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
SQL Variable Section
|
SQL Variable Section
|
||||||
|
|
@ -45,6 +44,10 @@ public class KiafumiConfig {
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private String database;
|
private String database;
|
||||||
|
private int poolSize;
|
||||||
|
private long connTimeout;
|
||||||
|
private long idleTimeout;
|
||||||
|
private long maxLifetime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for the config.
|
* Constructor for the config.
|
||||||
|
|
@ -84,12 +87,10 @@ public class KiafumiConfig {
|
||||||
|
|
||||||
// Kiafumi loaders
|
// Kiafumi loaders
|
||||||
var main = configuration.getConfigurationSection("main");
|
var main = configuration.getConfigurationSection("main");
|
||||||
sharded = main.getBoolean("sharded");
|
|
||||||
activityType = main.getString("activityType");
|
activityType = main.getString("activityType");
|
||||||
activityMsg = main.getString("activityMsg");
|
activityMsg = main.getString("activityMsg");
|
||||||
statusType = main.getString("statusType");
|
statusType = main.getString("statusType");
|
||||||
pingResponses = main.getStringList("pingResponses");
|
pingResponses = main.getStringList("pingResponses");
|
||||||
gameCheckTime = main.getInt("gameCheckTime");
|
|
||||||
|
|
||||||
// SQL loaders
|
// SQL loaders
|
||||||
var sql = configuration.getConfigurationSection("sql");
|
var sql = configuration.getConfigurationSection("sql");
|
||||||
|
|
@ -98,6 +99,10 @@ public class KiafumiConfig {
|
||||||
username = sql.getString("username");
|
username = sql.getString("username");
|
||||||
password = sql.getString("password");
|
password = sql.getString("password");
|
||||||
database = sql.getString("database");
|
database = sql.getString("database");
|
||||||
|
poolSize = sql.getInt("poolSize");
|
||||||
|
connTimeout = sql.getLong("connectionTimeout");
|
||||||
|
idleTimeout = sql.getLong("idleTimeout");
|
||||||
|
maxLifetime = sql.getLong("maxLifetime");
|
||||||
|
|
||||||
} catch(Exception ex) {
|
} catch(Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
|
|
@ -125,9 +130,11 @@ public class KiafumiConfig {
|
||||||
|
|
||||||
public String getLogChannel() { return logChannel; }
|
public String getLogChannel() { return logChannel; }
|
||||||
|
|
||||||
public int getGameCheckTime() { return gameCheckTime; }
|
|
||||||
|
|
||||||
public List<String> getPingResponses() { return pingResponses; }
|
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()); }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ public class InviteCommand extends CommandClass {
|
||||||
.setAuthor("Kiafumi", "https://oko.moe/kiafumi.htm", Kiafumi.JDA.getSelfUser().getAvatarUrl())
|
.setAuthor("Kiafumi", "https://oko.moe/kiafumi.htm", Kiafumi.JDA.getSelfUser().getAvatarUrl())
|
||||||
.setTitle("Invite me to your server!", Kiafumi.instance.config.assembleDefaultInvite())
|
.setTitle("Invite me to your server!", Kiafumi.instance.config.assembleDefaultInvite())
|
||||||
.setDescription("Alternatively, you can host me yourself!")
|
.setDescription("Alternatively, you can host me yourself!")
|
||||||
.appendDescription("https://fem.mint.lgbt/oko/kiafumi")
|
.appendDescription(" https://fem.mint.lgbt/oko/kiafumi")
|
||||||
.setFooter(EmbedUI.BRAND)
|
.setFooter(EmbedUI.BRAND)
|
||||||
.setTimestamp(ZonedDateTime.now());
|
.setTimestamp(ZonedDateTime.now());
|
||||||
e.getHook().sendMessageEmbeds(eb.build()).queue();
|
e.getHook().sendMessageEmbeds(eb.build()).queue();
|
||||||
|
|
|
||||||
13
src/main/java/moe/oko/Kiafumi/model/db/DBCredentials.java
Normal file
13
src/main/java/moe/oko/Kiafumi/model/db/DBCredentials.java
Normal 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
|
||||||
|
) {}
|
||||||
|
|
@ -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.Kiafumi;
|
||||||
|
import moe.oko.Kiafumi.model.Server;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
|
|
@ -14,10 +18,10 @@ import static moe.oko.Kiafumi.util.LoggingManager.*;
|
||||||
* Kiafumi DB Class
|
* Kiafumi DB Class
|
||||||
* Basically our helpful MySQL functions that pertain to data persistence.
|
* Basically our helpful MySQL functions that pertain to data persistence.
|
||||||
* @author Kay, with moral support from Yodabird
|
* @author Kay, with moral support from Yodabird
|
||||||
* @apiNote I HATE SQL I HATE SQL AAAAAAAAAAAAAA
|
|
||||||
*/
|
*/
|
||||||
public class KiafumiDB {
|
public class KiafumiDB {
|
||||||
// Our actual MySQL Connection, this is created on class construction.
|
|
||||||
|
private HikariDataSource dataSource;
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
|
|
||||||
// The prepared statement strings
|
// 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 = ?";
|
private final String SERVER_INFO_MODIFY = "UPDATE `serverInfo` SET welcomeChannel=?, modChannel=?, joinRole=? WHERE id = ?";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* KiafumiDB Config Constructor
|
* KiafumiDB Connection 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
|
|
||||||
*/
|
*/
|
||||||
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 {
|
try {
|
||||||
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db, username, password);
|
connection = dataSource.getConnection();
|
||||||
} catch (Exception ex) {
|
info("Connected to database.");
|
||||||
error("Failed to initialize the MySQL instance.");
|
} catch (SQLException e) {
|
||||||
ex.printStackTrace();
|
error("Unable to connect to database.");
|
||||||
return;
|
e.printStackTrace();
|
||||||
|
this.dataSource = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
initializeTables();
|
initializeTables();
|
||||||
|
|
@ -24,8 +24,6 @@ discord:
|
||||||
invitePermissionLevel: 139855252544
|
invitePermissionLevel: 139855252544
|
||||||
# MAIN CONFIG STACK
|
# MAIN CONFIG STACK
|
||||||
main:
|
main:
|
||||||
# Currently, unimplemented, maybe in the future if performance becomes and issue.
|
|
||||||
sharded: false
|
|
||||||
# Activity Type (STREAMING|PLAYING|WATCHING|COMPETING|DEFAULT)
|
# Activity Type (STREAMING|PLAYING|WATCHING|COMPETING|DEFAULT)
|
||||||
activityType: "PLAYING"
|
activityType: "PLAYING"
|
||||||
# Activity Message
|
# Activity Message
|
||||||
|
|
@ -49,4 +47,8 @@ sql:
|
||||||
port: 3306
|
port: 3306
|
||||||
database: "kiafumi"
|
database: "kiafumi"
|
||||||
username: "alpine"
|
username: "alpine"
|
||||||
password: "squidlover42069"
|
password: "squidlover42069"
|
||||||
|
poolSize: 2
|
||||||
|
connectionTimeout: 10000
|
||||||
|
idleTimeout: 600000
|
||||||
|
maxLifetime: 7200000
|
||||||
Loading…
Reference in a new issue