Skip to content

Commit

Permalink
Fixed connection leak issue when getting exact data
Browse files Browse the repository at this point in the history
  • Loading branch information
DeathGOD7 committed Dec 25, 2024
1 parent 320e393 commit 1e5e3a7
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 118 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
id("java")
id("maven-publish")
id("signing")
id("com.github.johnrengelman.shadow") version "8.1.1"
id("com.gradleup.shadow") version "9.0.0-beta4"
id("com.vanniktech.maven.publish") version "0.28.0"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ public PoolSettings getPoolSettings() {
* Database Info constructor for SQLite database
* @param dbname name of the database file
* @param dbdir path where the database file is located
* @param poolSettings {@link PoolSettings} the pool settings
* @since 1.0
*/
public DatabaseInfo(String dbname, String dbdir) {
public DatabaseInfo(String dbname, String dbdir, PoolSettings poolSettings) {
_dbType = DatabaseManager.DatabaseType.SQLite;
_dbName = dbname;
_dirDB = dbdir;
_poolSettings = poolSettings;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ private boolean connectMySQL() {
hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
hikariConfig.setLeakDetectionThreshold(2000);

PoolSettings poolSettings = this.dbInfo.getPoolSettings();
if (poolSettings != null) {
Expand All @@ -191,18 +192,16 @@ public void loadMysqlTables() {
String query = "SELECT table_name FROM information_schema.tables " +
"WHERE table_schema = '"+ this.getDBName() +"' AND table_type = 'base table' " +
"ORDER BY table_name";
try {
Connection con = this.getConnection();
try (Connection con = this.getConnection()) {
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String tablename = rs.getString(1);
tables.put(tablename, this.loadTable(tablename));
}

// close the fricking connection
DatabaseManager.getInstance().closeConnection(ps,rs);
DatabaseManager.getInstance().closeConnection(con);
// DatabaseManager.getInstance().closeConnection(ps,rs);
// DatabaseManager.getInstance().closeConnection(con);
} catch (SQLException ex) {
Logger.log("[ERROR] " + ex.getMessage());
}
Expand All @@ -219,9 +218,7 @@ public Table loadTable(String tablename) {
LinkedHashMap<String, Column> columns = new LinkedHashMap<>();
String primarykey = "";

try {
Connection con = this.getConnection();

try (Connection con = this.getConnection()) {
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();

Expand All @@ -248,10 +245,9 @@ public Table loadTable(String tablename) {

columns.put(name, column);
}

// close the fricking connection here too you dumb human
DatabaseManager.getInstance().closeConnection(ps,rs);
DatabaseManager.getInstance().closeConnection(con);
// DatabaseManager.getInstance().closeConnection(ps,rs);
// DatabaseManager.getInstance().closeConnection(con);
}
catch (SQLException ex) {
Logger.log("[ERROR] " + ex.getMessage());
Expand All @@ -264,6 +260,7 @@ public Table loadTable(String tablename) {

return new Table(tablename, primaryKey, columns.values());
}

private int getLimitFromText(String input) {
// Define a regular expression pattern to match the integer within parentheses
Pattern pattern = Pattern.compile("\\((\\d+)\\)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ public void removeTable(String tablename) {
* Create a new SQLite object
* @param dbName {@link String} The name of the database file
* @param directory {@link String} The directory of the database file
* @param poolSettings {@link PoolSettings} The pool settings
*/
public SQLite(String dbName, String directory){
public SQLite(String dbName, String directory, PoolSettings poolSettings){
this.dbName = dbName;
this.dirDB = directory;
this.dbInfo = new DatabaseInfo(dbName, directory);
this.dbInfo = new DatabaseInfo(dbName, directory, poolSettings);
if (connectSQLite()) {
Logger.log("[OBJECT CREATION INFO] MySQL connected successfully");
}else {
Expand Down Expand Up @@ -176,6 +177,7 @@ private boolean connectSQLite() {
hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
hikariConfig.setLeakDetectionThreshold(2000);

PoolSettings poolSettings = this.dbInfo.getPoolSettings();
if (poolSettings != null) {
Expand All @@ -202,20 +204,19 @@ public void loadSqliteTables() {
String sqlitever;
int[] sqliteverformatted = new int[3];

try {
try (Connection con = this.getConnection()) {
// Crrate a query
String query = "SELECT sqlite_version()";

// Create a statement
PreparedStatement ps = this.getConnection().prepareStatement(query);
PreparedStatement ps = con.prepareStatement(query);

// Execute the query to retrieve the SQLite version
ResultSet resultSet = ps.executeQuery();

// Retrieve the result
if (resultSet.next()) {
sqlitever = resultSet.getString(1);
ps.close();
}
} catch (SQLException ex) {
Logger.log("[ERROR] " + ex.getMessage());
Expand All @@ -228,8 +229,7 @@ public void loadSqliteTables() {
String query = "SELECT tbl_name FROM " + schematable +
" WHERE type = 'table' AND name NOT LIKE 'sqlite_%' " +
"ORDER BY tbl_name";
try {
Connection con = this.getConnection();
try (Connection con = this.getConnection()) {
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Expand All @@ -238,8 +238,8 @@ public void loadSqliteTables() {
}

// close the fricking connection .... even for sqlite
DatabaseManager.getInstance().closeConnection(ps,rs);
DatabaseManager.getInstance().closeConnection(con);
// DatabaseManager.getInstance().closeConnection(ps,rs);
// DatabaseManager.getInstance().closeConnection(con);
} catch (SQLException ex) {
Logger.log("[ERROR] " + ex.getMessage());
}
Expand Down Expand Up @@ -282,8 +282,8 @@ public Table loadTable(String tablename) {
}

// close the fricking connection again .... even for sqlite
DatabaseManager.getInstance().closeConnection(ps,rs);
DatabaseManager.getInstance().closeConnection(con);
// DatabaseManager.getInstance().closeConnection(ps,rs);
// DatabaseManager.getInstance().closeConnection(con);
}
catch (SQLException ex) {
Logger.log("[ERROR] " + ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public boolean createTable(Table table, DatabaseManager.DatabaseType dbtype) {
return false;

// Define schema rules for the collection
List<Column> allCols = table.getColumns();
List<Column> allCols = new ArrayList<>(table.getColumns());

if (!table.getPrimaryKey().getName().equals("_id")) {
return false;
Expand Down Expand Up @@ -400,8 +400,6 @@ public boolean insertData(String tablename, List<Column> columns) {
return false;
}



MongoCollection<Document> collection = db.getCollection(tablename);

Document doc = getColsAsDocument(columns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,13 @@ else if (dbtype == DatabaseType.MySQL && table.getPrimaryKey().isAutoIncrement()

Logger.log("[CREATE TABLE QUERY] " + query);

try {
Connection con = (Connection) DatabaseManager.getInstance().getConnection();
try (Connection con = (Connection) DatabaseManager.getInstance().getConnection()) {
PreparedStatement ps = con.prepareStatement(query.toString());
ps.executeUpdate();

// take his wife to gulag
DatabaseManager.getInstance().closeConnection(ps, null);
DatabaseManager.getInstance().closeConnection(con);
// DatabaseManager.getInstance().closeConnection(ps, null);
// DatabaseManager.getInstance().closeConnection(con);

DatabaseManager dbm = DatabaseManager.getInstance();
DatabaseType dbType = dbm.getDbInfo().getDbType();
Expand Down Expand Up @@ -189,14 +188,13 @@ public boolean dropTable(String tablename) {

Logger.log("[DROP TABLE QUERY] " + query);

try {
Connection con = (Connection) DatabaseManager.getInstance().getConnection();
try (Connection con = (Connection) DatabaseManager.getInstance().getConnection()){
PreparedStatement ps = con.prepareStatement(query.toString());
ps.executeUpdate();

// take him to gulag too
DatabaseManager.getInstance().closeConnection(ps, null);
DatabaseManager.getInstance().closeConnection(con);
// DatabaseManager.getInstance().closeConnection(ps, null);
// DatabaseManager.getInstance().closeConnection(con);

DatabaseManager dbm = DatabaseManager.getInstance();
DatabaseType dbType = dbm.getDbInfo().getDbType();
Expand Down Expand Up @@ -249,8 +247,7 @@ public boolean insertData(String tablename, List<Column> columns) {

Logger.log("[INSERT DATA QUERY] " + query);

try {
Connection con = (Connection) DatabaseManager.getInstance().getConnection();
try (Connection con = (Connection) DatabaseManager.getInstance().getConnection()) {
PreparedStatement s = con.prepareStatement(query.toString());
for (int i = 0; i < columns.size(); i++) {
switch (columns.get(i).getDataType()) {
Expand Down Expand Up @@ -317,8 +314,8 @@ public boolean insertData(String tablename, List<Column> columns) {
s.executeUpdate();

// here take back your connection you damn...m...f
DatabaseManager.getInstance().closeConnection(s, null);
DatabaseManager.getInstance().closeConnection(con);
// DatabaseManager.getInstance().closeConnection(s, null);
// DatabaseManager.getInstance().closeConnection(con);
return true;
} catch (SQLException e) {
Logger.log("[ERROR] " + e.getMessage());
Expand Down Expand Up @@ -368,14 +365,13 @@ public boolean updateData(String tablename, Column primaryKey, List<Column> colu

Logger.log("[UPDATE DATA QUERY] " + query);

try {
Connection con = (Connection) DatabaseManager.getInstance().getConnection();
try (Connection con = (Connection) DatabaseManager.getInstance().getConnection()) {
PreparedStatement s = con.prepareStatement(query.toString());
s.executeUpdate();

// again closing as promised xD
DatabaseManager.getInstance().closeConnection(s, null);
DatabaseManager.getInstance().closeConnection(con);
// DatabaseManager.getInstance().closeConnection(s, null);
// DatabaseManager.getInstance().closeConnection(con);
return true;
} catch (SQLException e) {
Logger.log("[ERROR] " + e.getMessage());
Expand All @@ -398,8 +394,7 @@ public boolean deleteData(String tablename, Column primaryKey) {

Logger.log("[DELETE DATA QUERY] " + query);

try {
Connection con = (Connection) DatabaseManager.getInstance().getConnection();
try (Connection con = (Connection) DatabaseManager.getInstance().getConnection()) {
PreparedStatement s = con.prepareStatement(query);
switch (primaryKey.getDataType()) {
case VARCHAR:
Expand Down Expand Up @@ -463,8 +458,8 @@ public boolean deleteData(String tablename, Column primaryKey) {
s.executeUpdate();

// close all the fricking con
DatabaseManager.getInstance().closeConnection(s, null);
DatabaseManager.getInstance().closeConnection(con);
// DatabaseManager.getInstance().closeConnection(s, null);
// DatabaseManager.getInstance().closeConnection(con);
return true;
} catch (SQLException e) {
Logger.log("[ERROR] " + e.getMessage());
Expand All @@ -491,8 +486,7 @@ public List<Column> getExactData(String tablename, Column primaryKey) {

Logger.log("[GET EXACT DATA QUERY] " + query);

try {
Connection con = (Connection) DatabaseManager.getInstance().getConnection();
try (Connection con = (Connection) DatabaseManager.getInstance().getConnection()) {
PreparedStatement s = con.prepareStatement(query);

switch (primaryKey.getDataType()) {
Expand Down Expand Up @@ -600,19 +594,19 @@ public List<Column> getExactData(String tablename, Column primaryKey) {

result.add(rCol);
}

// close the PS and RS
DatabaseManager.getInstance().closeConnection(s, rs);
DatabaseManager.getInstance().closeConnection(con);
} catch (SQLException e) {
// close all the fricking con
DatabaseManager.getInstance().closeConnection(s, rs);
DatabaseManager.getInstance().closeConnection(con);
// DatabaseManager.getInstance().closeConnection(s, rs);
// DatabaseManager.getInstance().closeConnection(con);
Logger.log("[ERROR] " + e.getMessage());
}
}
// close the PS and RS
// DatabaseManager.getInstance().closeConnection(s, rs);
// DatabaseManager.getInstance().closeConnection(con);
} catch (SQLException e) {
Logger.log("[ERROR] " + e.getMessage());
// close all the fricking con "PROPERLYYY"
// DatabaseManager.getInstance().closeConnection(con);
}

return result;
Expand All @@ -637,8 +631,7 @@ public List<List<Column>> findData(String tablename, Column column) {

Logger.log("[FIND DATA QUERY] " + query);

try {
Connection con = (Connection) DatabaseManager.getInstance().getConnection();
try (Connection con = (Connection) DatabaseManager.getInstance().getConnection()) {
PreparedStatement s = con.prepareStatement(query);

switch (column.getDataType()) {
Expand Down Expand Up @@ -704,7 +697,7 @@ public List<List<Column>> findData(String tablename, Column column) {
ResultSet rs = s.executeQuery();

// primary key and so on
List<Column> allTableCols = table.getColumns();
List<Column> allTableCols = new ArrayList<>(table.getColumns());
allTableCols.add(0, table.getPrimaryKey());

while (rs.next()) {
Expand Down Expand Up @@ -748,10 +741,13 @@ public List<List<Column>> findData(String tablename, Column column) {
}
results.add(temp);
}
DatabaseManager.getInstance().closeConnection(s, rs);
DatabaseManager.getInstance().closeConnection(con);

// DatabaseManager.getInstance().closeConnection(s, rs);
// DatabaseManager.getInstance().closeConnection(con);
} catch (SQLException e) {
Logger.log("[ERROR] " + e.getMessage());

// DatabaseManager.getInstance().closeConnection(con);
}

return results;
Expand All @@ -769,15 +765,14 @@ public List<List<Column>> getAllDatas(String tablename) {
String safeTableName = this.sanitizeSQLQuery(tablename);
Table table = DatabaseManager.getInstance().getTables().get(tablename);
String query = "SELECT * FROM `" + safeTableName + "`;";
try {
Logger.log("[GET ALL DATAS QUERY] " + query);
Logger.log("[GET ALL DATAS QUERY] " + query);

Connection con = (Connection) DatabaseManager.getInstance().getConnection();
try (Connection con = (Connection) DatabaseManager.getInstance().getConnection();) {
PreparedStatement s = con.prepareStatement(query);
ResultSet rs = s.executeQuery();

// primary key and so on
List<Column> allTableCols = table.getColumns();
List<Column> allTableCols = new ArrayList<>(table.getColumns());
allTableCols.add(0, table.getPrimaryKey());

while (rs.next()) {
Expand Down Expand Up @@ -822,11 +817,13 @@ public List<List<Column>> getAllDatas(String tablename) {
}
allDatas.add(temp);
}

// close both PS and RS
DatabaseManager.getInstance().closeConnection(s, rs);
DatabaseManager.getInstance().closeConnection(con);
// DatabaseManager.getInstance().closeConnection(s, rs);
// DatabaseManager.getInstance().closeConnection(con);
} catch (SQLException e) {
Logger.log("[ERROR] " + e.getMessage());
// DatabaseManager.getInstance().closeConnection(con);
}
return allDatas;
}
Expand Down
Loading

0 comments on commit 1e5e3a7

Please sign in to comment.