# Postgresql transactions

**URL:** <https://www.ruby-forum.com/t/postgresql-transactions/85244>\
**Category:** Rails\
**Created:** [February 11, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/postgresql-transactions/85244 "2007-02-11T22:57:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [February 11, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/postgresql-transactions/85244/1 "2007-02-11T22:57:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [February 14, 2007, 2:38pm UTC](https://www.ruby-forum.com/t/postgresql-transactions/85244/2 "2007-02-14T14:38:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![snacktime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@snacktime](https://www.ruby-forum.com/u/snacktime)\
**Post date:** [February 14, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/postgresql-transactions/85244/3 "2007-02-14T15:07:31Z")

</div>

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
