Search in sources :

Example 6 with NoMutationException

use of com.scalar.db.exception.storage.NoMutationException in project scalardb by scalar-labs.

the class JdbcDatabase method mutate.

@Override
public void mutate(List<? extends Mutation> mutations) throws ExecutionException {
    if (mutations.size() == 1) {
        Mutation mutation = mutations.get(0);
        if (mutation instanceof Put) {
            put((Put) mutation);
        } else if (mutation instanceof Delete) {
            delete((Delete) mutation);
        }
        return;
    }
    mutations = copyAndSetTargetToIfNot(mutations);
    Connection connection = null;
    try {
        connection = dataSource.getConnection();
        connection.setAutoCommit(false);
    } catch (SQLException e) {
        close(connection);
        throw new ExecutionException("mutate operation failed", e);
    }
    try {
        if (!jdbcService.mutate(mutations, connection)) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                throw new ExecutionException("failed to rollback", e);
            }
            throw new NoMutationException("no mutation was applied");
        } else {
            connection.commit();
        }
    } catch (SQLException e) {
        try {
            connection.rollback();
        } catch (SQLException sqlException) {
            throw new ExecutionException("failed to rollback", sqlException);
        }
        if (JdbcUtils.isConflictError(e, rdbEngine)) {
            // conflicts can happen. Throw RetriableExecutionException in that case.
            throw new RetriableExecutionException("conflict happened in a mutate operation", e);
        }
        throw new ExecutionException("mutate operation failed", e);
    } finally {
        close(connection);
    }
}
Also used : Delete(com.scalar.db.api.Delete) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Mutation(com.scalar.db.api.Mutation) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) NoMutationException(com.scalar.db.exception.storage.NoMutationException) Put(com.scalar.db.api.Put)

Example 7 with NoMutationException

use of com.scalar.db.exception.storage.NoMutationException in project scalardb by scalar-labs.

the class MutateStatementHandler method throwException.

private void throwException(CosmosException exception) throws ExecutionException {
    LOGGER.error(exception.getMessage());
    int statusCode = exception.getSubStatusCode();
    if (statusCode == CosmosErrorCode.PRECONDITION_FAILED.get()) {
        throw new NoMutationException("no mutation was applied.");
    } else if (statusCode == CosmosErrorCode.RETRY_WITH.get()) {
        throw new RetriableExecutionException(exception.getMessage(), exception);
    }
    throw new ExecutionException(exception.getMessage(), exception);
}
Also used : RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) NoMutationException(com.scalar.db.exception.storage.NoMutationException)

Example 8 with NoMutationException

use of com.scalar.db.exception.storage.NoMutationException in project scalardb by scalar-labs.

the class JdbcDatabase method delete.

@Override
public void delete(Delete delete) throws ExecutionException {
    delete = copyAndSetTargetToIfNot(delete);
    Connection connection = null;
    try {
        connection = dataSource.getConnection();
        if (!jdbcService.delete(delete, connection)) {
            throw new NoMutationException("no mutation was applied");
        }
    } catch (SQLException e) {
        throw new ExecutionException("delete operation failed", e);
    } finally {
        close(connection);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) NoMutationException(com.scalar.db.exception.storage.NoMutationException)

Example 9 with NoMutationException

use of com.scalar.db.exception.storage.NoMutationException in project scalardb by scalar-labs.

the class DeleteStatementHandler method handle.

@Nonnull
@Override
public List<Map<String, AttributeValue>> handle(Operation operation) throws ExecutionException {
    checkArgument(operation, Delete.class);
    Delete delete = (Delete) operation;
    TableMetadata tableMetadata = metadataManager.getTableMetadata(operation);
    try {
        delete(delete, tableMetadata);
    } catch (ConditionalCheckFailedException e) {
        throw new NoMutationException("no mutation was applied.", e);
    } catch (TransactionConflictException e) {
        throw new RetriableExecutionException(e.getMessage(), e);
    } catch (DynamoDbException e) {
        throw new ExecutionException(e.getMessage(), e);
    }
    return Collections.emptyList();
}
Also used : Delete(com.scalar.db.api.Delete) TableMetadata(com.scalar.db.api.TableMetadata) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) TransactionConflictException(software.amazon.awssdk.services.dynamodb.model.TransactionConflictException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) ConditionalCheckFailedException(software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException) NoMutationException(com.scalar.db.exception.storage.NoMutationException) Nonnull(javax.annotation.Nonnull)

Aggregations

ExecutionException (com.scalar.db.exception.storage.ExecutionException)9 NoMutationException (com.scalar.db.exception.storage.NoMutationException)9 RetriableExecutionException (com.scalar.db.exception.storage.RetriableExecutionException)9 TableMetadata (com.scalar.db.api.TableMetadata)3 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 Nonnull (javax.annotation.Nonnull)3 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)3 Delete (com.scalar.db.api.Delete)2 Mutation (com.scalar.db.api.Mutation)2 Put (com.scalar.db.api.Put)2 ConditionalCheckFailedException (software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException)2 TransactionConflictException (software.amazon.awssdk.services.dynamodb.model.TransactionConflictException)2 ResultSet (com.datastax.driver.core.ResultSet)1 WriteTimeoutException (com.datastax.driver.core.exceptions.WriteTimeoutException)1 ArrayList (java.util.ArrayList)1 CancellationReason (software.amazon.awssdk.services.dynamodb.model.CancellationReason)1 TransactWriteItem (software.amazon.awssdk.services.dynamodb.model.TransactWriteItem)1 TransactWriteItemsRequest (software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest)1 TransactionCanceledException (software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException)1