fix database connection url and closing. add ssl option
This commit is contained in:
parent
d7c0870546
commit
0272a9ef68
8 changed files with 54 additions and 4 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -207,5 +207,6 @@ public class NpcRegistryImpl implements NpcRegistry {
|
|||
|
||||
public void unload() {
|
||||
npcList.forEach(npcEntry -> npcEntry.getNpc().delete());
|
||||
storage.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,4 +8,7 @@ public interface NpcStorage {
|
|||
Collection<NpcEntryImpl> loadNpcs();
|
||||
void saveNpcs(Collection<NpcEntryImpl> npcs);
|
||||
void deleteNpc(NpcEntryImpl npc);
|
||||
default void close() {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,4 +13,6 @@ public abstract class Database {
|
|||
public abstract Connection getSQLConnection();
|
||||
|
||||
public abstract void load();
|
||||
|
||||
public abstract void close();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -312,4 +312,9 @@ public class SQLiteStorage implements NpcStorage {
|
|||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
database.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue