use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.
the class AbortedTest method testAbortedDuringRetryOfFailedBatchUpdateAsFirstStatement.
@Test
public void testAbortedDuringRetryOfFailedBatchUpdateAsFirstStatement() {
final Statement invalidStatement = Statement.of("INSERT INTO FOO");
StatusRuntimeException notFound = Status.NOT_FOUND.withDescription("Table not found").asRuntimeException();
mockSpanner.putStatementResult(StatementResult.exception(invalidStatement, notFound));
try (ITConnection connection = createConnection(createAbortFirstRetryListener(invalidStatement, notFound))) {
try {
connection.executeBatchUpdate(Collections.singletonList(invalidStatement));
fail("missing expected exception");
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
}
connection.execute(INSERT_STATEMENT);
// Force an abort and retry.
mockSpanner.abortNextStatement();
connection.commit();
}
assertThat(mockSpanner.countRequestsOfType(CommitRequest.class)).isEqualTo(2);
assertThat(mockSpanner.countRequestsOfType(ExecuteBatchDmlRequest.class)).isEqualTo(6);
}
use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.
the class AbortedTest method testCommitAborted.
@Test
public void testCommitAborted() {
// transaction is the most recent transaction of that session.
for (int i = 0; i < 2; i++) {
mockSpanner.putStatementResult(StatementResult.query(SELECT_COUNT_STATEMENT, SELECT_COUNT_RESULTSET_BEFORE_INSERT));
mockSpanner.putStatementResult(StatementResult.update(INSERT_STATEMENT, UPDATE_COUNT));
AbortInterceptor interceptor = new AbortInterceptor(0);
try (ITConnection connection = createConnection(interceptor, new CountTransactionRetryListener())) {
// verify that the there is no test record
try (ResultSet rs = connection.executeQuery(Statement.of("SELECT COUNT(*) AS C FROM TEST WHERE ID=1"))) {
assertThat(rs.next(), is(true));
assertThat(rs.getLong("C"), is(equalTo(0L)));
assertThat(rs.next(), is(false));
}
// do an insert
connection.executeUpdate(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'test aborted')"));
// indicate that the next statement should abort
interceptor.setProbability(1.0);
interceptor.setOnlyInjectOnce(true);
// do a commit that will first abort, and then on retry will succeed
connection.commit();
mockSpanner.putStatementResult(StatementResult.query(SELECT_COUNT_STATEMENT, SELECT_COUNT_RESULTSET_AFTER_INSERT));
// verify that the insert succeeded
try (ResultSet rs = connection.executeQuery(Statement.of("SELECT COUNT(*) AS C FROM TEST WHERE ID=1"))) {
assertThat(rs.next(), is(true));
assertThat(rs.getLong("C"), is(equalTo(1L)));
assertThat(rs.next(), is(false));
}
}
}
}
use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.
the class AbortedTest method testAbortedDuringRetryOfFailedUpdate.
@Test
public void testAbortedDuringRetryOfFailedUpdate() {
final Statement invalidStatement = Statement.of("INSERT INTO FOO");
StatusRuntimeException notFound = Status.NOT_FOUND.withDescription("Table not found").asRuntimeException();
mockSpanner.putStatementResult(StatementResult.exception(invalidStatement, notFound));
try (ITConnection connection = createConnection(createAbortFirstRetryListener(invalidStatement, notFound))) {
connection.execute(INSERT_STATEMENT);
try {
connection.execute(invalidStatement);
fail("missing expected exception");
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
}
// Force an abort and retry.
mockSpanner.abortNextStatement();
connection.commit();
}
assertThat(mockSpanner.countRequestsOfType(CommitRequest.class)).isEqualTo(2);
// The transaction will be executed 3 times, which means that there will be 6
// ExecuteSqlRequests:
// 1. The initial attempt.
// 2. The first retry attempt. This will fail on the invalid statement as it is aborted.
// 3. the second retry attempt. This will succeed.
assertThat(mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)).isEqualTo(6);
}
use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.
the class AbortedTest method testAbortedDuringRetryOfFailedBatchUpdate.
@Test
public void testAbortedDuringRetryOfFailedBatchUpdate() {
final Statement invalidStatement = Statement.of("INSERT INTO FOO");
StatusRuntimeException notFound = Status.NOT_FOUND.withDescription("Table not found").asRuntimeException();
mockSpanner.putStatementResult(StatementResult.exception(invalidStatement, notFound));
try (ITConnection connection = createConnection(createAbortFirstRetryListener(invalidStatement, notFound))) {
connection.execute(INSERT_STATEMENT);
try {
connection.executeBatchUpdate(Collections.singletonList(invalidStatement));
fail("missing expected exception");
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
}
// Force an abort and retry.
mockSpanner.abortNextStatement();
connection.commit();
}
assertThat(mockSpanner.countRequestsOfType(CommitRequest.class)).isEqualTo(2);
assertThat(mockSpanner.countRequestsOfType(ExecuteBatchDmlRequest.class)).isEqualTo(3);
}
use of com.google.cloud.spanner.connection.ITAbstractSpannerTest.ITConnection in project java-spanner by googleapis.
the class ConnectionAsyncApiAbortedTest method createConnection.
ITConnection createConnection(TransactionRetryListener listener) {
ITConnection connection = super.createConnection(ImmutableList.of(), ImmutableList.of(listener));
connection.setAutocommit(false);
return connection;
}
Aggregations