[FIX] delete the connection pool to template1 when finished

[IMP] sql_db.db_connect does not take the 'serialize' parameter anymore

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

bzr revid: christophe@tinyerp.com-20090112094020-6l1hdgp5oflengl6
This commit is contained in:
Christophe Simonis 2009-01-12 10:40:20 +01:00
parent e6c2039ec4
commit 9aa2c5fb5c
2 changed files with 37 additions and 26 deletions

View File

@ -68,12 +68,14 @@ class db(netsvc.Service):
self.actions[id] = {'clean': False} self.actions[id] = {'clean': False}
db = sql_db.db_connect('template1', serialize=1) db = sql_db.db_connect('template1')
cr = db.cursor() cr = db.cursor()
cr.autocommit(True) try:
time.sleep(0.2) cr.autocommit(True)
cr.execute('CREATE DATABASE ' + db_name + ' ENCODING \'unicode\'') cr.execute('CREATE DATABASE ' + db_name + ' ENCODING \'unicode\'')
cr.close() finally:
cr.close()
sql_db.close_db('template1')
class DBInitialize(object): class DBInitialize(object):
def __call__(self, serv, id, db_name, demo, lang, user_password='admin'): def __call__(self, serv, id, db_name, demo, lang, user_password='admin'):
try: try:
@ -146,7 +148,7 @@ class db(netsvc.Service):
sql_db.close_db(db_name) sql_db.close_db(db_name)
logger = netsvc.Logger() logger = netsvc.Logger()
db = sql_db.db_connect('template1', serialize=1) db = sql_db.db_connect('template1')
cr = db.cursor() cr = db.cursor()
cr.autocommit(True) cr.autocommit(True)
try: try:
@ -161,6 +163,7 @@ class db(netsvc.Service):
'DROP DB: %s' % (db_name)) 'DROP DB: %s' % (db_name))
finally: finally:
cr.close() cr.close()
sql_db.close_db('template1')
return True return True
def dump(self, password, db_name): def dump(self, password, db_name):
@ -197,11 +200,14 @@ class db(netsvc.Service):
'RESTORE DB: %s already exists' % (db_name,)) 'RESTORE DB: %s already exists' % (db_name,))
raise Exception, "Database already exists" raise Exception, "Database already exists"
db = sql_db.db_connect('template1', serialize=1) db = sql_db.db_connect('template1')
cr = db.cursor() cr = db.cursor()
cr.autocommit(True) cr.autocommit(True)
cr.execute('CREATE DATABASE ' + db_name + ' ENCODING \'unicode\'') try:
cr.close() cr.execute('CREATE DATABASE ' + db_name + ' ENCODING \'unicode\'')
finally:
cr.close()
sql_db.close_db('template1')
cmd = ['pg_restore'] cmd = ['pg_restore']
if tools.config['db_user']: if tools.config['db_user']:
@ -240,24 +246,28 @@ class db(netsvc.Service):
def list(self): def list(self):
db = sql_db.db_connect('template1') db = sql_db.db_connect('template1')
cr = db.cursor()
try: try:
cr = db.cursor() try:
db_user = tools.config["db_user"] cr = db.cursor()
if not db_user and os.name == 'posix': db_user = tools.config["db_user"]
import pwd if not db_user and os.name == 'posix':
db_user = pwd.getpwuid(os.getuid())[0] import pwd
if not db_user: db_user = pwd.getpwuid(os.getuid())[0]
cr.execute("select decode(usename, 'escape') from pg_user where usesysid=(select datdba from pg_database where datname=%s)", (tools.config["db_name"],)) if not db_user:
res = cr.fetchone() cr.execute("select decode(usename, 'escape') from pg_user where usesysid=(select datdba from pg_database where datname=%s)", (tools.config["db_name"],))
db_user = res and str(res[0]) res = cr.fetchone()
if db_user: db_user = res and str(res[0])
cr.execute("select decode(datname, 'escape') from pg_database where datdba=(select usesysid from pg_user where usename=%s) and datname not in ('template0', 'template1', 'postgres')", (db_user,)) if db_user:
else: cr.execute("select decode(datname, 'escape') from pg_database where datdba=(select usesysid from pg_user where usename=%s) and datname not in ('template0', 'template1', 'postgres')", (db_user,))
cr.execute("select decode(datname, 'escape') from pg_database where datname not in('template0', 'template1','postgres')") else:
res = [str(name) for (name,) in cr.fetchall()] cr.execute("select decode(datname, 'escape') from pg_database where datname not in('template0', 'template1','postgres')")
res = [str(name) for (name,) in cr.fetchall()]
except:
res = []
finally:
cr.close() cr.close()
except: sql_db.close_db('template1')
res = []
res.sort() res.sort()
return res return res

View File

@ -221,9 +221,10 @@ class PoolManager(object):
logger = netsvc.Logger() logger = netsvc.Logger()
logger.notifyChannel('dbpool', netsvc.LOG_INFO, 'Closing all connections to %s' % (db_name,)) logger.notifyChannel('dbpool', netsvc.LOG_INFO, 'Closing all connections to %s' % (db_name,))
cls._pools[db_name].closeall() cls._pools[db_name].closeall()
print "closeall done"
del cls._pools[db_name] del cls._pools[db_name]
def db_connect(db_name, serialize=0): def db_connect(db_name):
return PoolManager.get(db_name) return PoolManager.get(db_name)
def close_db(db_name): def close_db(db_name):