On Mon, Aug 9, 2010 at 11:27 PM, Michael Bayer <[email protected]> wrote:
>
> On Aug 10, 2010, at 12:03 AM, Jon Nelson wrote:
>
>> I tried setting isolation_level to SERIALIZABLE in my create_engine
>> options, while using psycopg2.
>> However, an strace clearly shows that it is using READ COMMITTED.
>> Is setting the isolation_level not supported with psycopg2?
>
>
> Here's a test:
>
> eng = create_engine('postgresql://....', isolation_level='SERIALIZABLE', 
> echo=True)
> print eng.execute('show transaction isolation level').scalar()
>
> for me it returns 'serializable'.

Aha, but in psycopg2, transactions ALWAYS get a new transaction level
(set on the psycopg2 connection object with set_isolation_level).  Add
to the above:


conn = eng.connect()
t = conn.begin()
print conn.execute('show transaction isolation level').scalar()

...

Mine shows:

read committed

And, of course, strace doesn't lie (much).

I have a patch! It's probably wrong, but it works (there should
probably be more checking on the actual *value* before we go about
using getattr, but here it is):


diff -r 753e46f6868c lib/sqlalchemy/dialects/postgresql/psycopg2.py
--- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py    Mon Aug 09
20:32:37 2010 -0400
+++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py    Mon Aug 09
23:37:10 2010 -0500
@@ -207,6 +207,12 @@
         psycopg = __import__('psycopg2')
         return psycopg

+    def do_begin(self, conn):
+        if self.isolation_level:
+            extensions = __import__('psycopg2.extensions').extensions
+            level = getattr(extensions, 'ISOLATION_LEVEL_' +
self.isolation_level.upper())
+            conn.set_isolation_level(level)
+
     def on_connect(self):
         base_on_connect = super(PGDialect_psycopg2, self).on_connect()
         if self.dbapi and self.use_native_unicode:


-- 
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to