Skip to main content

Transactions

Tigris provides global, ACID transactions with strict serializability using optimistic concurrency control. The transactions allow multiple clients to concurrently read and write data in the database with strong consistency guarantees.

Transactions in Tigris work across collections and documents without any restrictions. Unlike some other document databases, there are no confusing read or write concerns to configure, and no cross-shard caveats.

TigrisDatabase db = client.getDatabase(dbName);
db.transact(
tx -> {
try {
// retrieve transaction aware collection
TigrisCollection<User> userCollection = db.getCollection(User.class);
User emma = userCollection.readOne(Filters.eq("id", emma.getId()));
User lucy = userCollection.readOne(Filters.eq("id", lucy.getId()));

// reduce emma's balance by 10
userCollection.update(
Filters.eq("id", emma.getId()),
UpdateFields.newBuilder().set("balance", emma.getBalance() - 10).build());

// increment lucy's balance by 10
userCollection.update(
Filters.eq("id", lucy.getId()),
UpdateFields.newBuilder().set("balance", lucy.getBalance() + 10).build());
} catch (Exception ex) {
// throw it to rollback ongoing transaction
throw new IllegalStateException(ex);
}
}
);