Skip to content

Commit

Permalink
Merge pull request #1 from AkramLZ/master
Browse files Browse the repository at this point in the history
Fixed an issue related to connection close.
  • Loading branch information
Cobeine authored Dec 19, 2023
2 parents 92f5aab + e654af7 commit af20d1a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'me.Cobeine'
version '1.5.1-SNAPSHOT'
version '1.5.2-SNAPSHOT'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@

import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetProvider;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;

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

public class PreparedQuery {
private final MySQLConnection sqlConnection;
private Connection connection;
private PreparedStatement statement;
boolean batched;
public PreparedQuery(@NotNull MySQLConnection sqlConnection, @NotNull String buildQuery) {
this.sqlConnection = sqlConnection;
try {
this.statement = sqlConnection.getConnection().getConnection().prepareStatement(buildQuery);
this.connection = sqlConnection.getConnection().getConnection();
this.statement = connection.prepareStatement(buildQuery);
} catch (SQLException e) {
sqlConnection.getLogger().severe(String.format("Failed to prepare statement of query '%s': %s", buildQuery, e));
}
Expand All @@ -47,9 +46,9 @@ public int[] executeBatch() throws SQLException {
statement.close();
}

if (sqlConnection.getConnection() != null) {
sqlConnection.getConnection().getConnection().commit();
sqlConnection.getConnection().close();
if (connection != null) {
connection.commit();
connection.close();
}
}
}
Expand Down Expand Up @@ -82,8 +81,8 @@ public int executeUpdate() throws SQLException {
if (statement != null && !statement.isClosed())
statement.close();

if (sqlConnection.getConnection() != null)
sqlConnection.getConnection().close();
if (connection != null)
connection.close();

}
}
Expand All @@ -102,8 +101,8 @@ public int executeUpdateWithKeys() throws SQLException {
if (statement != null)
statement.close();

if (sqlConnection.getConnection() != null)
sqlConnection.getConnection().close();
if (connection != null)
connection.close();

}
}
Expand All @@ -120,8 +119,8 @@ public ResultSet executeQuery() throws SQLException {
if (statement != null && !statement.isClosed())
statement.close();

if (sqlConnection.getConnection() != null)
sqlConnection.getConnection().close();
if (connection != null)
connection.close();

}
return rowSet;
Expand All @@ -146,8 +145,8 @@ public void executeUpdateAsync(final Callback<Integer, SQLException> callback) {
private void addBatch() throws SQLException {
if (batched)
return;
if (sqlConnection.getConnection().getConnection().getAutoCommit()) {
sqlConnection.getConnection().setAutoCommit(false);
if (connection.getAutoCommit()) {
connection.setAutoCommit(false);
}
statement.addBatch();
batched = true;
Expand Down Expand Up @@ -190,7 +189,7 @@ public void executeQueryAsync(final Callback<ResultSet, SQLException> callback)
}
@SuppressWarnings("unused")
public void rollback() throws SQLException {
if (sqlConnection.getConnection() != null)
sqlConnection.getConnection().getConnection().rollback();
if (connection != null)
connection.rollback();
}
}

0 comments on commit af20d1a

Please sign in to comment.