Search in sources :

Example 6 with JdbcAbortedException

use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException in project java-spanner-jdbc by googleapis.

the class JdbcAbortedTransactionTest method testTransactionalUpdateAborted.

@Test
public void testTransactionalUpdateAborted() throws SQLException {
    // off.
    try (java.sql.Connection connection = createConnection()) {
        connection.setAutoCommit(false);
        mockSpanner.abortNextStatement();
        int updateCount = connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql());
        if (!retryAbortsInternally) {
            fail("missing expected exception");
        }
        assertThat(updateCount).isEqualTo(UPDATE_COUNT);
        assertThat(getRetryCount(connection)).isEqualTo(1);
    } catch (JdbcAbortedException e) {
        assertThat(retryAbortsInternally).isFalse();
    }
}
Also used : Connection(java.sql.Connection) JdbcAbortedException(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException) Test(org.junit.Test)

Example 7 with JdbcAbortedException

use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException in project java-spanner-jdbc by googleapis.

the class JdbcAbortedTransactionTest method testTransactionalUpdateWithErrorOnOriginalAndNotOnRetry.

@Test
public void testTransactionalUpdateWithErrorOnOriginalAndNotOnRetry() throws SQLException {
    final String sql = "UPDATE SOMETHING SET OTHER=1";
    mockSpanner.putStatementResult(StatementResult.exception(Statement.of(sql), Status.INVALID_ARGUMENT.withDescription("test").asRuntimeException()));
    try (java.sql.Connection connection = createConnection()) {
        connection.setAutoCommit(false);
        try (ResultSet rs = connection.createStatement().executeQuery(SELECT1.getSql())) {
            while (rs.next()) {
                assertThat(rs.getLong(1)).isEqualTo(1L);
            }
        }
        try {
            connection.createStatement().executeUpdate(sql);
            fail("missing 'test' exception");
        } catch (SQLException e) {
        // ignore
        }
        // Set the update statement to return a result next time (i.e. during retry).
        mockSpanner.putStatementResult(StatementResult.update(Statement.of(sql), 1L));
        mockSpanner.abortNextStatement();
        connection.commit();
        fail("missing expected aborted exception");
    } catch (JdbcAbortedDueToConcurrentModificationException e) {
        assertThat(retryAbortsInternally).isTrue();
        assertThat(e.getDatabaseErrorDuringRetry()).isNull();
    } catch (JdbcAbortedException e) {
        assertThat(retryAbortsInternally).isFalse();
    }
}
Also used : Connection(java.sql.Connection) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) JdbcAbortedDueToConcurrentModificationException(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedDueToConcurrentModificationException) JdbcAbortedException(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException) Test(org.junit.Test)

Example 8 with JdbcAbortedException

use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException in project java-spanner-jdbc by googleapis.

the class JdbcAbortedTransactionTest method testTransactionalSelectAborted.

@Test
public void testTransactionalSelectAborted() throws SQLException {
    try (java.sql.Connection connection = createConnection()) {
        connection.setAutoCommit(false);
        mockSpanner.abortNextStatement();
        try (ResultSet rs = connection.createStatement().executeQuery(SELECT1.getSql())) {
            while (rs.next()) {
                if (!retryAbortsInternally) {
                    fail("missing expected exception");
                }
                assertThat(rs.getLong(1)).isEqualTo(1L);
            }
        }
        assertThat(getRetryCount(connection)).isEqualTo(1);
    } catch (JdbcAbortedException e) {
        assertThat(retryAbortsInternally).isFalse();
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) JdbcAbortedException(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException) Test(org.junit.Test)

Aggregations

JdbcAbortedException (com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedException)8 Connection (java.sql.Connection)8 Test (org.junit.Test)7 ResultSet (java.sql.ResultSet)4 JdbcAbortedDueToConcurrentModificationException (com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedDueToConcurrentModificationException)3 SQLException (java.sql.SQLException)2 Mutation (com.google.cloud.spanner.Mutation)1 RandomResultSetGenerator (com.google.cloud.spanner.connection.RandomResultSetGenerator)1 CloudSpannerJdbcConnection (com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection)1 BigDecimal (java.math.BigDecimal)1