def backHome(dbfly): q= MotherPool._pool_queue m= MotherPool._pool_current_mutex sname= dbfly.session_name m.acquire() if MotherPool._pool_type == DB_POOL_ELASTIC and \ MotherPool._pool_current > MotherPool._pool_min: MotherPool._ns_discard(dbfly) Speaker.log_info("Elastic Pool: session %s " "closed and removed.", OKI_COL(sname)) m.release() return try: q.put_nowait(dbfly) except Exception, ss: Speaker.log_warning( "Removing connection %s from Pool: %s.", ERR_COL(dbfly.session_name), ERR_COL(ss)) MotherPool._ns_discard(dbfly) m.release() return
def _init_pool(n= None, pg_conn= None): n= n or MotherPool._pool_min Speaker.log_info("Initializing connection Pool ...") from Queue import Queue MotherPool._pool_queue= Queue() MotherPool._addConnection(n) MotherPool._pool_initialized= True
def newSession(name= None): Speaker.log_info("Initializing session %s", INF_COL(name)) if not MotherPool._pool_initialized: return DbFly(name) m= MotherPool._pool_get_mutex m.acquire() try: session= MotherPool._get_session() except Exception, ss: m.release() Speaker.log_int_raise( "Cannot retrieve Session from Pool (%s). FATAL.", ERR_COL(ss))
def init_abdbda(conf, forced= {}): if _DbInfo.db_initialized: Speaker.log_info('AbDbDa already initialized.') return # Take trace of possible errors: # When Speaker will be initialized, it # will be possible to log them err= None if not isinstance(conf, dict): import mother.speaker as speaker loc= {} names_dict= speaker.__dict__.copy() names_dict.update(globals()) try: execfile(conf, names_dict, loc) except Exception, ss: err= "Unable to read Mother configuration " \ "file %s: %s" % (ERR_COL(conf), ss)
def _add_conn(n=1): """ Never call me directly: use _addConnection!""" cur= MotherPool._pool_current max= MotherPool._pool_max Speaker.log_info( "Adding %s new connection(s) (max= %s, cur= %s)", OKI_COL(n), OKI_COL(max), OKI_COL(cur)) for i in xrange(n): p= DbFly() q= MotherPool._pool_queue p._pool_queue= q q.put(p) MotherPool._pool_added.append(p) MotherPool._pool_current+= n return n
def _safe_execute(execattr, s, d): try: return execattr(s, d) except BrokenConnection, e: Speaker.log_info('Connection to DB seems broken, ' 'now reconnecting...') try: DbOne._connect() except: Speaker.log_raise('Cannot re-establish connection', BrokenConnection) # if we are inside a trans, we have to signal # the broken transaction -> exception. # otherwise, the query is tried once more if DbOne.trans_level: Speaker.log_raise('Connection re-established: transaction is lost.', ConnectionError) return execattr(s, d)
def _get_session(): """ Never call me directly: use newSession!""" db_pool= MotherPool._pool_queue try: db= db_pool.get_nowait() return db except MotherPool._empty_queue: pass full= MotherPool._full() calm= MotherPool._pool_calm # Wait or create immediately a new connection? if calm or full: # wait. ptimeout= MotherPool._pool_timeout Speaker.log_info( "MotherPool: waiting for a free connection " "(timeout= %s) ...", INF_COL(ptimeout)) # If full, wait and hope. if full: return db_pool.get(True, ptimeout) # If calm, wait only try: return db_pool.get(True, ptimeout) except MotherPool._empty_queue: pass # Ok, here the pool is not full. # Moreover, we have already waited. MotherPool._addConnection() return db_pool.get_nowait()
"closed and removed.", OKI_COL(sname)) m.release() return try: q.put_nowait(dbfly) except Exception, ss: Speaker.log_warning( "Removing connection %s from Pool: %s.", ERR_COL(dbfly.session_name), ERR_COL(ss)) MotherPool._ns_discard(dbfly) m.release() return MotherPool._del_orphan(dbfly) Speaker.log_info('Session %s back to the Pool.', OKI_COL(sname)) m.release() return @staticmethod def _ns_discard(dbfly): MotherPool._pool_current-= 1 MotherPool._del_orphan(dbfly) try: dbfly._close() except Exception, ss: Speaker.log_error("Unable to close connection: %s", ERR_COL(ss)) @staticmethod def discard(dbfly):