diff --git a/src/main/java/com/aerospike/jdbc/AerospikePreparedStatement.java b/src/main/java/com/aerospike/jdbc/AerospikePreparedStatement.java index 72bd347..e53c4ce 100644 --- a/src/main/java/com/aerospike/jdbc/AerospikePreparedStatement.java +++ b/src/main/java/com/aerospike/jdbc/AerospikePreparedStatement.java @@ -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); @@ -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); diff --git a/src/main/java/com/aerospike/jdbc/AerospikeStatement.java b/src/main/java/com/aerospike/jdbc/AerospikeStatement.java index aefcd86..3e84aa5 100644 --- a/src/main/java/com/aerospike/jdbc/AerospikeStatement.java +++ b/src/main/java/com/aerospike/jdbc/AerospikeStatement.java @@ -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; @@ -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; @@ -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); @@ -82,7 +87,7 @@ public int executeUpdate(String sql) throws SQLException { @Override public void close() { - // do nothing + isClosed.set(true); } @Override @@ -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); @@ -281,7 +288,7 @@ public int getResultSetHoldability() { @Override public boolean isClosed() { - return false; + return isClosed.get(); } @Override @@ -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"); + } + } } diff --git a/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java b/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java index 20fdab8..0c7574e 100644 --- a/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java +++ b/src/test/java/com/aerospike/jdbc/PreparedQueriesTest.java @@ -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 { @@ -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(); + }); + } } diff --git a/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java b/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java index a7405c1..9edfb08 100644 --- a/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java +++ b/src/test/java/com/aerospike/jdbc/SimpleQueriesTest.java @@ -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 { @@ -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); + }); + } }