ACTIVE RECORD -
TRANSACTIONS
ZVI EPSTEIN
WHAT ISTRANSACTION ?
Rails transactions are a way to ensure
that a set of database operations will only
occur if all of them succeed. Otherwise,
they will rollback to the previous state of
data.
WHAT SHOULD WE USETRANSACTION?
An operation that affects more than a single database
row.
For example: Deposit, withdrawl from User, and create
Transfer log after.
Note that ActiveRecord::Base#save automatically opens
a transaction, so changes you make in callbacks, nested
attributes processing etc. will automatically run inside a
transaction.
CLASSIC EXAMPLE
The classic example is a transfer between two accounts
where you can only have a deposit if the withdrawal
succeeded and vice versa.
HOW DOES IT WORK?
Rails will open a transaction in the database engine, then start
executing the block.There are three possibilities:
1. If no exceptions occur during the block then Rails closes the
transaction and the database commits the data
2. If there is an exception, Rails will tell the database to cancel the
transaction and no data is changed
3. If the entire Rails process or server dies then the transaction
will timeout and be cancelled by the database
ROLLBACK
 ROLLBACK returns the database to the state before the
transaction began.
 Transactions will only rollback the transaction if an error is
raised.
 The bang modifier (!) is a Rails convention for a method which
will throw an exception upon failure.
RAISING ROLLBACK
It is also possible to invalidate a transaction without raising
an exception that will propagate upwards by
using ActiveRecord::Rollback. This special exception allows
you to invalidate a transaction and reset the database
records without needing to rescue elsewhere in your code.
CALLBACKS
after_commit
This callback fires
when the transaction
succeeds.
after_rollback
This callback fires
when the transaction
succeeds.
CLASS & INSTANCE METHODS
Class method
Instance method
THANKYOU!

Active record - Transactions

  • 1.
  • 2.
    WHAT ISTRANSACTION ? Railstransactions are a way to ensure that a set of database operations will only occur if all of them succeed. Otherwise, they will rollback to the previous state of data.
  • 3.
    WHAT SHOULD WEUSETRANSACTION? An operation that affects more than a single database row. For example: Deposit, withdrawl from User, and create Transfer log after. Note that ActiveRecord::Base#save automatically opens a transaction, so changes you make in callbacks, nested attributes processing etc. will automatically run inside a transaction.
  • 4.
    CLASSIC EXAMPLE The classicexample is a transfer between two accounts where you can only have a deposit if the withdrawal succeeded and vice versa.
  • 5.
    HOW DOES ITWORK? Rails will open a transaction in the database engine, then start executing the block.There are three possibilities: 1. If no exceptions occur during the block then Rails closes the transaction and the database commits the data 2. If there is an exception, Rails will tell the database to cancel the transaction and no data is changed 3. If the entire Rails process or server dies then the transaction will timeout and be cancelled by the database
  • 6.
    ROLLBACK  ROLLBACK returnsthe database to the state before the transaction began.  Transactions will only rollback the transaction if an error is raised.  The bang modifier (!) is a Rails convention for a method which will throw an exception upon failure.
  • 7.
    RAISING ROLLBACK It isalso possible to invalidate a transaction without raising an exception that will propagate upwards by using ActiveRecord::Rollback. This special exception allows you to invalidate a transaction and reset the database records without needing to rescue elsewhere in your code.
  • 8.
    CALLBACKS after_commit This callback fires whenthe transaction succeeds. after_rollback This callback fires when the transaction succeeds.
  • 9.
    CLASS & INSTANCEMETHODS Class method Instance method
  • 10.