Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,24 @@ ExecuteResult prepareAndExecuteInternal(
final PrepareCallback callback)
throws SQLException {

return ArrowFlightPreparedStatement.builder(getConnection())
.withQuery(query)
.withExistingStatement(this)
.build()
.prepareAndExecuteInternal(callback);
final ArrowFlightPreparedStatement replacement =
ArrowFlightPreparedStatement.builder(getConnection())
.withQuery(query)
.withExistingStatement(this)
.build();
try {
closePreparedResources();
} catch (final RuntimeException closeFailure) {
try {
replacement.closePreparedResources();
} catch (final RuntimeException replacementCloseFailure) {
if (replacementCloseFailure != closeFailure) {
closeFailure.addSuppressed(replacementCloseFailure);
}
}
throw closeFailure;
}
return replacement.prepareAndExecuteInternal(callback);
}

Schema getDataSetSchema() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,64 @@ public void testExecuteShouldRunSelectQuery() throws SQLException {
is(allOf(equalTo(statement.getLargeUpdateCount()), equalTo(-1L))));
}

@Test
public void testExecuteMultipleTimesClosesAllPreparedStatements() throws SQLException {
PRODUCER.clearActionTypeCounter();

assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));
assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));

assertThat(
PRODUCER
.getActionTypeCounter()
.getOrDefault(FlightSqlUtils.FLIGHT_SQL_CREATE_PREPARED_STATEMENT.getType(), 0),
is(2));
assertThat(
PRODUCER
.getActionTypeCounter()
.getOrDefault(FlightSqlUtils.FLIGHT_SQL_CLOSE_PREPARED_STATEMENT.getType(), 0),
is(1));

statement.close();

assertThat(
PRODUCER
.getActionTypeCounter()
.getOrDefault(FlightSqlUtils.FLIGHT_SQL_CLOSE_PREPARED_STATEMENT.getType(), 0),
is(2));
}

@Test
public void testExecuteAttemptsToCloseReplacementWhenPreviousCloseFails() throws SQLException {
PRODUCER.clearActionTypeCounter();
assertThat(statement.execute(SAMPLE_QUERY_CMD), is(true));
PRODUCER.setClosePreparedStatementFailure(
CallStatus.INTERNAL.withDescription("Prepared statement close failed"));

try {
final SQLException exception =
assertThrows(SQLException.class, () -> statement.execute(SAMPLE_QUERY_CMD));

assertThat(exception.getCause(), instanceOf(FlightRuntimeException.class));
final FlightRuntimeException closeFailure = (FlightRuntimeException) exception.getCause();
assertThat(
closeFailure.status().description(), containsString("Prepared statement close failed"));
assertThat(closeFailure.getSuppressed().length, is(1));
assertThat(
PRODUCER
.getActionTypeCounter()
.getOrDefault(FlightSqlUtils.FLIGHT_SQL_CREATE_PREPARED_STATEMENT.getType(), 0),
is(2));
assertThat(
PRODUCER
.getActionTypeCounter()
.getOrDefault(FlightSqlUtils.FLIGHT_SQL_CLOSE_PREPARED_STATEMENT.getType(), 0),
is(2));
} finally {
PRODUCER.setClosePreparedStatementFailure(null);
}
}

@Test
public void testExecutePrepareFailurePreservesServerError() {
final SQLException exception =
Expand Down
Loading