def setUp(self): self.pool = txpostgres.ConnectionPool(None, user=DB_USER, password=DB_PASS, host=DB_HOST, database=DB_NAME) return self.pool.start()
def add_txpool(self): from txpostgres import txpostgres dsn = self.engine.raw_connection().connection.dsn self.txpool = txpostgres.ConnectionPool("heleleley", dsn, min=1) self.txpool_start_deferred = self.txpool.start() self.txpool_start_deferred.addCallback( lambda p: logger.info("TxPool %r started.", p)) return self.txpool_start_deferred
def use_txpostgres(self): engine = create_engine(self.connstr, **self.kwargs) dsn = engine.raw_connection().connection.dsn engine.dispose() from txpostgres import txpostgres self.txpool = txpostgres.ConnectionPool(self.name, dsn) self.deferred_start = self.txpool.start() self.engine.close()
def test_errorsInInteractionHotswappingConnections(self): """ After getting a RollbackFailed failure it is possible to remove the offending connection from the pool, open a new one and put it in the pool to replace the removed one. """ pool = txpostgres.ConnectionPool(None, user=DB_USER, password=DB_PASS, host=DB_HOST, database=DB_NAME, min=1) self.assertEquals(pool.min, 1) d = pool.start() # poison the connection c, = pool.connections c.cursorFactory = NotRollingBackCursor # run stuff that breaks def brokenInteraction(c): return c.execute("boom") d.addCallback(lambda _: pool.runInteraction(brokenInteraction)) d.addCallback(lambda _: self.fail("No exception")) def checkErrorAndHotswap(f): f.trap(txpostgres.RollbackFailed) e = f.value self.assertIdentical(e.connection.cursorFactory, NotRollingBackCursor) errors = self.flushLoggedErrors() self.assertEquals(len(errors), 1) self.assertEquals(errors[0].value.args[0], "boom") pool.remove(e.connection) e.connection.close() c = txpostgres.Connection() self.assertNotIdentical(c.cursorFactory, NotRollingBackCursor) d = c.connect(user=DB_USER, password=DB_PASS, host=DB_HOST, database=DB_NAME) return d.addCallback(lambda c: pool.add(c)) d.addErrback(checkErrorAndHotswap) d.addCallback(lambda _: defer.gatherResults( [pool.runQuery("select 1") for _ in range(3)])) d.addCallback(self.assertEquals, [[(1, )]] * 3) return d.addCallback(lambda _: pool.close())
def test_removeWhileBusy(self): """ Removing a connection from the pool while it's running a query raises an exception. """ pool = txpostgres.ConnectionPool(None, user=DB_USER, password=DB_PASS, host=DB_HOST, database=DB_NAME, min=1) d = pool.start() def simple(c): self.assertRaises(ValueError, pool.remove, c._connection) d.addCallback(lambda pool: pool.runInteraction(simple)) return d.addCallback(lambda _: pool.close())
def __init__(self, id, config): """ :param id: The ID of the connection item. :type id: unicode :param config: The connection item configuration. :type config: dict """ self.id = id self.config = config self.started = None self.stopped = None params = { 'host': config.get('host', 'localhost'), 'port': config.get('port', 5432), 'database': config['database'], 'user': config['user'], 'password': get_config_value(config, 'password'), } self.pool = txpostgres.ConnectionPool(None, min=5, **params)
def onJoin(self, details): ## create a new database connection pool. connections are created lazy (as needed) ## see: https://twistedmatrix.com/documents/current/api/twisted.enterprise.adbapi.ConnectionPool.html ## pool = txpostgres.ConnectionPool(None, host='127.0.0.1', port=5432, database='test', user='******', password='******') yield pool.start() print("DB connection pool started") ## we'll be doing all database access via this database connection pool ## self.db = pool ## register all procedures on this class which have been ## decorated to register them for remoting. ## regs = yield self.register(self) print("registered {} procedures".format(len(regs)))
def __init__(self, config): txpostgres.Connection.connectionFactory = self._tcp_connfactory({ 'tcp_keepidle': int(config['BDMD_TCP_KEEPIDLE']), 'tcp_keepcnt': int(config['BDMD_TCP_KEEPCNT']), 'tcp_keepintvl': int(config['BDMD_TCP_KEEPINTVL']), }) self.dbpool = txpostgres.ConnectionPool( None, min=int(config['BDMD_TXPG_CONNPOOL']), host=config['BDM_PG_HOST'], port=int(config['BDM_PG_PORT']), database=config['BDM_PG_MGMT_DBNAME'], user=config['BDM_PG_USER'], password=config['BDM_PG_PASSWORD'], ) self.dbpool_started = False self.config = {} self.config['logdir'] = os.path.join( os.path.abspath(config['VAR_DIR']), LOG_SUBDIR) self.config['max_delay'] = int(config['BDMD_MAX_DELAY']) self.config['time_error'] = int(config['BDMD_TIME_ERROR'])
def _connectPool(self): logging.debug("IndxConnectionPool _connectionPool") return txpostgres.ConnectionPool(self._ignored, *self.connargs, **self.connkw)
'host': '127.0.0.1', 'port': 5432, } if os.environ.get('DATABASE_URL', None): url = urlparse.urlparse(os.environ['DATABASE_URL']) database.update({ 'dbname': url.path[1:], 'user': url.username, 'password': url.password, 'host': url.hostname, 'port': url.port, }) pool = txpostgres.ConnectionPool(None, connection_factory=extras.RealDictConnection, **database) pool.start() @defer.inlineCallbacks def query(q, parameters={}): try: log.msg("db query %s:, %s" % (q, parameters)) result = yield pool.runQuery(q, parameters) log.msg("returning: %s" % result) defer.returnValue(result) except psycopg2.ProgrammingError as e: if e.message == 'no results to fetch': defer.returnValue([]) else: raise