Search in sources :

Example 6 with TransactionException

use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.

the class JdbcTransactionManager method start.

@Override
public JdbcTransaction start(String txId) throws TransactionException {
    try {
        JdbcTransaction transaction = new JdbcTransaction(txId, jdbcService, dataSource.getConnection(), rdbEngine);
        getNamespace().ifPresent(transaction::withNamespace);
        getTable().ifPresent(transaction::withTable);
        return transaction;
    } catch (SQLException e) {
        throw new TransactionException("failed to start the transaction", e);
    }
}
Also used : TransactionException(com.scalar.db.exception.transaction.TransactionException) SQLException(java.sql.SQLException)

Example 7 with TransactionException

use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.

the class DistributedTransactionIntegrationTestBase method populateRecords.

private void populateRecords() throws TransactionException {
    DistributedTransaction transaction = manager.start();
    IntStream.range(0, NUM_ACCOUNTS).forEach(i -> IntStream.range(0, NUM_TYPES).forEach(j -> {
        Key partitionKey = new Key(ACCOUNT_ID, i);
        Key clusteringKey = new Key(ACCOUNT_TYPE, j);
        Put put = new Put(partitionKey, clusteringKey).forNamespace(namespace).forTable(TABLE).withIntValue(BALANCE, INITIAL_BALANCE).withIntValue(SOME_COLUMN, i * j);
        try {
            transaction.put(put);
        } catch (CrudException e) {
            throw new RuntimeException(e);
        }
    }));
    transaction.commit();
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) IntColumn(com.scalar.db.io.IntColumn) IntValue(com.scalar.db.io.IntValue) Arrays(java.util.Arrays) TestUtils.assertResultsAreASubsetOf(com.scalar.db.util.TestUtils.assertResultsAreASubsetOf) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExecutionException(com.scalar.db.exception.storage.ExecutionException) Value(com.scalar.db.io.Value) TestUtils(com.scalar.db.util.TestUtils) CrudException(com.scalar.db.exception.transaction.CrudException) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ImmutableList(com.google.common.collect.ImmutableList) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) CommitException(com.scalar.db.exception.transaction.CommitException) DataType(com.scalar.db.io.DataType) CommitConflictException(com.scalar.db.exception.transaction.CommitConflictException) Key(com.scalar.db.io.Key) Properties(java.util.Properties) ExpectedResultBuilder(com.scalar.db.util.TestUtils.ExpectedResult.ExpectedResultBuilder) TestUtils.assertResultsContainsExactlyInAnyOrder(com.scalar.db.util.TestUtils.assertResultsContainsExactlyInAnyOrder) TransactionFactory(com.scalar.db.service.TransactionFactory) Test(org.junit.jupiter.api.Test) List(java.util.List) ExpectedResult(com.scalar.db.util.TestUtils.ExpectedResult) Ordering(com.scalar.db.api.Scan.Ordering) Optional(java.util.Optional) TransactionException(com.scalar.db.exception.transaction.TransactionException) Collections(java.util.Collections) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Key(com.scalar.db.io.Key) CrudException(com.scalar.db.exception.transaction.CrudException)

Example 8 with TransactionException

use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.

the class ElectronicMoneyWithTransaction method pay.

@Override
public void pay(String fromId, String toId, int amount) throws TransactionException {
    // Start a transaction
    DistributedTransaction tx = manager.start();
    try {
        // Retrieve the current balances for ids
        Get fromGet = new Get(new Key(ID, fromId));
        Get toGet = new Get(new Key(ID, toId));
        Optional<Result> fromResult = tx.get(fromGet);
        Optional<Result> toResult = tx.get(toGet);
        // Calculate the balances (it assumes that both accounts exist)
        int newFromBalance = fromResult.get().getValue(BALANCE).get().getAsInt() - amount;
        int newToBalance = toResult.get().getValue(BALANCE).get().getAsInt() + amount;
        if (newFromBalance < 0) {
            throw new RuntimeException(fromId + " doesn't have enough balance.");
        }
        // Update the balances
        Put fromPut = new Put(new Key(ID, fromId)).withValue(BALANCE, newFromBalance);
        Put toPut = new Put(new Key(ID, toId)).withValue(BALANCE, newToBalance);
        tx.put(fromPut);
        tx.put(toPut);
        // Commit the transaction (records are automatically recovered in case of failure)
        tx.commit();
    } catch (Exception e) {
        tx.abort();
        throw e;
    }
}
Also used : DistributedTransaction(com.scalar.db.api.DistributedTransaction) Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) TransactionException(com.scalar.db.exception.transaction.TransactionException) IOException(java.io.IOException) Result(com.scalar.db.api.Result)

Example 9 with TransactionException

use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.

the class GrpcTransactionOnBidirectionalStream method throwIfErrorForStart.

private void throwIfErrorForStart(ResponseOrError responseOrError) throws TransactionException {
    if (responseOrError.isError()) {
        finished.set(true);
        Throwable error = responseOrError.getError();
        if (error instanceof StatusRuntimeException) {
            StatusRuntimeException e = (StatusRuntimeException) error;
            if (e.getStatus().getCode() == Code.INVALID_ARGUMENT) {
                throw new IllegalArgumentException(e.getMessage(), e);
            }
            if (e.getStatus().getCode() == Code.UNAVAILABLE) {
                throw new ServiceTemporaryUnavailableException(e.getMessage(), e);
            }
        }
        if (error instanceof Error) {
            throw (Error) error;
        }
        throw new TransactionException("failed to start", error);
    }
}
Also used : TransactionException(com.scalar.db.exception.transaction.TransactionException) ServiceTemporaryUnavailableException(com.scalar.db.util.retry.ServiceTemporaryUnavailableException) StatusRuntimeException(io.grpc.StatusRuntimeException)

Example 10 with TransactionException

use of com.scalar.db.exception.transaction.TransactionException in project scalardb by scalar-labs.

the class GrpcTwoPhaseCommitTransactionOnBidirectionalStream method throwIfErrorForStartOrJoin.

private void throwIfErrorForStartOrJoin(ResponseOrError responseOrError, boolean start) throws TransactionException {
    if (responseOrError.isError()) {
        finished.set(true);
        Throwable error = responseOrError.getError();
        if (error instanceof StatusRuntimeException) {
            StatusRuntimeException e = (StatusRuntimeException) error;
            if (e.getStatus().getCode() == Code.INVALID_ARGUMENT) {
                throw new IllegalArgumentException(e.getMessage(), e);
            }
            if (e.getStatus().getCode() == Code.UNAVAILABLE) {
                throw new ServiceTemporaryUnavailableException(e.getMessage(), e);
            }
        }
        if (error instanceof Error) {
            throw (Error) error;
        }
        throw new TransactionException("failed to " + (start ? "start" : "join"), error);
    }
}
Also used : TransactionException(com.scalar.db.exception.transaction.TransactionException) ServiceTemporaryUnavailableException(com.scalar.db.util.retry.ServiceTemporaryUnavailableException) StatusRuntimeException(io.grpc.StatusRuntimeException)

Aggregations

TransactionException (com.scalar.db.exception.transaction.TransactionException)12 Key (com.scalar.db.io.Key)9 ExecutionException (com.scalar.db.exception.storage.ExecutionException)7 DataType (com.scalar.db.io.DataType)7 IntValue (com.scalar.db.io.IntValue)7 Value (com.scalar.db.io.Value)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Optional (java.util.Optional)7 IntStream (java.util.stream.IntStream)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 CrudException (com.scalar.db.exception.transaction.CrudException)6 Get (com.scalar.db.api.Get)5 Put (com.scalar.db.api.Put)5 Result (com.scalar.db.api.Result)5 CommitException (com.scalar.db.exception.transaction.CommitException)5 Arrays (java.util.Arrays)5 Assertions.assertThatCode (org.assertj.core.api.Assertions.assertThatCode)5 ImmutableList (com.google.common.collect.ImmutableList)4 Ordering (com.scalar.db.api.Scan.Ordering)4