From 9c721d3090d41f25da4a93afcfe46822e0350224 Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Sat, 31 Jan 2009 19:36:18 +0100 Subject: [PATCH] [FIX] database connections aren't serialized by default lp bug: https://launchpad.net/bugs/314449 fixed bzr revid: christophe@taupe-20090131183618-pglu86hzq3ewsfxi --- bin/service/web_services.py | 12 ++++++------ bin/sql_db.py | 11 ++++++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/bin/service/web_services.py b/bin/service/web_services.py index 53936d202f5..d4b2a6e9780 100644 --- a/bin/service/web_services.py +++ b/bin/service/web_services.py @@ -69,9 +69,9 @@ class db(netsvc.Service): self.actions[id] = {'clean': False} db = sql_db.db_connect('template1') - cr = db.cursor() + cr = db.serialized_cursor() try: - cr.autocommit(True) + cr.autocommit(True) # XXX inhibit the effect of a serialized cursor. is it what we want ? cr.execute('CREATE DATABASE "%s" ENCODING \'unicode\'' % db_name) finally: cr.close() @@ -149,8 +149,8 @@ class db(netsvc.Service): logger = netsvc.Logger() db = sql_db.db_connect('template1') - cr = db.cursor() - cr.autocommit(True) + cr = db.serialized_cursor() + cr.autocommit(True) # XXX inhibit the effect of a serialized cursor. is it what we want ? try: try: cr.execute('DROP DATABASE "%s"' % db_name) @@ -201,8 +201,8 @@ class db(netsvc.Service): raise Exception, "Database already exists" db = sql_db.db_connect('template1') - cr = db.cursor() - cr.autocommit(True) + cr = db.serialized_cursor() + cr.autocommit(True) # XXX inhibit the effect of a serialized cursor. is it what we want ? try: cr.execute("""CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE 'template0'""" % db_name) finally: diff --git a/bin/sql_db.py b/bin/sql_db.py index b0212ac1ea4..ff9a6b0c2c6 100644 --- a/bin/sql_db.py +++ b/bin/sql_db.py @@ -21,7 +21,7 @@ ############################################################################## import netsvc -from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_SERIALIZABLE +from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE from psycopg2.pool import ThreadedConnectionPool from psycopg2.psycopg1 import cursor as psycopg1cursor @@ -76,8 +76,9 @@ class Cursor(object): return f(self, *args, **kwargs) return wrapper - def __init__(self, pool): + def __init__(self, pool, serialized=False): self._pool = pool + self._serialized = serialized self._cnx = pool.getconn() self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor) self.autocommit(False) @@ -168,7 +169,8 @@ class Cursor(object): @check def autocommit(self, on): - self._cnx.set_isolation_level([ISOLATION_LEVEL_SERIALIZABLE, ISOLATION_LEVEL_AUTOCOMMIT][bool(on)]) + offlevel = [ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE][bool(self._serialized)] + self._cnx.set_isolation_level([offlevel, ISOLATION_LEVEL_AUTOCOMMIT][bool(on)]) @check def commit(self): @@ -190,6 +192,9 @@ class ConnectionPool(object): def cursor(self): return Cursor(self) + def serialized_cursor(self): + return Cursor(self, True) + def __getattr__(self, name): return getattr(self._pool, name)