2004-11-23 20:04:44 -05:00
|
|
|
require 'thread'
|
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
module Transactions # :nodoc:
|
2005-04-10 13:34:29 -04:00
|
|
|
class TransactionError < ActiveRecordError # :nodoc:
|
|
|
|
end
|
|
|
|
|
2006-04-29 14:10:14 -04:00
|
|
|
def self.included(base)
|
2004-11-23 20:04:44 -05:00
|
|
|
base.extend(ClassMethods)
|
|
|
|
|
|
|
|
base.class_eval do
|
2006-10-10 15:34:25 -04:00
|
|
|
[:destroy, :save, :save!].each do |method|
|
2006-04-29 16:20:22 -04:00
|
|
|
alias_method_chain method, :transactions
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-12-22 06:26:03 -05:00
|
|
|
# Transactions are protective blocks where SQL statements are only permanent if they can all succeed as one atomic action.
|
2005-02-07 09:15:53 -05:00
|
|
|
# The classic example is a transfer between two accounts where you can only have a deposit if the withdrawal succeeded and
|
2005-10-26 09:05:48 -04:00
|
|
|
# vice versa. Transactions enforce the integrity of the database and guard the data against program errors or database break-downs.
|
2004-11-23 20:04:44 -05:00
|
|
|
# So basically you should use transaction blocks whenever you have a number of statements that must be executed together or
|
|
|
|
# not at all. Example:
|
|
|
|
#
|
|
|
|
# transaction do
|
|
|
|
# david.withdrawal(100)
|
|
|
|
# mary.deposit(100)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This example will only take money from David and give to Mary if neither +withdrawal+ nor +deposit+ raises an exception.
|
|
|
|
# Exceptions will force a ROLLBACK that returns the database to the state before the transaction was begun. Be aware, though,
|
2008-04-04 23:52:58 -04:00
|
|
|
# that the objects will _not_ have their instance data returned to their pre-transactional state.
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# == Different Active Record classes in a single transaction
|
2007-11-06 14:04:54 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# Though the transaction class method is called on some Active Record class,
|
2007-11-06 14:04:54 -05:00
|
|
|
# the objects within the transaction block need not all be instances of
|
|
|
|
# that class.
|
|
|
|
# In this example a <tt>Balance</tt> record is transactionally saved even
|
|
|
|
# though <tt>transaction</tt> is called on the <tt>Account</tt> class:
|
|
|
|
#
|
|
|
|
# Account.transaction do
|
2007-11-06 14:11:42 -05:00
|
|
|
# balance.save!
|
|
|
|
# account.save!
|
2007-11-06 14:04:54 -05:00
|
|
|
# end
|
|
|
|
#
|
2004-11-23 20:04:44 -05:00
|
|
|
# == Transactions are not distributed across database connections
|
|
|
|
#
|
|
|
|
# A transaction acts on a single database connection. If you have
|
|
|
|
# multiple class-specific databases, the transaction will not protect
|
|
|
|
# interaction among them. One workaround is to begin a transaction
|
|
|
|
# on each class whose models you alter:
|
|
|
|
#
|
|
|
|
# Student.transaction do
|
|
|
|
# Course.transaction do
|
|
|
|
# course.enroll(student)
|
|
|
|
# student.units += course.units
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This is a poor solution, but full distributed transactions are beyond
|
|
|
|
# the scope of Active Record.
|
|
|
|
#
|
|
|
|
# == Save and destroy are automatically wrapped in a transaction
|
|
|
|
#
|
|
|
|
# Both Base#save and Base#destroy come wrapped in a transaction that ensures that whatever you do in validations or callbacks
|
|
|
|
# will happen under the protected cover of a transaction. So you can use validations to check for values that the transaction
|
2007-11-07 22:37:16 -05:00
|
|
|
# depends on or you can raise exceptions in the callbacks to rollback.
|
2004-11-23 20:04:44 -05:00
|
|
|
#
|
|
|
|
# == Exception handling
|
|
|
|
#
|
|
|
|
# Also have in mind that exceptions thrown within a transaction block will be propagated (after triggering the ROLLBACK), so you
|
2007-11-29 17:25:42 -05:00
|
|
|
# should be ready to catch those in your application code. One exception is the ActiveRecord::Rollback exception, which will
|
|
|
|
# trigger a ROLLBACK when raised, but not be re-raised by the transaction block.
|
2004-12-30 09:51:04 -05:00
|
|
|
module ClassMethods
|
2007-03-16 18:39:01 -04:00
|
|
|
def transaction(&block)
|
2006-06-19 18:48:51 -04:00
|
|
|
increment_open_transactions
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
begin
|
2007-03-16 18:39:01 -04:00
|
|
|
connection.transaction(Thread.current['start_db_transaction'], &block)
|
2004-11-23 20:04:44 -05:00
|
|
|
ensure
|
2006-06-19 18:48:51 -04:00
|
|
|
decrement_open_transactions
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
2006-06-19 18:48:51 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def increment_open_transactions #:nodoc:
|
|
|
|
open = Thread.current['open_transactions'] ||= 0
|
|
|
|
Thread.current['start_db_transaction'] = open.zero?
|
|
|
|
Thread.current['open_transactions'] = open + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def decrement_open_transactions #:nodoc:
|
|
|
|
Thread.current['open_transactions'] -= 1
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2007-03-16 18:39:01 -04:00
|
|
|
def transaction(&block)
|
|
|
|
self.class.transaction(&block)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_with_transactions #:nodoc:
|
2004-12-21 19:48:24 -05:00
|
|
|
transaction { destroy_without_transactions }
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-10-10 15:34:25 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def save_with_transactions(perform_validation = true) #:nodoc:
|
2007-01-12 00:10:06 -05:00
|
|
|
rollback_active_record_state! { transaction { save_without_transactions(perform_validation) } }
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-10-10 15:34:25 -04:00
|
|
|
|
|
|
|
def save_with_transactions! #:nodoc:
|
2007-01-12 00:10:06 -05:00
|
|
|
rollback_active_record_state! { transaction { save_without_transactions! } }
|
2007-01-02 00:36:30 -05:00
|
|
|
end
|
2007-01-12 00:10:06 -05:00
|
|
|
|
|
|
|
# Reset id and @new_record if the transaction rolls back.
|
|
|
|
def rollback_active_record_state!
|
|
|
|
id_present = has_attribute?(self.class.primary_key)
|
|
|
|
previous_id = id
|
2007-12-22 06:26:03 -05:00
|
|
|
previous_new_record = new_record?
|
2007-01-12 00:10:06 -05:00
|
|
|
yield
|
|
|
|
rescue Exception
|
|
|
|
@new_record = previous_new_record
|
|
|
|
if id_present
|
2007-01-02 00:36:30 -05:00
|
|
|
self.id = previous_id
|
2007-01-12 00:10:06 -05:00
|
|
|
else
|
|
|
|
@attributes.delete(self.class.primary_key)
|
2007-08-14 04:53:02 -04:00
|
|
|
@attributes_cache.delete(self.class.primary_key)
|
2007-12-22 06:26:03 -05:00
|
|
|
end
|
2007-01-12 00:10:06 -05:00
|
|
|
raise
|
2006-10-10 15:34:25 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2005-02-07 09:15:53 -05:00
|
|
|
end
|