Sava Chankov wrote the following on 14.02.2007 14:37 :> snacktime wrote:
>
>> Anyone found an elegant way to do the following in rails with
postgresql?
>> 1. Two phase commit
>> 2. Setting isolation level on transactions
>>
>
> No, ActiveRecord does not support any of them. But if you need them often
you
> can write a plugin (and share it, so others that need this functionality
will be
> grateful).
>
>
And this is quite easy to do, here''s a quick hack for serializable
connections (not tested with actual *objects) I use extensively for
account credit management in one of my projects.
lib/serialized_transactions.rb (to include in environment.rb or put in a
plugin)
# This library extends the ActiveRecord::Base class
# to provide transactions with SERIALIZABLE isolation
# level
class ActiveRecord::Base
# This is a transaction in serialized mode
def self.serialized_transaction(*objects, &block)
serial_sql = ''SET TRANSACTION ISOLATION LEVEL
SERIALIZABLE''
# pass a modified block to the original transaction method
self.transaction(*objects) {
# Serialize on this connection
connection.execute(serial_sql)
objects.each { |o|
# Serialize on each object connection if any
o.connection.execute(serial_sql)
}
block.call
}
end
end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---