[FIX] Закрытие соеденений после запроса

This commit is contained in:
Gravit 2019-03-16 00:59:17 +07:00
parent dc2a978716
commit 0268a48fe6
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
3 changed files with 33 additions and 30 deletions

View file

@ -4,6 +4,7 @@
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
import java.io.IOException; import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -64,8 +65,8 @@ protected Entry fetchEntry(UUID uuid) throws IOException {
} }
private Entry query(String sql, String value) throws IOException { private Entry query(String sql, String value) throws IOException {
try { try(Connection c = mySQLHolder.getConnection()) {
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(sql); PreparedStatement s = c.prepareStatement(sql);
s.setString(1, value); s.setString(1, value);
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
try (ResultSet set = s.executeQuery()) { try (ResultSet set = s.executeQuery()) {
@ -78,8 +79,8 @@ private Entry query(String sql, String value) throws IOException {
@Override @Override
protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException { protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException {
try { try(Connection c = mySQLHolder.getConnection()) {
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(updateAuthSQL); PreparedStatement s = c.prepareStatement(updateAuthSQL);
s.setString(1, username); // Username case s.setString(1, username); // Username case
s.setString(2, accessToken); s.setString(2, accessToken);
s.setString(3, uuid.toString()); s.setString(3, uuid.toString());
@ -92,8 +93,8 @@ protected boolean updateAuth(UUID uuid, String username, String accessToken) thr
@Override @Override
protected boolean updateServerID(UUID uuid, String serverID) throws IOException { protected boolean updateServerID(UUID uuid, String serverID) throws IOException {
try { try(Connection c = mySQLHolder.getConnection()) {
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(updateServerIDSQL); PreparedStatement s = c.prepareStatement(updateServerIDSQL);
s.setString(1, serverID); s.setString(1, serverID);
s.setString(2, uuid.toString()); s.setString(2, uuid.toString());
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);

View file

@ -63,8 +63,7 @@ public class MysqlHWIDHandler extends HWIDHandler {
public void check0(HWID hwid, String username) throws HWIDException { public void check0(HWID hwid, String username) throws HWIDException {
if (hwid instanceof OshiHWID) { if (hwid instanceof OshiHWID) {
OshiHWID oshiHWID = (OshiHWID) hwid; OshiHWID oshiHWID = (OshiHWID) hwid;
try { try(Connection c = mySQLHolder.getConnection()) {
Connection c = mySQLHolder.getConnection();
PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1", PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1",
userFieldHwid, userFieldLogin, tableUsers, userFieldLogin)); userFieldHwid, userFieldLogin, tableUsers, userFieldLogin));
@ -178,12 +177,7 @@ public void setIsBanned(HWID hwid, boolean isBanned) {
LogHelper.debug("%s Request HWID: %s", isBanned ? "Ban" : "UnBan", hwid.toString()); LogHelper.debug("%s Request HWID: %s", isBanned ? "Ban" : "UnBan", hwid.toString());
if (hwid instanceof OshiHWID) { if (hwid instanceof OshiHWID) {
OshiHWID oshiHWID = (OshiHWID) hwid; OshiHWID oshiHWID = (OshiHWID) hwid;
Connection c = null; try(Connection c = mySQLHolder.getConnection()) {
try {
c = mySQLHolder.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
try (PreparedStatement a = c.prepareStatement(queryBan)) { try (PreparedStatement a = c.prepareStatement(queryBan)) {
String[] replaceParamsUpd = {"totalMemory", String.valueOf(oshiHWID.totalMemory), "serialNumber", oshiHWID.serialNumber, "HWDiskSerial", oshiHWID.HWDiskSerial, "processorID", oshiHWID.processorID, "isBanned", isBanned ? "1" : "0"}; String[] replaceParamsUpd = {"totalMemory", String.valueOf(oshiHWID.totalMemory), "serialNumber", oshiHWID.serialNumber, "HWDiskSerial", oshiHWID.HWDiskSerial, "processorID", oshiHWID.processorID, "isBanned", isBanned ? "1" : "0"};
for (int i = 0; i < paramsBan.length; i++) { for (int i = 0; i < paramsBan.length; i++) {
@ -194,6 +188,10 @@ public void setIsBanned(HWID hwid, boolean isBanned) {
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} catch (SQLException e) {
e.printStackTrace();
}
} }
} }
@ -214,9 +212,8 @@ public void unban(List<HWID> list) {
@Override @Override
public List<HWID> getHwid(String username) { public List<HWID> getHwid(String username) {
ArrayList<HWID> list = new ArrayList<>(); ArrayList<HWID> list = new ArrayList<>();
try { try(Connection c = mySQLHolder.getConnection()) {
LogHelper.debug("Try find HWID from username %s", username); LogHelper.debug("Try find HWID from username %s", username);
Connection c = mySQLHolder.getConnection();
PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1", userFieldHwid, userFieldLogin, tableUsers, userFieldLogin)); PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1", userFieldHwid, userFieldLogin, tableUsers, userFieldLogin));
s.setString(1, username); s.setString(1, username);

View file

@ -8,6 +8,7 @@
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.SecurityHelper;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -28,7 +29,9 @@ public void init() {
@Override @Override
public AuthProviderResult auth(String login, String password, String ip) throws SQLException, AuthException { public AuthProviderResult auth(String login, String password, String ip) throws SQLException, AuthException {
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(query); try(Connection c = mySQLHolder.getConnection())
{
PreparedStatement s = c.prepareStatement(query);
String[] replaceParams = {"login", login, "password", password, "ip", ip}; String[] replaceParams = {"login", login, "password", password, "ip", ip};
for (int i = 0; i < queryParams.length; i++) for (int i = 0; i < queryParams.length; i++)
s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams)); s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams));
@ -40,6 +43,8 @@ public AuthProviderResult auth(String login, String password, String ip) throws
} }
} }
}
@Override @Override
public void close() { public void close() {
mySQLHolder.close(); mySQLHolder.close();