MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / sessionmaker

Class sessionmaker

lib/sqlalchemy/orm/session.py:4934–5163  ·  view source on GitHub ↗

A configurable :class:`.Session` factory. The :class:`.sessionmaker` factory generates new :class:`.Session` objects when called, creating them given the configurational arguments established here. e.g.:: from sqlalchemy import create_engine from sqlalchemy.orm imp

Source from the content-addressed store, hash-verified

4932
4933
4934class sessionmaker(_SessionClassMethods, Generic[_S]):
4935 """A configurable :class:`.Session` factory.
4936
4937 The :class:`.sessionmaker` factory generates new
4938 :class:`.Session` objects when called, creating them given
4939 the configurational arguments established here.
4940
4941 e.g.::
4942
4943 from sqlalchemy import create_engine
4944 from sqlalchemy.orm import sessionmaker
4945
4946 # an Engine, which the Session will use for connection
4947 # resources
4948 engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/")
4949
4950 Session = sessionmaker(engine)
4951
4952 with Session() as session:
4953 session.add(some_object)
4954 session.add(some_other_object)
4955 session.commit()
4956
4957 Context manager use is optional; otherwise, the returned
4958 :class:`_orm.Session` object may be closed explicitly via the
4959 :meth:`_orm.Session.close` method. Using a
4960 ``try:/finally:`` block is optional, however will ensure that the close
4961 takes place even if there are database errors::
4962
4963 session = Session()
4964 try:
4965 session.add(some_object)
4966 session.add(some_other_object)
4967 session.commit()
4968 finally:
4969 session.close()
4970
4971 :class:`.sessionmaker` acts as a factory for :class:`_orm.Session`
4972 objects in the same way as an :class:`_engine.Engine` acts as a factory
4973 for :class:`_engine.Connection` objects. In this way it also includes
4974 a :meth:`_orm.sessionmaker.begin` method, that provides a context
4975 manager which both begins and commits a transaction, as well as closes
4976 out the :class:`_orm.Session` when complete, rolling back the transaction
4977 if any errors occur::
4978
4979 Session = sessionmaker(engine)
4980
4981 with Session.begin() as session:
4982 session.add(some_object)
4983 session.add(some_other_object)
4984 # commits transaction, closes session
4985
4986 .. versionadded:: 1.4
4987
4988 When calling upon :class:`_orm.sessionmaker` to construct a
4989 :class:`_orm.Session`, keyword arguments may also be passed to the
4990 method; these arguments will override that of the globally configured
4991 parameters. Below we use a :class:`_orm.sessionmaker` bound to a certain

Callers 15

scoped_session.pyFile · 0.90
test_with_for_updateFunction · 0.90
test_exec_optionsFunction · 0.90
session_factoryFunction · 0.90
scoped_session_factoryFunction · 0.90
test_8837_syncFunction · 0.90
setup_mappersMethod · 0.90
setup_sessionMethod · 0.90
test_baked_mixMethod · 0.90
setup_sessionMethod · 0.90
test_attributesMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_with_for_updateFunction · 0.72
test_exec_optionsFunction · 0.72
test_8837_syncFunction · 0.72
setup_mappersMethod · 0.72
setup_sessionMethod · 0.72
test_baked_mixMethod · 0.72
setup_sessionMethod · 0.72
test_attributesMethod · 0.72