Skip to content

Commit 3aec67a

Browse files
authored
Fix connection leak
1 parent 5d60769 commit 3aec67a

1 file changed

Lines changed: 34 additions & 33 deletions

File tree

src/main/java/dev/noah/perplayerkit/storage/SQLStorage.java

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,42 @@
2222
import dev.noah.perplayerkit.storage.exceptions.StorageOperationException;
2323
import dev.noah.perplayerkit.storage.sql.SQLDatabase;
2424

25+
import java.sql.Connection;
2526
import java.sql.PreparedStatement;
2627
import java.sql.ResultSet;
2728
import java.sql.SQLException;
2829

2930
public class SQLStorage implements StorageManager {
3031

31-
3232
private final SQLDatabase db;
3333

3434
public SQLStorage(SQLDatabase db) {
3535
this.db = db;
3636
}
3737

38-
39-
private void createTable() throws SQLException {
40-
try (PreparedStatement ps = db.getConnection().prepareStatement(
38+
private void createTable() throws SQLException, StorageConnectionException {
39+
// FIX: The Connection is now inside the try-with-resources block
40+
try (Connection conn = db.getConnection();
41+
PreparedStatement ps = conn.prepareStatement(
4142
"CREATE TABLE IF NOT EXISTS kits (KITID VARCHAR(100), KITDATA TEXT(15000), PRIMARY KEY (KITID))")) {
4243
ps.executeUpdate();
4344
}
4445
}
4546

46-
4747
@Override
4848
public void init() throws StorageOperationException {
4949
try {
5050
createTable();
51-
}catch (SQLException e) {
51+
} catch (SQLException | StorageConnectionException e) { // FIX: Added StorageConnectionException
5252
throw new StorageOperationException("Failed to initialize the database", e);
5353
}
5454
}
5555

5656
@Override
5757
public void connect() throws StorageConnectionException {
58-
try{
58+
try {
5959
db.connect();
60-
}catch (ClassNotFoundException | SQLException e) {
60+
} catch (ClassNotFoundException | SQLException e) {
6161
throw new StorageConnectionException("Failed to connect to the database", e);
6262
}
6363
}
@@ -78,72 +78,73 @@ public void close() throws StorageConnectionException {
7878

7979
@Override
8080
public void keepAlive() throws StorageConnectionException {
81-
try (PreparedStatement ps = db.getConnection().prepareStatement("SELECT 1");
82-
ResultSet rs = ps.executeQuery()) {
81+
// FIX: The Connection is now inside the try-with-resources block
82+
try (Connection conn = db.getConnection();
83+
PreparedStatement ps = conn.prepareStatement("SELECT 1")) {
84+
ps.executeQuery(); // Some drivers require executeQuery for SELECT
8385
} catch (SQLException e) {
8486
throw new StorageConnectionException("Failed to keep the connection alive", e);
8587
}
8688
}
8789

88-
8990
@Override
9091
public void saveKitDataByID(String kitID, String data) {
91-
try (PreparedStatement ps = db.getConnection().prepareStatement(
92+
// FIX: The Connection is now inside the try-with-resources block
93+
try (Connection conn = db.getConnection();
94+
PreparedStatement ps = conn.prepareStatement(
9295
"REPLACE INTO kits (KITID, KITDATA) VALUES (?,?)")) {
9396
ps.setString(1, kitID);
9497
ps.setString(2, data);
9598
ps.executeUpdate();
96-
} catch (SQLException e) {
99+
} catch (SQLException | StorageConnectionException e) {
97100
e.printStackTrace();
98101
}
99102
}
100103

101-
102104
@Override
103105
public String getKitDataByID(String kitID) {
104-
if (doesKitExistByID(kitID)) {
105-
try (PreparedStatement ps = db.getConnection().prepareStatement(
106+
// FIX: The Connection is now inside the try-with-resources block
107+
try (Connection conn = db.getConnection();
108+
PreparedStatement ps = conn.prepareStatement(
106109
"SELECT KITDATA FROM kits WHERE KITID=?")) {
107-
ps.setString(1, kitID);
108-
try (ResultSet rs = ps.executeQuery()) {
109-
if (rs.next()) {
110-
return rs.getString("KITDATA");
111-
}
110+
ps.setString(1, kitID);
111+
try (ResultSet rs = ps.executeQuery()) {
112+
if (rs.next()) {
113+
return rs.getString("KITDATA");
112114
}
113-
} catch (SQLException e) {
114-
e.printStackTrace();
115115
}
116-
return "Error";
116+
} catch (SQLException | StorageConnectionException e) {
117+
e.printStackTrace();
117118
}
118-
return "Error";
119+
return "Error"; // Return "Error" if not found or on exception
119120
}
120121

121-
122122
@Override
123123
public boolean doesKitExistByID(String kitID) {
124-
try (PreparedStatement ps = db.getConnection().prepareStatement(
124+
// FIX: The Connection is now inside the try-with-resources block
125+
try (Connection conn = db.getConnection();
126+
PreparedStatement ps = conn.prepareStatement(
125127
"SELECT KITID FROM kits WHERE KITID=?")) {
126128
ps.setString(1, kitID);
127129
try (ResultSet rs = ps.executeQuery()) {
128130
return rs.next();
129131
}
130-
} catch (SQLException e) {
132+
} catch (SQLException | StorageConnectionException e) {
131133
e.printStackTrace();
132134
}
133135
return false;
134136
}
135137

136-
137138
@Override
138139
public void deleteKitByID(String kitID) {
139-
try (PreparedStatement ps = db.getConnection().prepareStatement(
140+
// FIX: The Connection is now inside the try-with-resources block
141+
try (Connection conn = db.getConnection();
142+
PreparedStatement ps = conn.prepareStatement(
140143
"DELETE FROM kits WHERE KITID=?")) {
141144
ps.setString(1, kitID);
142145
ps.executeUpdate();
143-
} catch (SQLException e) {
146+
} catch (SQLException | StorageConnectionException e) {
144147
e.printStackTrace();
145148
}
146149
}
147-
148-
149150
}

0 commit comments

Comments
 (0)