emitted, a new transaction begins implicitly::
with engine.connect() as connection:
- connection.execute("<some statement>")
+ connection.execute(text("<some statement>"))
connection.commit() # commits "some statement"
# new transaction starts
- connection.execute("<some other statement>")
+ connection.execute(text("<some other statement>"))
connection.rollback() # rolls back "some other statement"
# new transaction starts
- connection.execute("<a third statement>")
+ connection.execute(text("<a third statement>"))
connection.commit() # commits "a third statement"
.. versionadded:: 2.0 "commit as you go" style is a new feature of
isolation_level="REPEATABLE READ"
) as connection:
with connection.begin():
- connection.execute("<statement>")
+ connection.execute(text("<statement>"))
.. tip:: The return value of
the :meth:`_engine.Connection.execution_options` method is the same
with engine.connect() as connection:
connection.execution_options(isolation_level="AUTOCOMMIT")
- connection.execute("<statement>")
- connection.execute("<statement>")
+ connection.execute(text("<statement>"))
+ connection.execute(text("<statement>"))
Above illustrates normal usage of "DBAPI autocommit" mode. There is no
need to make use of methods such as :meth:`_engine.Connection.begin`
# this begin() does not affect the DBAPI connection, isolation stays at AUTOCOMMIT
with connection.begin() as trans:
- connection.execute("<statement>")
- connection.execute("<statement>")
+ connection.execute(text("<statement>"))
+ connection.execute(text("<statement>"))
When we run a block like the above with logging turned on, the logging
will attempt to indicate that while a DBAPI level ``.commit()`` is called,
connection = connection.execution_options(isolation_level="AUTOCOMMIT")
# "transaction" is autobegin (but has no effect due to autocommit)
- connection.execute("<statement>")
+ connection.execute(text("<statement>"))
# this will raise; "transaction" is already begun
with connection.begin() as trans:
- connection.execute("<statement>")
+ connection.execute(text("<statement>"))
The above example also demonstrates the same theme that the "autocommit"
isolation level is a configurational detail of the underlying database
connection.execution_options(isolation_level="AUTOCOMMIT")
# run statement(s) in autocommit mode
- connection.execute("<statement>")
+ connection.execute(text("<statement>"))
# "commit" the autobegun "transaction"
connection.commit()
# use a begin block
with connection.begin() as trans:
- connection.execute("<statement>")
+ connection.execute(text("<statement>"))
Above, to manually revert the isolation level we made use of
:attr:`_engine.Connection.default_isolation_level` to restore the default
# use an autocommit block
with engine.connect().execution_options(isolation_level="AUTOCOMMIT") as connection:
# run statement in autocommit mode
- connection.execute("<statement>")
+ connection.execute(text("<statement>"))
# use a regular block
with engine.begin() as connection:
- connection.execute("<statement>")
+ connection.execute(text("<statement>"))
To sum up:
>>> from sqlalchemy import create_engine
>>> e = create_engine("sqlite://", echo="debug")
>>> with e.connect().execution_options(logging_token="track1") as conn:
- ... conn.execute("select 1").all()
+ ... conn.execute(text("select 1")).all()
2021-02-03 11:48:45,754 INFO sqlalchemy.engine.Engine [track1] select 1
2021-02-03 11:48:45,754 INFO sqlalchemy.engine.Engine [track1] [raw sql] ()
2021-02-03 11:48:45,754 DEBUG sqlalchemy.engine.Engine [track1] Col ('1',)
>>> e1 = e.execution_options(logging_token="track1")
>>> e2 = e.execution_options(logging_token="track2")
>>> with e1.connect() as conn:
- ... conn.execute("select 1").all()
+ ... conn.execute(text("select 1")).all()
2021-02-03 11:51:08,960 INFO sqlalchemy.engine.Engine [track1] select 1
2021-02-03 11:51:08,960 INFO sqlalchemy.engine.Engine [track1] [raw sql] ()
2021-02-03 11:51:08,960 DEBUG sqlalchemy.engine.Engine [track1] Col ('1',)
2021-02-03 11:51:08,961 DEBUG sqlalchemy.engine.Engine [track1] Row (1,)
>>> with e2.connect() as conn:
- ... conn.execute("select 2").all()
+ ... conn.execute(text("select 2")).all()
2021-02-03 11:52:05,518 INFO sqlalchemy.engine.Engine [track2] Select 1
2021-02-03 11:52:05,519 INFO sqlalchemy.engine.Engine [track2] [raw sql] ()
2021-02-03 11:52:05,520 DEBUG sqlalchemy.engine.Engine [track2] Col ('1',)
session.commit() # commits
# will automatically begin again
- result = session.execute("< some select statement >")
+ result = session.execute(text("< some select statement >"))
session.add_all([more_objects, ...])
session.commit() # commits
session.commit() # commits
- result = session.execute("<some SELECT statement>")
+ result = session.execute(text("<some SELECT statement>"))
# remaining transactional state from the .execute() call is
# discarded
with autocommit_session() as session:
- some_objects = session.execute("<statement>")
- some_other_objects = session.execute("<statement>")
+ some_objects = session.execute(text("<statement>"))
+ some_other_objects = session.execute(text("<statement>"))
# closes connection