Created
June 6, 2019 08:09
-
-
Save afonasev/7e1470dcba42cda980eb621a417b8e3d to your computer and use it in GitHub Desktop.
Sqlalchemy session as contextmanager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@contextmanager | |
def session(*args: Any, **kwargs: Any) -> Iterator[sa.orm.Session]: | |
with closing(session_factory(*args, **kwargs)) as _session: | |
try: | |
yield _session | |
except Exception: | |
_session.rollback() | |
raise | |
else: | |
_session.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment