Nested Transactions in Rails

In case of a single transation we can apply transation on one database. What if we need transactions on multiple databases? Rails supports nested transactions for this but before using it ,read about it carefully.

Suppose we have two databases A and B. You want to apply transactions on both the databases so that either two of these databases get updated together or nothing gets updated.

Case 1:

A.transaction do
	B.transaction do
		#some operations on A and B both
		raise ActiveRecord::Rollback if (some condition)
	end
	#other operations
end

If you rollback the transaction this way, only the operations of database B are rolled back in this case becuase you are rollbacking the transactions of inner block.Other options are executed too.

Case 2:

A.transaction do
	B.transaction do
		#some operations on A and B both
	end
	raise ActiveRecord::Rollback if (some condition)
end

If you rollback the transaction this way, only the operations of database A are rolled back because transaction block for B has alreday ended.

Case 3:

A.transaction do
#some operaitons on A
raise ActiveRecord::Rollback if (some condition)
	B.transaction do
		#some operations on A and B both
	end
end

In this case operations related to A are rolled back and system does not execute B’s transaction block.

Case 4(A valid way):

#A valid way
begin
A.transaction do
	B.transaction do
		#some operations on A and B both
		raise Exception if (some condition) #you can create your own exception for raising
	end
end
rescue Exception=>e
Rails.logger.info {"Both the transactions rollbacked."}
end

In this case both the transactions are rollbacked.

If you want to rollback both the transactions using ActiveRecord::Rollback you have to use raise ActiveRecord::Rollback in both inner and outer block.

Check out this blog post,you will find it helpful.

comments powered by Disqus