def scoped_session(session_factory, scopefunc=None): """Provides thread-local management of Sessions. This is a front-end function to the [sqlalchemy.orm.scoping#ScopedSession] class. Usage:: Session = scoped_session(sessionmaker(autoflush=True)) To instantiate a Session object which is part of the scoped context, instantiate normally:: session = Session() Most session methods are available as classmethods from the scoped session:: Session.commit() Session.close() To map classes so that new instances are saved in the current Session automatically, as well as to provide session-aware class attributes such as "query", use the `mapper` classmethod from the scoped session:: mapper = Session.mapper mapper(Class, table, ...) """ return ScopedSession(session_factory, scopefunc=scopefunc)
def scoped_session(session_factory, scopefunc=None): """Provides thread-local management of Sessions. This is a front-end function to :class:`~sqlalchemy.orm.scoping.ScopedSession`. :param session_factory: a callable function that produces :class:`Session` instances, such as :func:`sessionmaker` or :func:`create_session`. :param scopefunc: optional, TODO :returns: an :class:`~sqlalchemy.orm.scoping.ScopedSession` instance Usage:: Session = scoped_session(sessionmaker(autoflush=True)) To instantiate a Session object which is part of the scoped context, instantiate normally:: session = Session() Most session methods are available as classmethods from the scoped session:: Session.commit() Session.close() """ return ScopedSession(session_factory, scopefunc=scopefunc)
def _insert_initial_rows(cls, session_factory: ScopedSession, liveliness_probe_s: int, epoch_start_date: datetime, max_claim_retries: int, min_ms_between_claim_retries: int, max_ms_between_claim_retries: int, manifest_row_class: Any, properties_class: Any) -> None: """ Populates the newly created manifest and properties tables with the correct data. """ session = session_factory() manifest_rows = [ manifest_row_class(generator_id=i) for i in range(cls.MAX_GENERATOR_ID) ] properties = [ properties_class(key="liveliness_probe_s", value=liveliness_probe_s), properties_class(key="epoch_start_ms", value=epoch_start_date.timestamp()), properties_class(key="max_claim_retries", value=max_claim_retries), properties_class(key="min_ms_between_claim_retries", value=min_ms_between_claim_retries), properties_class(key="max_ms_between_claim_retries", value=max_ms_between_claim_retries) ] try: logging.info("Populating manifest table") session.bulk_save_objects(manifest_rows) logging.info("Populating properties table") session.bulk_save_objects(properties) session.commit() except InvalidRequestError: session.rollback() finally: session_factory.remove()
from sqlalchemy.orm.scoping import ScopedSession from transaction._transaction import Status as ZopeStatus import warnings from zope.sqlalchemy import ZopeTransactionExtension TWOPHASE = True class AlchemistWarning(RuntimeWarning): """Warnings of features that will be removed in a next version. """ class AlchemistTransactionExtension(ZopeTransactionExtension): def before_commit(self, session): # zope.sqlalchemy makes an assert here, to not allow saving the session # directly, so that commits must go through the transaction. # Alchemist's manager worked without this, and so we must provide a # BBB for alchemist users. We change the assert to a mare warning. if self.transaction_manager.get().status != ZopeStatus.COMMITTING: warnings.warn( "Transaction must be committed using the transaction manager", AlchemistWarning) Session = ScopedSession( sessionmaker(autoflush=True, autocommit=False, extension=AlchemistTransactionExtension(), twophase=TWOPHASE))
def get_session_factory_of_scoped(self) -> ScopedSession: session_factory = sessionmaker( bind=self.engine, class_=_SessionContext) # 改成了自定义的Session类 return ScopedSession(session_factory)