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

Method begin

lib/sqlalchemy/engine/base.py:3235–3261  ·  view source on GitHub ↗

Return a context manager delivering a :class:`_engine.Connection` with a :class:`.Transaction` established. E.g.:: with engine.begin() as conn: conn.execute(text("insert into table (x, y, z) values (1, 2, 3)")) conn.execute(text("my_speci

(self)

Source from the content-addressed store, hash-verified

3233
3234 @contextlib.contextmanager
3235 def begin(self) -> Iterator[Connection]:
3236 """Return a context manager delivering a :class:`_engine.Connection`
3237 with a :class:`.Transaction` established.
3238
3239 E.g.::
3240
3241 with engine.begin() as conn:
3242 conn.execute(text("insert into table (x, y, z) values (1, 2, 3)"))
3243 conn.execute(text("my_special_procedure(5)"))
3244
3245 Upon successful operation, the :class:`.Transaction`
3246 is committed. If an error is raised, the :class:`.Transaction`
3247 is rolled back.
3248
3249 .. seealso::
3250
3251 :meth:`_engine.Engine.connect` - procure a
3252 :class:`_engine.Connection` from
3253 an :class:`_engine.Engine`.
3254
3255 :meth:`_engine.Connection.begin` - start a :class:`.Transaction`
3256 for a particular :class:`_engine.Connection`.
3257
3258 """ # noqa: E501
3259 with self.connect() as conn:
3260 with conn.begin():
3261 yield conn
3262
3263 def _run_ddl_visitor(
3264 self,

Callers 15

_run_ddl_visitorMethod · 0.95
_pg_create_dbFunction · 0.45
_pg_drop_dbFunction · 0.45
do_begin_twophaseMethod · 0.45
_oracle_create_dbFunction · 0.45
_oracle_drop_dbFunction · 0.45
_purge_recyclebinFunction · 0.45
_reap_oracle_dbsFunction · 0.45
_mysql_create_dbFunction · 0.45
_mysql_drop_dbFunction · 0.45

Calls 1

connectMethod · 0.95

Tested by 15

test_ss_cursor_statusMethod · 0.36
test_aliases_and_ssMethod · 0.36
test_integrity_errorMethod · 0.36
test_all_levelsMethod · 0.36
_simple_roundtripMethod · 0.36
test_has_tableMethod · 0.36
test_has_table_schemaMethod · 0.36