MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / begin

Method begin

lib/sqlalchemy/engine/base.py:808–881  ·  view source on GitHub ↗

Begin a transaction prior to autobegin occurring. E.g.:: with engine.connect() as conn: with conn.begin() as trans: conn.execute(table.insert(), {"username": "sandy"}) The returned object is an instance of :class:`_engine.RootTransac

(self)

Source from the content-addressed store, hash-verified

806 self.begin()
807
808 def begin(self) -> RootTransaction:
809 """Begin a transaction prior to autobegin occurring.
810
811 E.g.::
812
813 with engine.connect() as conn:
814 with conn.begin() as trans:
815 conn.execute(table.insert(), {"username": "sandy"})
816
817 The returned object is an instance of :class:`_engine.RootTransaction`.
818 This object represents the "scope" of the transaction,
819 which completes when either the :meth:`_engine.Transaction.rollback`
820 or :meth:`_engine.Transaction.commit` method is called; the object
821 also works as a context manager as illustrated above.
822
823 The :meth:`_engine.Connection.begin` method begins a
824 transaction that normally will be begun in any case when the connection
825 is first used to execute a statement. The reason this method might be
826 used would be to invoke the :meth:`_events.ConnectionEvents.begin`
827 event at a specific time, or to organize code within the scope of a
828 connection checkout in terms of context managed blocks, such as::
829
830 with engine.connect() as conn:
831 with conn.begin():
832 conn.execute(...)
833 conn.execute(...)
834
835 with conn.begin():
836 conn.execute(...)
837 conn.execute(...)
838
839 The above code is not fundamentally any different in its behavior than
840 the following code which does not use
841 :meth:`_engine.Connection.begin`; the below style is known
842 as "commit as you go" style::
843
844 with engine.connect() as conn:
845 conn.execute(...)
846 conn.execute(...)
847 conn.commit()
848
849 conn.execute(...)
850 conn.execute(...)
851 conn.commit()
852
853 From a database point of view, the :meth:`_engine.Connection.begin`
854 method does not emit any SQL or change the state of the underlying
855 DBAPI connection in any way; the Python DBAPI does not have any
856 concept of explicit transaction begin.
857
858 .. seealso::
859
860 :ref:`tutorial_working_with_transactions` - in the
861 :ref:`unified_tutorial`
862
863 :meth:`_engine.Connection.begin_nested` - use a SAVEPOINT
864
865 :meth:`_engine.Connection.begin_twophase` -

Callers 1

_autobeginMethod · 0.95

Calls 1

RootTransactionClass · 0.85

Tested by

no test coverage detected