Search in sources :

Example 1 with DistributedTransaction

use of com.scalar.db.api.DistributedTransaction in project scalardb by scalar-labs.

the class ElectronicMoneyWithTransaction method charge.

@Override
public void charge(String id, int amount) throws TransactionException {
    // Start a transaction
    DistributedTransaction tx = manager.start();
    try {
        // Retrieve the current balance for id
        Get get = new Get(new Key(ID, id));
        Optional<Result> result = tx.get(get);
        // Calculate the balance
        int balance = amount;
        if (result.isPresent()) {
            int current = result.get().getValue(BALANCE).get().getAsInt();
            balance += current;
        }
        // Update the balance
        Put put = new Put(new Key(ID, id)).withValue(BALANCE, balance);
        tx.put(put);
        // 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 2 with DistributedTransaction

use of com.scalar.db.api.DistributedTransaction 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)

Aggregations

DistributedTransaction (com.scalar.db.api.DistributedTransaction)2 Get (com.scalar.db.api.Get)2 Put (com.scalar.db.api.Put)2 Result (com.scalar.db.api.Result)2 TransactionException (com.scalar.db.exception.transaction.TransactionException)2 Key (com.scalar.db.io.Key)2 IOException (java.io.IOException)2