[FIX] database connections aren't serialized by default

lp bug: https://launchpad.net/bugs/314449 fixed

bzr revid: christophe@taupe-20090131183618-pglu86hzq3ewsfxi
This commit is contained in:
Christophe Simonis 2009-01-31 19:36:18 +01:00
parent 27240361ca
commit 9c721d3090
2 changed files with 14 additions and 9 deletions

View File

@ -69,9 +69,9 @@ class db(netsvc.Service):
self.actions[id] = {'clean': False} self.actions[id] = {'clean': False}
db = sql_db.db_connect('template1') 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) cr.execute('CREATE DATABASE "%s" ENCODING \'unicode\'' % db_name)
finally: finally:
cr.close() cr.close()
@ -149,8 +149,8 @@ class db(netsvc.Service):
logger = netsvc.Logger() logger = netsvc.Logger()
db = sql_db.db_connect('template1') db = sql_db.db_connect('template1')
cr = db.cursor() cr = db.serialized_cursor()
cr.autocommit(True) cr.autocommit(True) # XXX inhibit the effect of a serialized cursor. is it what we want ?
try: try:
try: try:
cr.execute('DROP DATABASE "%s"' % db_name) cr.execute('DROP DATABASE "%s"' % db_name)
@ -201,8 +201,8 @@ class db(netsvc.Service):
raise Exception, "Database already exists" raise Exception, "Database already exists"
db = sql_db.db_connect('template1') db = sql_db.db_connect('template1')
cr = db.cursor() cr = db.serialized_cursor()
cr.autocommit(True) cr.autocommit(True) # XXX inhibit the effect of a serialized cursor. is it what we want ?
try: try:
cr.execute("""CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE 'template0'""" % db_name) cr.execute("""CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE 'template0'""" % db_name)
finally: finally:

View 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.pool import ThreadedConnectionPool
from psycopg2.psycopg1 import cursor as psycopg1cursor from psycopg2.psycopg1 import cursor as psycopg1cursor
@ -76,8 +76,9 @@ class Cursor(object):
return f(self, *args, **kwargs) return f(self, *args, **kwargs)
return wrapper 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._cnx = pool.getconn()
self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor) self._obj = self._cnx.cursor(cursor_factory=psycopg1cursor)
self.autocommit(False) self.autocommit(False)
@ -168,7 +169,8 @@ class Cursor(object):
@check @check
def autocommit(self, on): 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 @check
def commit(self): def commit(self):
@ -190,6 +192,9 @@ class ConnectionPool(object):
def cursor(self): def cursor(self):
return Cursor(self) return Cursor(self)
def serialized_cursor(self):
return Cursor(self, True)
def __getattr__(self, name): def __getattr__(self, name):
return getattr(self._pool, name) return getattr(self._pool, name)