Table of Contents
Lafcadio will support transactions as long as the underlying table type will support them. This is done using blocks; when the block is exited, the transaction is committed. For example, in a PayPal-like system where you're transferring money from one user to another, you want transactions to make sure all the rows are recorded or none of them are.
object_store = Lafcadio::ObjectStore.get_object_store object_store.transaction do |tr| from_account.update!( 'amount' => from_account.amount - transfer_amount ) to_account.update!( 'amount' => to_account.amount + transfer_amount ) end
Rollbacks work by calling the rollback method on the transactional object passed through the block.
object_store.transaction do |tr| from_account.update!( 'amount' => from_account.amount - transfer_amount ) tr.rollback to_account.update!( 'amount' => to_account.amount + transfer_amount ) end
In fact, raising any sort of error within the transaction will rollback the transaction.
object_store.transaction do |tr| from_account.update!( 'amount' => from_account.amount - transfer_amount ) raise "just kidding" to_account.update!( 'amount' => to_account.amount + transfer_amount ) end
Note also that the MockObjectStore handles transactions as well. This means that you can write unit-tests testing transaction-dependent logic, and test them in-memory.