Postgresql transactions

Anyone found an elegant way to do the following in rails with
postgresql? I can grab the underlying connection handle and use it
directly, but I’d rather find a better way.

  1. Two phase commit
  2. Setting isolation level on transactions

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).


Sava C.

Sava C. wrote the following on 14.02.2007 14:37 :

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs