Skip to content

Commit ac5af80

Browse files
committed
Fixed an issue where the pool is being closed instead of the requested connection
1 parent 92f5aab commit ac5af80

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/main/java/me/cobeine/sqlava/connection/database/query/PreparedQuery.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66

77
import javax.sql.rowset.CachedRowSet;
88
import javax.sql.rowset.RowSetProvider;
9-
import java.sql.PreparedStatement;
10-
import java.sql.ResultSet;
11-
import java.sql.SQLException;
12-
import java.sql.Statement;
9+
import java.sql.*;
1310

1411
/**
1512
* @author <a href="https://github.com/Cobeine">Cobeine</a>
1613
*/
1714

1815
public class PreparedQuery {
1916
private final MySQLConnection sqlConnection;
17+
private Connection connection;
2018
private PreparedStatement statement;
2119
boolean batched;
2220
public PreparedQuery(@NotNull MySQLConnection sqlConnection, @NotNull String buildQuery) {
2321
this.sqlConnection = sqlConnection;
2422
try {
25-
this.statement = sqlConnection.getConnection().getConnection().prepareStatement(buildQuery);
23+
this.connection = sqlConnection.getConnection().getConnection();
24+
this.statement = connection.prepareStatement(buildQuery);
2625
} catch (SQLException e) {
2726
sqlConnection.getLogger().severe(String.format("Failed to prepare statement of query '%s': %s", buildQuery, e));
2827
}
@@ -47,9 +46,9 @@ public int[] executeBatch() throws SQLException {
4746
statement.close();
4847
}
4948

50-
if (sqlConnection.getConnection() != null) {
51-
sqlConnection.getConnection().getConnection().commit();
52-
sqlConnection.getConnection().close();
49+
if (connection != null) {
50+
connection.commit();
51+
connection.close();
5352
}
5453
}
5554
}
@@ -82,8 +81,8 @@ public int executeUpdate() throws SQLException {
8281
if (statement != null && !statement.isClosed())
8382
statement.close();
8483

85-
if (sqlConnection.getConnection() != null)
86-
sqlConnection.getConnection().close();
84+
if (connection != null)
85+
connection.close();
8786

8887
}
8988
}
@@ -102,8 +101,8 @@ public int executeUpdateWithKeys() throws SQLException {
102101
if (statement != null)
103102
statement.close();
104103

105-
if (sqlConnection.getConnection() != null)
106-
sqlConnection.getConnection().close();
104+
if (connection != null)
105+
connection.close();
107106

108107
}
109108
}
@@ -120,8 +119,8 @@ public ResultSet executeQuery() throws SQLException {
120119
if (statement != null && !statement.isClosed())
121120
statement.close();
122121

123-
if (sqlConnection.getConnection() != null)
124-
sqlConnection.getConnection().close();
122+
if (connection != null)
123+
connection.close();
125124

126125
}
127126
return rowSet;
@@ -146,8 +145,8 @@ public void executeUpdateAsync(final Callback<Integer, SQLException> callback) {
146145
private void addBatch() throws SQLException {
147146
if (batched)
148147
return;
149-
if (sqlConnection.getConnection().getConnection().getAutoCommit()) {
150-
sqlConnection.getConnection().setAutoCommit(false);
148+
if (connection.getAutoCommit()) {
149+
connection.setAutoCommit(false);
151150
}
152151
statement.addBatch();
153152
batched = true;
@@ -190,7 +189,7 @@ public void executeQueryAsync(final Callback<ResultSet, SQLException> callback)
190189
}
191190
@SuppressWarnings("unused")
192191
public void rollback() throws SQLException {
193-
if (sqlConnection.getConnection() != null)
194-
sqlConnection.getConnection().getConnection().rollback();
192+
if (connection != null)
193+
connection.rollback();
195194
}
196195
}

0 commit comments

Comments
 (0)