~donkirkby/openobject-server/setup-site-packages

« back to all changes in this revision

Viewing changes to bin/sql_db.py

  • Committer: Christophe Simonis
  • Date: 2010-01-20 16:39:13 UTC
  • Revision ID: chs@tinyerp.com-20100120163913-5ftbs7e85lwqb1et
[FIX] remove the lock on database connections
[ADD] exported method check_connectivity
[REF] better logging of the Connection and ConnectionPool objects
[REF] use template0 as template when create new database

Show diffs side-by-side

added added

removed removed

Lines of Context:
119
119
 
120
120
        if self.sql_log:
121
121
            now = mdt.now()
122
 
        
 
122
 
123
123
        try:
124
124
            params = params or None
125
125
            res = self._obj.execute(query, params)
195
195
    def autocommit(self, on):
196
196
        offlevel = [ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE][bool(self._serialized)]
197
197
        self._cnx.set_isolation_level([offlevel, ISOLATION_LEVEL_AUTOCOMMIT][bool(on)])
198
 
    
 
198
 
199
199
    @check
200
200
    def commit(self):
201
201
        return self._cnx.commit()
202
 
    
 
202
 
203
203
    @check
204
204
    def rollback(self):
205
205
        return self._cnx.rollback()
228
228
        self._lock = threading.Lock()
229
229
        self._logger = netsvc.Logger()
230
230
 
231
 
    def _log(self, msg):
232
 
        #self._logger.notifyChannel('ConnectionPool', netsvc.LOG_INFO, msg)
233
 
        pass
234
231
    def _debug(self, msg):
235
 
        #self._logger.notifyChannel('ConnectionPool', netsvc.LOG_DEBUG, msg)
236
 
        pass
 
232
        self._logger.notifyChannel('ConnectionPool', netsvc.LOG_DEBUG, msg)
 
233
        #pass
237
234
 
238
235
    @locked
239
236
    def borrow(self, dsn):
240
 
        self._log('Borrow connection to %s' % (dsn,))
 
237
        self._debug('Borrow connection to %s' % (dsn,))
241
238
 
242
239
        result = None
243
240
        for i, (cnx, used) in enumerate(self._connections):
270
267
 
271
268
    @locked
272
269
    def give_back(self, connection):
273
 
        self._log('Give back connection to %s' % (connection.dsn,))
 
270
        self._debug('Give back connection to %s' % (connection.dsn,))
274
271
        for i, (cnx, used) in enumerate(self._connections):
275
272
            if cnx is connection:
276
273
                self._connections.pop(i)
281
278
 
282
279
    @locked
283
280
    def close_all(self, dsn):
 
281
        self._debug('Close all connections to %s' % (dsn,))
284
282
        for i, (cnx, used) in tools.reverse_enumerate(self._connections):
285
283
            if dsn_are_equals(cnx.dsn, dsn):
286
284
                cnx.close()
288
286
 
289
287
 
290
288
class Connection(object):
291
 
    __LOCKS = {}
 
289
    def _debug(self, msg):
 
290
        self._logger.notifyChannel('Connection', netsvc.LOG_DEBUG, msg)
292
291
 
293
 
    def __init__(self, pool, dbname, unique=False):
 
292
    def __init__(self, pool, dbname):
294
293
        self.dbname = dbname
295
294
        self._pool = pool
296
 
        self._unique = unique
297
 
 
298
 
    def __enter__(self):
299
 
        if self._unique:
300
 
            self.lock()
301
 
        return self
302
 
    
303
 
    def __exit__(self, exc_type, exc_value, traceback):
304
 
        if self._unique:
305
 
            self.release()
306
 
 
307
 
    def lock(self):
308
 
        if self.dbname not in self.__LOCKS:
309
 
            self.__LOCKS[self.dbname] = threading.Lock()
310
 
        self.__LOCKS[self.dbname].acquire()
311
 
        
312
 
    def release(self):
313
 
        close_db(self.dbname)
314
 
        self.__LOCKS[self.dbname].release()
 
295
        self._logger = netsvc.Logger()
315
296
 
316
297
    def cursor(self, serialized=False):
317
 
        if self._unique:
318
 
            lock = self.__LOCKS.get(self.dbname, None)
319
 
            if not (lock and lock.locked()):
320
 
                netsvc.Logger().notifyChannel('Connection', netsvc.LOG_WARNING, 'Unprotected connection to %s' % (self.dbname,))
321
 
 
 
298
        cursor_type = serialized and 'serialized ' or ''
 
299
        self._debug('create %scursor to "%s"' % (cursor_type, self.dbname,))
322
300
        return Cursor(self._pool, self.dbname, serialized=serialized)
323
301
 
324
302
    def serialized_cursor(self):
354
332
_Pool = ConnectionPool(int(tools.config['db_maxconn']))
355
333
 
356
334
def db_connect(db_name):
357
 
    unique = db_name in ['template1', 'template0']
358
 
    return Connection(_Pool, db_name, unique)
 
335
    return Connection(_Pool, db_name)
359
336
 
360
337
def close_db(db_name):
361
338
    _Pool.close_all(dsn(db_name))