def __enter__(self): db = self.dbmgr._transaction_local.rdb # outermost readonly db if not db: db = self.dbmgr._transaction_local.wdb # reuse wrapped connection if db: db = ConnectionWrapper(db.cnx, db.log, readonly=True) else: db = self.dbmgr.get_connection(readonly=True) self.dbmgr._transaction_local.rdb = self.db = db return db
def __enter__(self): db = self.dbmgr._transaction_local.wdb # outermost writable db if not db: db = self.dbmgr._transaction_local.rdb # reuse wrapped connection if db: db = ConnectionWrapper(db.cnx, db.log) else: db = self.dbmgr.get_connection() self.dbmgr._transaction_local.wdb = self.db = db return db
def get_connection(self, readonly=False): """Get a database connection from the pool. If `readonly` is `True`, the returned connection will purposely lack the `rollback` and `commit` methods. """ if not self._cnx_pool: connector, args = self.get_connector() self._cnx_pool = ConnectionPool(5, connector, **args) db = self._cnx_pool.get_cnx(self.timeout or None) if readonly: db = ConnectionWrapper(db, readonly=True) return db