Skip to content

Commit a1da7a5

Browse files
committed
[Fix] Connection leaking
1 parent 0784e58 commit a1da7a5

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

src/main/java/jp/azisaba/lgw/kdstatus/sql/PlayerDataMySQLController.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,16 @@ public void createTable(){
3939

4040
public boolean exist(UUID uuid){
4141

42-
try {
43-
44-
PreparedStatement ps = plugin.sql.getConnection().prepareStatement("SELECT * FROM kill_death_data WHERE name=?");
42+
try(Connection conn = plugin.sql.getConnection();
43+
PreparedStatement ps = conn.prepareStatement("SELECT * FROM kill_death_data WHERE name=?")) {
4544
ps.setString(1,uuid.toString());
4645

4746
ResultSet result = ps.executeQuery();
4847

49-
if(result.next()){
50-
return true;
51-
}
52-
53-
return false;
48+
boolean isExist = result.next();
49+
result.close();
5450

51+
return isExist;
5552
} catch (SQLException e) {
5653
e.printStackTrace();
5754
}
@@ -65,9 +62,8 @@ public void create(KDUserData data){
6562
if(exist(data.getUuid()))
6663
return;
6764

68-
try {
69-
70-
PreparedStatement ps = plugin.sql.getConnection().prepareStatement("INSERT INTO kill_death_data (uuid,name,kills,deaths,daily_kills,monthly_kills,yearly_kills,last_updated) VALUES (?,?,?,?,?,?,?,?)");
65+
try(Connection conn = plugin.sql.getConnection();
66+
PreparedStatement ps = conn.prepareStatement("INSERT INTO kill_death_data (uuid,name,kills,deaths,daily_kills,monthly_kills,yearly_kills,last_updated) VALUES (?,?,?,?,?,?,?,?)");) {
7167
ps.setString(1,data.getUuid().toString());
7268
ps.setString(2,data.getName());
7369
ps.setInt(3,data.getKills(TimeUnit.LIFETIME));
@@ -78,19 +74,16 @@ public void create(KDUserData data){
7874
ps.setLong(8,data.getLastUpdated());
7975

8076
ps.executeUpdate();
81-
ps.close();
8277

8378
} catch (SQLException e) {
8479
e.printStackTrace();
8580
}
86-
8781
}
8882

8983
public boolean update(KDUserData data){
9084

91-
try {
92-
93-
PreparedStatement ps = plugin.sql.getConnection().prepareStatement("UPDATE kill_death_data SET name=? ,kills=? ,deaths=? ,daily_kills=? ,monthly_kills=? ,yearly_kills=? ,last_updated=? WHERE uuid=?");
85+
try(Connection conn = plugin.sql.getConnection();
86+
PreparedStatement ps = conn.prepareStatement("UPDATE kill_death_data SET name=? ,kills=? ,deaths=? ,daily_kills=? ,monthly_kills=? ,yearly_kills=? ,last_updated=? WHERE uuid=?");) {
9487
ps.setString(8,data.getUuid().toString());
9588
ps.setString(1,data.getName());
9689
ps.setInt(2,data.getKills(TimeUnit.LIFETIME));
@@ -101,7 +94,6 @@ public boolean update(KDUserData data){
10194
ps.setLong(7,data.getLastUpdated());
10295

10396
ps.executeUpdate();
104-
ps.close();
10597
return true;
10698

10799
} catch (SQLException e) {
@@ -113,9 +105,8 @@ public boolean update(KDUserData data){
113105

114106
public BigInteger getKills(@NonNull UUID uuid, @NonNull TimeUnit unit) {
115107

116-
try {
117-
118-
PreparedStatement ps = plugin.sql.getConnection().prepareStatement("SELECT "+ unit.getSqlColumnName() + " FROM kill_death_data WHERE uuid=?");
108+
try(Connection conn = plugin.sql.getConnection();
109+
PreparedStatement ps = conn.prepareStatement("SELECT "+ unit.getSqlColumnName() + " FROM kill_death_data WHERE uuid=?")) {
119110
ps.setString(1, uuid.toString());
120111

121112
ResultSet result = ps.executeQuery();
@@ -125,6 +116,7 @@ public BigInteger getKills(@NonNull UUID uuid, @NonNull TimeUnit unit) {
125116
}
126117

127118
ps.close();
119+
result.close();
128120

129121
return BigInteger.valueOf(-1);
130122

@@ -163,9 +155,8 @@ public BigInteger getDeaths(@NonNull UUID uuid) {
163155

164156
public String getName(@NonNull UUID uuid) {
165157

166-
try {
167-
168-
PreparedStatement ps = plugin.sql.getConnection().prepareStatement("SELECT name FROM kill_death_data WHERE uuid=?");
158+
try(Connection conn = plugin.sql.getConnection();
159+
PreparedStatement ps = conn.prepareStatement("SELECT name FROM kill_death_data WHERE uuid=?")) {
169160
ps.setString(1, uuid.toString());
170161

171162
ResultSet result = ps.executeQuery();
@@ -175,6 +166,7 @@ public String getName(@NonNull UUID uuid) {
175166
}
176167

177168
ps.close();
169+
result.close();
178170

179171
return null;
180172

@@ -212,10 +204,8 @@ public long getLastUpdated(@NonNull UUID uuid) {
212204
}
213205

214206
public ResultSet getRawData(@NonNull UUID uuid) {
215-
216-
PreparedStatement ps;
217-
try {
218-
ps = plugin.sql.getConnection().prepareStatement("SELECT * FROM kill_death_data WHERE uuid=?");
207+
try(Connection conn = plugin.sql.getConnection();
208+
PreparedStatement ps = conn.prepareStatement("SELECT * FROM kill_death_data WHERE uuid=?")) {
219209
ps.setString(1, uuid.toString());
220210
return ps.executeQuery();
221211
} catch (SQLException throwables) {
@@ -227,7 +217,8 @@ public ResultSet getRawData(@NonNull UUID uuid) {
227217
public static final String RANK_QUERY = "SELECT * FROM (SELECT uuid, ?, last_updated, RANK() over (ORDER BY ? DESC) as 'rank' FROM kill_death_data WHERE last_updated > ?) s WHERE s.uuid=?";
228218

229219
public int getRank(UUID uuid,TimeUnit unit){
230-
try(PreparedStatement p = plugin.sql.getConnection().prepareStatement(RANK_QUERY)) {
220+
try(Connection conn = plugin.sql.getConnection();
221+
PreparedStatement p = conn.prepareStatement(RANK_QUERY)) {
231222
p.setString(1, unit.getSqlColumnName());
232223
p.setString(2, unit.getSqlColumnName());
233224
p.setLong(3, TimeUnit.getFirstMilliSecond(unit));
@@ -236,6 +227,7 @@ public int getRank(UUID uuid,TimeUnit unit){
236227
if(result.next()) {
237228
return result.getInt("rank");
238229
}
230+
result.close();
239231
} catch (SQLException e) {
240232
e.printStackTrace();
241233
}

0 commit comments

Comments
 (0)