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)])
200
200
def commit(self):
201
201
return self._cnx.commit()
204
204
def rollback(self):
205
205
return self._cnx.rollback()
228
228
self._lock = threading.Lock()
229
229
self._logger = netsvc.Logger()
232
#self._logger.notifyChannel('ConnectionPool', netsvc.LOG_INFO, msg)
234
231
def _debug(self, msg):
235
#self._logger.notifyChannel('ConnectionPool', netsvc.LOG_DEBUG, msg)
232
self._logger.notifyChannel('ConnectionPool', netsvc.LOG_DEBUG, msg)
239
236
def borrow(self, dsn):
240
self._log('Borrow connection to %s' % (dsn,))
237
self._debug('Borrow connection to %s' % (dsn,))
243
240
for i, (cnx, used) in enumerate(self._connections):
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)
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):
290
288
class Connection(object):
289
def _debug(self, msg):
290
self._logger.notifyChannel('Connection', netsvc.LOG_DEBUG, msg)
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
303
def __exit__(self, exc_type, exc_value, traceback):
308
if self.dbname not in self.__LOCKS:
309
self.__LOCKS[self.dbname] = threading.Lock()
310
self.__LOCKS[self.dbname].acquire()
313
close_db(self.dbname)
314
self.__LOCKS[self.dbname].release()
295
self._logger = netsvc.Logger()
316
297
def cursor(self, serialized=False):
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,))
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)
324
302
def serialized_cursor(self):
354
332
_Pool = ConnectionPool(int(tools.config['db_maxconn']))
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)
360
337
def close_db(db_name):
361
338
_Pool.close_all(dsn(db_name))