def add_txpool(self): from txpostgres.txpostgres import Connection, ConnectionPool from txpostgres.reconnection import DeadConnectionDetector class LoggingDeadConnectionDetector(DeadConnectionDetector): def startReconnecting(self, f): logger.warning('TxPool database connection down: %r)', f.value) return DeadConnectionDetector.startReconnecting(self, f) def reconnect(self): logger.warning('TxPool reconnecting...') return DeadConnectionDetector.reconnect(self) def connectionRecovered(self): logger.warning('TxPool connection recovered') return DeadConnectionDetector.connectionRecovered(self) dsn = self.engine.raw_connection().connection.dsn self.txpool = 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 main(): global db_pool db_pool = ConnectionPool('psycopg2', **db_config) yield db_pool.start()
class SqlDataStore(DataStoreBase): def __init__(self, connection_string=None, engine=None, metadata=None, **kwargs): DataStoreBase.__init__(self, type='sqlalchemy') from sqlalchemy import MetaData from sqlalchemy.orm import sessionmaker from sqlalchemy.engine import Engine if engine is not None: assert isinstance(engine, Engine) if metadata is not None: assert isinstance(metadata, MetaData) self.__kwargs = kwargs self.__metadata = None self.__engine = None self.Session = None self.metadata = metadata or MetaData() self.engine = engine self.Session = sessionmaker() self.connection_string = connection_string self.txpool = None """TxPostgres connection pool. Added when `add_txpool` is called.""" self.txpool_start_deferred = None """Deferred from TxPostgres pool start().""" def add_txpool(self): from txpostgres.txpostgres import Connection, ConnectionPool from txpostgres.reconnection import DeadConnectionDetector class LoggingDeadConnectionDetector(DeadConnectionDetector): def startReconnecting(self, f): logger.warning('TxPool database connection down: %r)', f.value) return DeadConnectionDetector.startReconnecting(self, f) def reconnect(self): logger.warning('TxPool reconnecting...') return DeadConnectionDetector.reconnect(self) def connectionRecovered(self): logger.warning('TxPool connection recovered') return DeadConnectionDetector.connectionRecovered(self) dsn = self.engine.raw_connection().connection.dsn self.txpool = 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 connect(self): return self.__engine.connect() @property def meta(self): return self.__metadata @property def metadata(self): return self.__metadata @metadata.setter def metadata(self, metadata): self.__metadata = metadata if self.__engine is not None and metadata is not None: self.__metadata.bind = self.__engine @property def engine(self): return self.__engine @engine.setter def engine(self, engine): self.__engine = engine if engine is not None: if self.metadata is not None: self.metadata.bind = engine if self.Session is not None: self.Session.configure(bind=engine, expire_on_commit=False) @property def kwargs(self): return self.__kwargs @property def connection_string(self): return self.__connection_string @connection_string.setter def connection_string(self, what): from sqlalchemy.engine import create_engine self.__connection_string = what if what is None: self.engine = None else: if what.startswith('sqlite://') or what.endswith(":memory:"): from sqlalchemy.pool import StaticPool self.__kwargs['poolclass'] = StaticPool if what.startswith('sqlite'): self.__kwargs['connect_args'] = {'check_same_thread': False} if 'pool_size' in self.__kwargs: del self.__kwargs['pool_size'] self.engine = create_engine(what, **self.__kwargs) logger.info("%r started with: %r", self.engine, self.kwargs)