Skip to content

Commit 0784e58

Browse files
committed
[Fix] Connection Leaks
1 parent 75aabd7 commit 0784e58

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

src/main/java/jp/azisaba/lgw/kdstatus/KDStatusReloaded.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package jp.azisaba.lgw.kdstatus;
22

33
import java.io.File;
4+
import java.sql.Connection;
45
import java.sql.PreparedStatement;
56
import java.sql.SQLException;
67
import java.util.logging.Level;
@@ -66,7 +67,8 @@ public void onEnable() {
6667

6768
if(sql.isConnected()){
6869
getLogger().info("SQL Testing...");
69-
try(PreparedStatement pstmt = sql.getConnection().prepareStatement("SELECT 1")) {
70+
try(Connection conn = sql.getConnection();
71+
PreparedStatement pstmt = conn.prepareStatement("SELECT 1")) {
7072
if(pstmt.executeQuery().next()) {
7173
getLogger().info("SQL Test was success!");
7274
} else {
@@ -120,8 +122,8 @@ public void onDisable() {
120122
if(sql.isConnected()){
121123
sql.close();
122124
}
123-
saveTask.cancel();
124-
dbCheckTask.cancel();
125+
if(saveTask!=null) saveTask.cancel();
126+
if(dbCheckTask!=null) dbCheckTask.cancel();
125127
Bukkit.getLogger().info(getName() + " disabled.");
126128
}
127129

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public void connect() {
5353
config.setPassword(password);
5454
config.setJdbcUrl(jdbcUrl);
5555
config.setMaximumPoolSize(maxPoolSize);
56+
config.setLeakDetectionThreshold(2000);
5657
// config.setConnectionInitSql("SELECT 1");
5758
config.setAutoCommit(true);
5859
config.setConnectionTimeout(1500); // TODO change this
@@ -164,4 +165,14 @@ public boolean executeUpdate(String sql) {
164165
return false;
165166
}
166167
}
168+
169+
public boolean isConnectionAlive() {
170+
try(Connection conn = getConnection();
171+
PreparedStatement pstmt = conn.prepareStatement("SELECT 1");
172+
ResultSet rs = pstmt.executeQuery()) {
173+
return rs.next();
174+
} catch (SQLException e) {
175+
return false;
176+
}
177+
}
167178
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ public class PlayerDataMySQLController {
1818

1919
public void createTable(){
2020

21-
try{
22-
plugin.getLogger().info("Creating database table...");
23-
PreparedStatement ps = plugin.sql.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS kill_death_data "
21+
try(Connection conn = plugin.sql.getConnection();
22+
PreparedStatement ps = conn.prepareStatement("CREATE TABLE IF NOT EXISTS kill_death_data "
2423
+ "(uuid VARCHAR(64) NOT NULL ,name VARCHAR(36) NOT NULL," +
2524
"kills INT DEFAULT 0, " +
2625
"deaths INT DEFAULT 0 ," +
2726
"daily_kills INT DEFAULT 0," +
2827
"monthly_kills INT DEFAULT 0," +
2928
"yearly_kills INT DEFAULT 0," +
30-
"last_updated BIGINT DEFAULT -1 )");
29+
"last_updated BIGINT DEFAULT -1 )")) {
30+
plugin.getLogger().info("Creating database table...");
3131

3232
ps.executeUpdate();
3333
ps.close();
@@ -246,13 +246,12 @@ public int getRank(UUID uuid,TimeUnit unit){
246246

247247
public List<KillRankingData> getTopKillRankingData(TimeUnit unit, int count){
248248

249-
try {
250-
251-
PreparedStatement ps = plugin.sql.getConnection().prepareStatement("select uuid, name, " + unit.getSqlColumnName()
249+
try(Connection conn = plugin.sql.getConnection();
250+
PreparedStatement ps = conn.prepareStatement("select uuid, name, " + unit.getSqlColumnName()
252251
+ " from kill_death_data"
253252
+ " where last_updated >= ?"
254253
+ " order by " + unit.getSqlColumnName() + " DESC"
255-
+ " LIMIT ?");
254+
+ " LIMIT ?")) {
256255

257256
ps.setLong(1,TimeUnit.getFirstMilliSecond(unit));
258257
ps.setInt(2,count);
@@ -275,8 +274,6 @@ public List<KillRankingData> getTopKillRankingData(TimeUnit unit, int count){
275274

276275
}
277276

278-
ps.close();
279-
280277
return ranking;
281278

282279
} catch (SQLException throwables) {

src/main/java/jp/azisaba/lgw/kdstatus/task/DBConnectionCheckTask.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import lombok.RequiredArgsConstructor;
55
import org.bukkit.scheduler.BukkitRunnable;
66

7+
import java.sql.Connection;
78
import java.sql.PreparedStatement;
89
import java.sql.SQLException;
910
import java.util.logging.Level;
@@ -13,19 +14,19 @@ public class DBConnectionCheckTask extends BukkitRunnable {
1314
private final KDStatusReloaded plugin;
1415
@Override
1516
public void run() {
16-
try {
17-
if(plugin.sql.getConnection() == null) {
17+
try (Connection conn = plugin.sql.getConnection()){
18+
if(conn == null) {
1819
plugin.sql.connect();
1920
}
20-
PreparedStatement pstmt = plugin.sql.getConnection().prepareStatement("SELECT 1");
21-
if(pstmt.executeQuery().next()) {
22-
if(plugin.getPluginConfig().showLogInConsole) {
23-
plugin.getLogger().info("SQL Connection is alive!");
21+
try(Connection con = plugin.sql.getConnection();
22+
PreparedStatement pstmt = con.prepareStatement("SELECT 1")) {
23+
if (pstmt.executeQuery().next()) {
24+
if (plugin.getPluginConfig().showLogInConsole) {
25+
plugin.getLogger().info("SQL Connection is alive!");
26+
}
27+
return;
2428
}
25-
pstmt.close();
26-
return;
2729
}
28-
pstmt.close();
2930
} catch (SQLException e) {
3031
plugin.getLogger().log(Level.WARNING, "Failed to pass health check", e);
3132
}

0 commit comments

Comments
 (0)