[FIX] database connections aren't serialized by default
authorChristophe Simonis <christophe@taupe>
Sat, 31 Jan 2009 18:36:18 +0000 (19:36 +0100)
committerChristophe Simonis <christophe@taupe>
Sat, 31 Jan 2009 18:36:18 +0000 (19:36 +0100)
lp bug: https://launchpad.net/bugs/314449 fixed

bzr revid: christophe@taupe-20090131183618-pglu86hzq3ewsfxi

bin/service/web_services.py
bin/sql_db.py

index 53936d2..d4b2a6e 100644 (file)
@@ -69,9 +69,9 @@ class db(netsvc.Service):
         self.actions[id] = {'clean': False}
 
         db = sql_db.db_connect('template1')
         self.actions[id] = {'clean': False}
 
         db = sql_db.db_connect('template1')
-        cr = db.cursor()
+        cr = db.serialized_cursor()
         try:
         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()
             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')
         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)
         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')
             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:
         try:
             cr.execute("""CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE 'template0'""" % db_name)
         finally:
index b0212ac..ff9a6b0 100644 (file)
@@ -21,7 +21,7 @@
 ##############################################################################
 
 import netsvc
 ##############################################################################
 
 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
 
 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
 
             return f(self, *args, **kwargs)
         return wrapper
 
-    def __init__(self, pool):
+    def __init__(self, pool, serialized=False):
         self._pool = pool
         self._pool = pool
+        self._serialized = serialized
         self._cnx = pool.getconn()
         self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor)
         self.autocommit(False)
         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):
     
     @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):
     
     @check
     def commit(self):
@@ -190,6 +192,9 @@ class ConnectionPool(object):
     def cursor(self):
         return Cursor(self)
 
     def cursor(self):
         return Cursor(self)
 
+    def serialized_cursor(self):
+        return Cursor(self, True)
+
     def __getattr__(self, name):
         return getattr(self._pool, name)
 
     def __getattr__(self, name):
         return getattr(self._pool, name)