Skip to content

Commit

Permalink
fix(tgsql): Delay close of PreparedStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Jan 21, 2025
1 parent 0ef2c95 commit f1e91cb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.tsurugidb.tsubakuro.sql.StatementMetadata;
import com.tsurugidb.tsubakuro.sql.TableMetadata;
import com.tsurugidb.tsubakuro.sql.exception.TargetNotFoundException;
import com.tsurugidb.tsubakuro.util.Owner;

/**
* A basic implementation of {@link SqlProcessor}.
Expand Down Expand Up @@ -190,12 +191,13 @@ public void rollbackTransaction() throws ServerException, IOException, Interrupt
LOG.debug("start prepare: '{}'", statement);
desireActive();
var client = getSqlClient();
try (var prepared = client.prepare(statement).await()) {
try (var preparedOwner = Owner.of(client.prepare(statement).await())) {
var prepared = preparedOwner.get();
var t = transaction.getTransaction();
if (prepared.hasResultRecords()) {
LOG.debug("start query: '{}'", statement);
var result = t.executeQuery(prepared).await();
return new PreparedStatementResult(result);
return new PreparedStatementResult(result, preparedOwner.release());
}
LOG.debug("start execute: '{}'", statement);
var result = t.executeStatement(prepared).await();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@

import com.tsurugidb.tsubakuro.exception.ServerException;
import com.tsurugidb.tsubakuro.sql.ExecuteResult;
import com.tsurugidb.tsubakuro.sql.PreparedStatement;
import com.tsurugidb.tsubakuro.sql.ResultSet;

public class PreparedStatementResult implements AutoCloseable {

private final ResultSet resultSet;
private final ExecuteResult executeResult;
private final PreparedStatement preparedStatement;

public PreparedStatementResult(ResultSet resultSet) {
public PreparedStatementResult(ResultSet resultSet, PreparedStatement preparedStatement) {
this.resultSet = resultSet;
this.executeResult = null;
this.preparedStatement = preparedStatement;
}

public PreparedStatementResult(ExecuteResult executeResult) {
this.resultSet = null;
this.executeResult = executeResult;
this.preparedStatement = null;
}

public ResultSet getResultSet() {
Expand All @@ -46,7 +50,7 @@ public ExecuteResult getExecuteResult() {

@Override
public void close() throws ServerException, IOException, InterruptedException {
try (resultSet) {
try (preparedStatement; resultSet) {
// close only
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void empty_statement() throws Exception {
}

@Test
void generic_staement_wo_result() throws Exception {
void generic_statement_wo_result() throws Exception {
var reached = new AtomicBoolean();
MockSqlProcessor sql = new MockSqlProcessor(true) {
@Override
Expand All @@ -195,7 +195,7 @@ public Map<CounterType, Long> getCounters() {
}

@Test
void generic_staement_w_result() throws Exception {
void generic_statement_w_result() throws Exception {
var reachedExec = new AtomicBoolean();
MockSqlProcessor sql = new MockSqlProcessor(true) {
@Override
Expand All @@ -205,7 +205,7 @@ public PreparedStatementResult execute(String statement, Region region) {
}
assertEquals("SELECT * FROM T", statement);
var rs = Relation.of(new Object[][] { { 1 } }).getResultSet(new ResultSetMetadataAdapter(SqlResponse.ResultSetMetadata.newBuilder().addColumns(Types.column(int.class)).build()));
return new PreparedStatementResult(rs);
return new PreparedStatementResult(rs, null);
}
};
var reachedRs = new AtomicBoolean();
Expand All @@ -231,7 +231,7 @@ public long process(ResultSet target) throws ServerException, IOException, Inter
}

@Test
void call_staement_fall_through() throws Exception {
void call_statement_fall_through() throws Exception {
var reached = new AtomicBoolean();
MockSqlProcessor sql = new MockSqlProcessor(true) {
@Override
Expand All @@ -257,7 +257,7 @@ public Map<CounterType, Long> getCounters() {
}

@Test
void generic_staement_inactive_tx() throws Exception {
void generic_statement_inactive_tx() throws Exception {
MockSqlProcessor sql = new MockSqlProcessor(false);
MockResultProcessor rs = new MockResultProcessor();
var engine = newBasicEngine(sql, rs);
Expand Down

0 comments on commit f1e91cb

Please sign in to comment.