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);
}
}
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();
}
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;
}
}
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);
}
}
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);
}
}
Aggregations