Skip to content

Commit

Permalink
FMWK-588 Handle closed state in statements (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn authored Oct 30, 2024
1 parent 36de5ea commit 56edd55
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ private Object[] buildSqlParameters(String sql) {

@Override
public ResultSet executeQuery() throws SQLException {
checkClosed();

logger.info(() -> format("executeQuery: %s, params: %s", sqlStatement, Arrays.toString(sqlParameters)));
AerospikeQuery query = parseQuery(sqlStatement, Arrays.asList(sqlParameters));
runQuery(query);
Expand Down Expand Up @@ -178,6 +180,8 @@ public void setObject(int parameterIndex, Object x) throws SQLException {

@Override
public boolean execute() throws SQLException {
checkClosed();

logger.info(() -> format("execute: %s, params: %s", sqlStatement, Arrays.toString(sqlParameters)));
AerospikeQuery query = parseQuery(sqlStatement, Arrays.asList(sqlParameters));
runQuery(query);
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/aerospike/jdbc/AerospikeStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;

import static java.lang.String.format;
Expand All @@ -33,6 +34,8 @@ public class AerospikeStatement implements Statement, SimpleWrapper {
protected final IAerospikeClient client;
protected final AerospikeConnection connection;

private final AtomicBoolean isClosed = new AtomicBoolean();

protected String catalog;
protected ResultSet resultSet;
protected int updateCount;
Expand All @@ -48,6 +51,8 @@ public AerospikeStatement(IAerospikeClient client, AerospikeConnection connectio

@Override
public ResultSet executeQuery(String sql) throws SQLException {
checkClosed();

logger.info(() -> "executeQuery: " + sql);
AerospikeQuery query = parseQuery(sql, null);
runQuery(query);
Expand Down Expand Up @@ -82,7 +87,7 @@ public int executeUpdate(String sql) throws SQLException {

@Override
public void close() {
// do nothing
isClosed.set(true);
}

@Override
Expand Down Expand Up @@ -142,6 +147,8 @@ public void setCursorName(String name) throws SQLException {

@Override
public boolean execute(String sql) throws SQLException {
checkClosed();

logger.info(() -> "execute: " + sql);
AerospikeQuery query = parseQuery(sql, null);
runQuery(query);
Expand Down Expand Up @@ -281,7 +288,7 @@ public int getResultSetHoldability() {

@Override
public boolean isClosed() {
return false;
return isClosed.get();
}

@Override
Expand All @@ -305,4 +312,10 @@ public void closeOnCompletion() {
public boolean isCloseOnCompletion() {
return false;
}

protected void checkClosed() throws SQLException {
if (isClosed()) {
throw new SQLException("Statement is closed");
}
}
}
11 changes: 11 additions & 0 deletions src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;

public class PreparedQueriesTest {
Expand Down Expand Up @@ -327,4 +328,14 @@ public void testSelectBetweenQuery() throws SQLException {
closeQuietly(resultSet);
}
}

@Test
public void testStatementClosed() {
String query = format("select * from %s limit 10", TABLE_NAME);
assertThrows(SQLException.class, () -> {
PreparedStatement statement = connection.prepareStatement(query);
statement.close();
statement.executeQuery();
});
}
}
11 changes: 11 additions & 0 deletions src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;

public class SimpleQueriesTest {
Expand Down Expand Up @@ -336,4 +337,14 @@ public void testSelectUpperBoundaryBetweenQuery() throws SQLException {
closeQuietly(resultSet);
}
}

@Test
public void testStatementClosed() {
String query = format("SELECT count(*) FROM %s", TABLE_NAME);
assertThrows(SQLException.class, () -> {
Statement statement = connection.createStatement();
statement.close();
statement.executeQuery(query);
});
}
}

0 comments on commit 56edd55

Please sign in to comment.