MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / sessionmaker

Class sessionmaker

lib/sqlalchemy/orm/session.py:5098–5327  ·  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

5096
5097
5098class sessionmaker(_SessionClassMethods, Generic[_S]):
5099 """A configurable :class:`.Session` factory.
5100
5101 The :class:`.sessionmaker` factory generates new
5102 :class:`.Session` objects when called, creating them given
5103 the configurational arguments established here.
5104
5105 e.g.::
5106
5107 from sqlalchemy import create_engine
5108 from sqlalchemy.orm import sessionmaker
5109
5110 # an Engine, which the Session will use for connection
5111 # resources
5112 engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/")
5113
5114 Session = sessionmaker(engine)
5115
5116 with Session() as session:
5117 session.add(some_object)
5118 session.add(some_other_object)
5119 session.commit()
5120
5121 Context manager use is optional; otherwise, the returned
5122 :class:`_orm.Session` object may be closed explicitly via the
5123 :meth:`_orm.Session.close` method. Using a
5124 ``try:/finally:`` block is optional, however will ensure that the close
5125 takes place even if there are database errors::
5126
5127 session = Session()
5128 try:
5129 session.add(some_object)
5130 session.add(some_other_object)
5131 session.commit()
5132 finally:
5133 session.close()
5134
5135 :class:`.sessionmaker` acts as a factory for :class:`_orm.Session`
5136 objects in the same way as an :class:`_engine.Engine` acts as a factory
5137 for :class:`_engine.Connection` objects. In this way it also includes
5138 a :meth:`_orm.sessionmaker.begin` method, that provides a context
5139 manager which both begins and commits a transaction, as well as closes
5140 out the :class:`_orm.Session` when complete, rolling back the transaction
5141 if any errors occur::
5142
5143 Session = sessionmaker(engine)
5144
5145 with Session.begin() as session:
5146 session.add(some_object)
5147 session.add(some_other_object)
5148 # commits transaction, closes session
5149
5150 .. versionadded:: 1.4
5151
5152 When calling upon :class:`_orm.sessionmaker` to construct a
5153 :class:`_orm.Session`, keyword arguments may also be passed to the
5154 method; these arguments will override that of the globally configured
5155 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