fix database connection url and closing. add ssl option

This commit is contained in:
D3v1s0m 2024-08-18 14:51:32 +05:30
parent d7c0870546
commit 0272a9ef68
No known key found for this signature in database
GPG key ID: FA1F770C7B1D40C1
8 changed files with 54 additions and 4 deletions

View file

@ -30,9 +30,14 @@ public interface DatabaseConfig {
@DefaultString("znpcsplus")
String databaseName();
@ConfKey("use-ssl")
@ConfComments("Should SSL be used when connecting to the database?")
@DefaultBoolean(false)
boolean useSSL();
default String createConnectionURL(String dbType) {
if (dbType.equalsIgnoreCase("mysql")) {
return "jdbc:mysql://" + host() + ":" + port() + "/" + databaseName() + "?useSSL=false&user=" + username() + "&password=" + password();
return "jdbc:mysql://" + host() + ":" + port() + "/" + databaseName() + "?useSSL=" + useSSL();
} else {
throw new IllegalArgumentException("Unsupported database type: " + dbType);
}

View file

@ -207,5 +207,6 @@ public class NpcRegistryImpl implements NpcRegistry {
public void unload() {
npcList.forEach(npcEntry -> npcEntry.getNpc().delete());
storage.close();
}
}

View file

@ -8,4 +8,7 @@ public interface NpcStorage {
Collection<NpcEntryImpl> loadNpcs();
void saveNpcs(Collection<NpcEntryImpl> npcs);
void deleteNpc(NpcEntryImpl npc);
default void close() {
}
}

View file

@ -13,4 +13,6 @@ public abstract class Database {
public abstract Connection getSQLConnection();
public abstract void load();
public abstract void close();
}

View file

@ -10,10 +10,14 @@ import java.util.logging.Logger;
public class MySQL extends Database {
private final String connectionURL;
private final String username;
private final String password;
public MySQL(String connectionURL, Logger logger) {
public MySQL(String connectionURL, String username, String password, Logger logger) {
super(logger);
this.connectionURL = connectionURL;
this.username = username;
this.password = password;
}
@Override
@ -25,7 +29,7 @@ public class MySQL extends Database {
return connection;
}
Class.forName("com.mysql.jdbc.Driver");
connection = java.sql.DriverManager.getConnection(connectionURL);
connection = java.sql.DriverManager.getConnection(connectionURL, username, password);
return connection;
} catch (ClassNotFoundException ex) {
logger.severe("MySQL JDBC library not found" + ex);
@ -56,6 +60,18 @@ public class MySQL extends Database {
connection = getSQLConnection();
}
@Override
public void close() {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
logger.severe("An error occurred while closing the connection");
e.printStackTrace();
}
}
public boolean tableExists(String tableName) {
try {
Statement s = connection.createStatement();

View file

@ -46,7 +46,8 @@ public class MySQLStorage implements NpcStorage {
this.typeRegistry = typeRegistry;
this.propertyRegistry = propertyRegistry;
this.textSerializer = textSerializer;
this.database = new MySQL(configManager.getConfig().databaseConfig().createConnectionURL("mysql"), logger);
this.database = new MySQL(configManager.getConfig().databaseConfig().createConnectionURL("mysql"),
configManager.getConfig().databaseConfig().username(), configManager.getConfig().databaseConfig().password(), logger);
database.load();
if (database.getSQLConnection() == null) {
throw new RuntimeException("Failed to initialize MySQL Storage");
@ -313,4 +314,9 @@ public class MySQLStorage implements NpcStorage {
exception.printStackTrace();
}
}
@Override
public void close() {
database.close();
}
}

View file

@ -41,6 +41,18 @@ public class SQLite extends Database{
connection = getSQLConnection();
}
@Override
public void close() {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
logger.severe("An error occurred while closing the connection");
e.printStackTrace();
}
}
public boolean tableExists(String tableName) {
try {
Statement s = connection.createStatement();

View file

@ -312,4 +312,9 @@ public class SQLiteStorage implements NpcStorage {
exception.printStackTrace();
}
}
@Override
public void close() {
database.close();
}
}