~openerp-dev/openobject-server/trunk-bug-1161881-ishwar

« back to all changes in this revision

Viewing changes to openerp/sql_db.py

  • Committer: Vo Minh Thu
  • Date: 2013-04-04 13:07:04 UTC
  • mfrom: (4743.1.176 openobject-server)
  • Revision ID: vmt@openerp.com-20130404130704-24vsmczw34cssytd
[MERGE] forward merge 7.0 until revision 4919.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#
4
4
#    OpenERP, Open Source Management Solution
5
5
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6
 
#    Copyright (C) 2010-2011 OpenERP s.a. (<http://openerp.com>).
 
6
#    Copyright (C) 2010-2013 OpenERP s.a. (<http://openerp.com>).
7
7
#
8
8
#    This program is free software: you can redistribute it and/or modify
9
9
#    it under the terms of the GNU Affero General Public License as
36
36
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_REPEATABLE_READ
37
37
from psycopg2.pool import PoolError
38
38
from psycopg2.psycopg1 import cursor as psycopg1cursor
39
 
from threading import currentThread
40
39
 
41
40
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
42
41
 
393
392
    def borrow(self, dsn):
394
393
        self._debug('Borrow connection to %r', dsn)
395
394
 
396
 
        # free leaked connections
 
395
        # free dead and leaked connections
397
396
        for i, (cnx, _) in tools.reverse_enumerate(self._connections):
398
397
            if cnx.closed:
399
398
                self._connections.pop(i)
407
406
 
408
407
        for i, (cnx, used) in enumerate(self._connections):
409
408
            if not used and dsn_are_equals(cnx.dsn, dsn):
 
409
                try:
 
410
                    cnx.reset()
 
411
                except psycopg2.OperationalError:
 
412
                    self._debug('Cannot reset connection at index %d: %r', i, cnx.dsn)
 
413
                    # psycopg2 2.4.4 and earlier do not allow closing a closed connection
 
414
                    if not cnx.closed:
 
415
                        cnx.close()
 
416
                    continue
410
417
                self._connections.pop(i)
411
418
                self._connections.append((cnx, True))
412
419
                self._debug('Existing connection found at index %d', i)
507
514
    global _Pool
508
515
    if _Pool is None:
509
516
        _Pool = ConnectionPool(int(tools.config['db_maxconn']))
510
 
    currentThread().dbname = db_name
511
517
    return Connection(_Pool, db_name)
512
518
 
513
519
def close_db(db_name):
515
521
    global _Pool
516
522
    if _Pool:
517
523
        _Pool.close_all(dsn(db_name))
518
 
    ct = currentThread()
519
 
    if hasattr(ct, 'dbname'):
520
 
        delattr(ct, 'dbname')
521
524
 
522
525
 
523
526
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: