Skip to content

Support Statement.closeOnCompletion. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/org/tarantool/jdbc/SQLDatabaseMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,8 @@ private SQLNullResultSet sqlNullResultSet(List<String> columnNames, List<List<Ob
return new SQLNullResultSet(SQLResultHolder.ofQuery(meta, rows), createMetadataStatement());
}

private SQLStatement createMetadataStatement() throws SQLException {
return connection.createStatement().unwrap(SQLStatement.class);
private TarantoolStatement createMetadataStatement() throws SQLException {
return connection.createStatement().unwrap(TarantoolStatement.class);
}

private static <T> T ensureType(Class<T> cls, Object v) throws Exception {
Expand All @@ -1152,7 +1152,7 @@ private SQLNullResultSet emptyResultSet(List<String> colNames) throws SQLExcepti

protected class SQLNullResultSet extends SQLResultSet {

public SQLNullResultSet(SQLResultHolder holder, SQLStatement ownerStatement) throws SQLException {
public SQLNullResultSet(SQLResultHolder holder, TarantoolStatement ownerStatement) throws SQLException {
super(holder, ownerStatement);
}

Expand Down
20 changes: 14 additions & 6 deletions src/main/java/org/tarantool/jdbc/SQLResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@

public class SQLResultSet implements ResultSet {

private final CursorIterator<List<Object>> iterator;
private final SQLResultSetMetaData metaData;
private CursorIterator<List<Object>> iterator;
private SQLResultSetMetaData metaData;

private Map<String, Integer> columnByNameLookups;
private boolean lastColumnWasNull;

private final Statement statement;
private final TarantoolStatement statement;
private final int maxRows;

private AtomicBoolean isClosed = new AtomicBoolean(false);
Expand All @@ -53,7 +53,7 @@ public class SQLResultSet implements ResultSet {
private final int concurrencyLevel;
private final int holdability;

public SQLResultSet(SQLResultHolder holder, SQLStatement ownerStatement) throws SQLException {
public SQLResultSet(SQLResultHolder holder, TarantoolStatement ownerStatement) throws SQLException {
metaData = new SQLResultSetMetaData(holder.getSqlMetadata());
statement = ownerStatement;
scrollType = statement.getResultSetType();
Expand Down Expand Up @@ -108,7 +108,13 @@ protected Number getNullableNumber(int columnIndex) throws SQLException {
@Override
public void close() throws SQLException {
if (isClosed.compareAndSet(false, true)) {
iterator.close();
try {
iterator.close();
iterator = null;
metaData = null;
} finally {
statement.checkCompletion();
}
}
}

Expand Down Expand Up @@ -393,11 +399,13 @@ public String getCursorName() throws SQLException {

@Override
public ResultSetMetaData getMetaData() throws SQLException {
checkNotClosed();
return metaData;
}

@Override
public int findColumn(String columnLabel) throws SQLException {
checkNotClosed();
return findColumnIndex(columnLabel);
}

Expand Down Expand Up @@ -1122,7 +1130,7 @@ public <T> T unwrap(Class<T> type) throws SQLException {
if (isWrapperFor(type)) {
return type.cast(this);
}
throw new SQLNonTransientException("ResultSet does not wrap " + type.getName());
throw new SQLNonTransientException("SQLResultSet does not wrap " + type.getName());
}

@Override
Expand Down
51 changes: 34 additions & 17 deletions src/main/java/org/tarantool/jdbc/SQLStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* types of cursors.
* Supports only {@link ResultSet#HOLD_CURSORS_OVER_COMMIT} holdability type.
*/
public class SQLStatement implements Statement {
public class SQLStatement implements TarantoolStatement {

protected final SQLConnection connection;

Expand All @@ -31,6 +31,8 @@ public class SQLStatement implements Statement {
protected SQLResultSet resultSet;
protected int updateCount;

private boolean isCloseOnCompletion;

private final int resultSetType;
private final int resultSetConcurrency;
private final int resultSetHoldability;
Expand Down Expand Up @@ -310,23 +312,44 @@ public boolean isPoolable() throws SQLException {
throw new SQLFeatureNotSupportedException();
}

/**
* {@inheritDoc}
* <p>
* <strong>Impl Note:</strong> this method doesn't affect
* execution methods which close the last result set implicitly.
* It is applied only when {@link ResultSet#close()} is invoked
* explicitly by the app.
*
* @throws SQLException if this method is called on a closed
* {@code Statement}
*/
@Override
public void closeOnCompletion() throws SQLException {

checkNotClosed();
isCloseOnCompletion = true;
}

@Override
public boolean isCloseOnCompletion() throws SQLException {
checkNotClosed();
return false;
return isCloseOnCompletion;
}

@Override
public void checkCompletion() throws SQLException {
if (isCloseOnCompletion &&
resultSet != null &&
resultSet.isClosed()) {
close();
}
}

@Override
public <T> T unwrap(Class<T> type) throws SQLException {
if (isWrapperFor(type)) {
return type.cast(this);
}
throw new SQLNonTransientException("Statement does not wrap " + type.getName());
throw new SQLNonTransientException("SQLStatement does not wrap " + type.getName());
}

@Override
Expand All @@ -338,15 +361,18 @@ public boolean isWrapperFor(Class<?> type) throws SQLException {
* Clears the results of the most recent execution.
*/
protected void discardLastResults() throws SQLException {
final SQLResultSet lastResultSet = resultSet;

clearWarnings();
updateCount = -1;
if (resultSet != null) {
resultSet = null;

if (lastResultSet != null) {
try {
resultSet.close();
lastResultSet.close();
} catch (Exception ignored) {
// No-op.
}
resultSet = null;
}
}

Expand Down Expand Up @@ -375,16 +401,7 @@ protected boolean executeInternal(String sql, Object... params) throws SQLExcept
return holder.isQueryResult();
}

/**
* Returns {@link ResultSet} which will be initialized by <code>data</code>.
*
* @param data predefined result to be wrapped by {@link ResultSet}
*
* @return wrapped result
*
* @throws SQLException if a database access error occurs or
* this method is called on a closed <code>Statement</code>
*/
@Override
public ResultSet executeMetadata(SQLResultHolder data) throws SQLException {
checkNotClosed();
return createResultSet(data);
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/tarantool/jdbc/TarantoolStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.tarantool.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* Tarantool specific statement extensions.
*/
public interface TarantoolStatement extends Statement {

/**
* Checks for statement completion and closes itself,
* according to {@link Statement#closeOnCompletion()}.
*/
void checkCompletion() throws SQLException;

/**
* Returns {@link ResultSet} which will be initialized by <code>data</code>.
*
* @param data predefined result to be wrapped by {@link ResultSet}
*
* @return wrapped result
*
* @throws SQLException if a database access error occurs or
* this method is called on a closed <code>Statement</code>
*/
ResultSet executeMetadata(SQLResultHolder data) throws SQLException;

}
4 changes: 3 additions & 1 deletion src/test/java/org/tarantool/TarantoolConnectionSQLOpsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public void setup() {

@AfterEach
public void tearDown() {
connection.close();
if (connection != null) {
connection.close();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.tarantool.TestAssumptions.assumeMinimalServerVersion;
import static org.tarantool.TestUtils.makeInstanceEnv;

import org.tarantool.ServerVersion;
import org.tarantool.TarantoolClientConfig;
import org.tarantool.TarantoolConsole;
import org.tarantool.TarantoolControl;
import org.tarantool.protocol.TarantoolPacket;

Expand All @@ -25,6 +28,7 @@
public class JdbcConnectionTimeoutIT {

protected static final String LUA_FILE = "jdk-testing.lua";
private static final String HOST = "localhost";
protected static final int LISTEN = 3301;
protected static final int ADMIN = 3313;
private static final String INSTANCE_NAME = "jdk-testing";
Expand All @@ -47,6 +51,7 @@ public static void tearDownEnv() {

@BeforeEach
void setUp() throws SQLException {
assumeMinimalServerVersion(TarantoolConsole.open(HOST, ADMIN), ServerVersion.V_2_1);
connection = new SQLConnection("", new Properties()) {
@Override
protected SQLTarantoolClientImpl makeSqlClient(String address, TarantoolClientConfig config) {
Expand All @@ -66,7 +71,9 @@ protected void completeSql(TarantoolOp<?> operation, TarantoolPacket pack) {

@AfterEach
void tearDown() throws SQLException {
connection.close();
if (connection != null) {
connection.close();
}
}

@Test
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/org/tarantool/jdbc/JdbcResultSetIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ public void testResultSetMetadataAfterClose() throws SQLException {
assertNotNull(resultSet);
ResultSetMetaData metaData = resultSet.getMetaData();
assertNotNull(metaData);

int expectedColumnSize = 2;
assertEquals(expectedColumnSize, metaData.getColumnCount());

resultSet.close();
assertEquals(metaData, resultSet.getMetaData());
assertEquals(expectedColumnSize, metaData.getColumnCount());
}

@Test
Expand Down
Loading