Transactions and Connection Management

Managing Transactions

A newly constructed Session may be said to be in the “begin” state.In this state, the Session has not established any connection ortransactional state with any of the Engine objects that may be associatedwith it.

The Session then receives requests to operate upon a database connection.Typically, this means it is called upon to execute SQL statements using a particularEngine, which may be via Session.query(), Session.execute(),or within a flush operation of pending data, which occurs when such state existsand Session.commit() or Session.flush() is called.

As these requests are received, each new Engine encountered is associatedwith an ongoing transactional state maintained by the Session.When the first Engine is operated upon, the Session can be saidto have left the “begin” state and entered “transactional” state. For eachEngine encountered, a Connection is associated with it,which is acquired via the Engine.contextual_connect() method. If aConnection was directly associated with the Session (see Joining a Session into an External Transaction (such as for test suites)for an example of this), it isadded to the transactional state directly.

For each Connection, the Session also maintains a Transaction object,which is acquired by calling Connection.begin() on each Connection,or if the Sessionobject has been established using the flag twophase=True, a TwoPhaseTransactionobject acquired via Connection.begin_twophase(). These transactions are all committed orrolled back corresponding to the invocation of theSession.commit() and Session.rollback() methods. A commit operation willalso call the TwoPhaseTransaction.prepare() method on all transactions if applicable.

When the transactional state is completed after a rollback or commit, the Sessionreleases all Transaction and Connection resources,and goes back to the “begin” state, whichwill again invoke new Connection and Transaction objects as newrequests to emit SQL statements are received.

The example below illustrates this lifecycle:

  1. engine = create_engine("...")
  2. Session = sessionmaker(bind=engine)
  3.  
  4. # new session. no connections are in use.
  5. session = Session()
  6. try:
  7. # first query. a Connection is acquired
  8. # from the Engine, and a Transaction
  9. # started.
  10. item1 = session.query(Item).get(1)
  11.  
  12. # second query. the same Connection/Transaction
  13. # are used.
  14. item2 = session.query(Item).get(2)
  15.  
  16. # pending changes are created.
  17. item1.foo = 'bar'
  18. item2.bar = 'foo'
  19.  
  20. # commit. The pending changes above
  21. # are flushed via flush(), the Transaction
  22. # is committed, the Connection object closed
  23. # and discarded, the underlying DBAPI connection
  24. # returned to the connection pool.
  25. session.commit()
  26. except:
  27. # on rollback, the same closure of state
  28. # as that of commit proceeds.
  29. session.rollback()
  30. raise
  31. finally:
  32. # close the Session. This will expunge any remaining
  33. # objects as well as reset any existing SessionTransaction
  34. # state. Neither of these steps are usually essential.
  35. # However, if the commit() or rollback() itself experienced
  36. # an unanticipated internal failure (such as due to a mis-behaved
  37. # user-defined event handler), .close() will ensure that
  38. # invalid state is removed.
  39. session.close()

Using SAVEPOINT

SAVEPOINT transactions, if supported by the underlying engine, may bedelineated using the begin_nested()method:

  1. Session = sessionmaker()
  2. session = Session()
  3. session.add(u1)
  4. session.add(u2)
  5.  
  6. session.begin_nested() # establish a savepoint
  7. session.add(u3)
  8. session.rollback() # rolls back u3, keeps u1 and u2
  9.  
  10. session.commit() # commits u1 and u2

begin_nested() may be called any numberof times, which will issue a new SAVEPOINT with a unique identifier for eachcall. For each begin_nested() call, acorresponding rollback() orcommit() must be issued. (But note that if the return value isused as a context manager, i.e. in a with-statement, then this rollback/commitis issued by the context manager upon exiting the context, and so should not beadded explicitly.)

When begin_nested() is called, aflush() is unconditionally issued(regardless of the autoflush setting). This is so that when arollback() occurs, the full state of thesession is expired, thus causing all subsequent attribute/instance access toreference the full state of the Session rightbefore begin_nested() was called.

begin_nested(), in the same manner as the less oftenused begin() method, returns a SessionTransaction objectwhich works as a context manager.It can be succinctly used around individual record inserts in order to catchthings like unique constraint exceptions:

  1. for record in records:
  2. try:
  3. with session.begin_nested():
  4. session.merge(record)
  5. except:
  6. print("Skipped record %s" % record)
  7. session.commit()

Autocommit Mode

The examples of session lifecycle at Managing Transactions referto a Session that runs in its default mode of autocommit=False.In this mode, the Session begins new transactions automaticallyas soon as it needs to do work upon a database connection; the transactionthen stays in progress until the Session.commit() or Session.rollback()methods are called.

The Session also features an older legacy mode of use calledautocommit mode, where a transaction is not started implicitly, and unlessthe Session.begin() method is invoked, the Session willperform each database operation on a new connection checked out from theconnection pool, which is then released back to the pool immediatelyafter the operation completes. This refers tomethods like Session.execute() as well as when executing a queryreturned by Session.query(). For a flush operation, the Sessionstarts a new transaction for the duration of the flush, and commits it whencomplete.

Warning

“autocommit” mode is a legacy mode of use and should not beconsidered for new projects. If autocommit mode is used, it is stronglyadvised that the application at least ensure that transaction scopeis made present via the Session.begin() method, rather thanusing the session in pure autocommit mode.

If the Session.begin() method is not used, and operations are allowedto proceed using ad-hoc connections with immediate autocommit, then theapplication probably should set autoflush=False, expire_on_commit=False,since these features are intended to be used only within the contextof a database transaction.

Modern usage of “autocommit mode” tends to be for framework integrations thatwish to control specifically when the “begin” state occurs. A session which isconfigured with autocommit=True may be placed into the “begin” state usingthe Session.begin() method. After the cycle completes uponSession.commit() or Session.rollback(), connection andtransaction resources are released and the Session goes backinto “autocommit” mode, until Session.begin() is called again:

  1. Session = sessionmaker(bind=engine, autocommit=True)
  2. session = Session()
  3. session.begin()
  4. try:
  5. item1 = session.query(Item).get(1)
  6. item2 = session.query(Item).get(2)
  7. item1.foo = 'bar'
  8. item2.bar = 'foo'
  9. session.commit()
  10. except:
  11. session.rollback()
  12. raise

The Session.begin() method also returns a transactional token which iscompatible with the with statement:

  1. Session = sessionmaker(bind=engine, autocommit=True)
  2. session = Session()
  3. with session.begin():
  4. item1 = session.query(Item).get(1)
  5. item2 = session.query(Item).get(2)
  6. item1.foo = 'bar'
  7. item2.bar = 'foo'

Using Subtransactions with Autocommit

A subtransaction indicates usage of the Session.begin() method in conjunction withthe subtransactions=True flag. This produces a non-transactional, delimiting construct thatallows nesting of calls to begin() and commit().Its purpose is to allow the construction of code that can function within a transactionboth independently of any external code that starts a transaction,as well as within a block that has already demarcated a transaction.

subtransactions=True is generally only useful in conjunction withautocommit, and is equivalent to the pattern described at Nesting of Transaction Blocks,where any number of functions can call Connection.begin() and Transaction.commit()as though they are the initiator of the transaction, but in fact may be participatingin an already ongoing transaction:

  1. # method_a starts a transaction and calls method_b
  2. def method_a(session):
  3. session.begin(subtransactions=True)
  4. try:
  5. method_b(session)
  6. session.commit() # transaction is committed here
  7. except:
  8. session.rollback() # rolls back the transaction
  9. raise
  10.  
  11. # method_b also starts a transaction, but when
  12. # called from method_a participates in the ongoing
  13. # transaction.
  14. def method_b(session):
  15. session.begin(subtransactions=True)
  16. try:
  17. session.add(SomeObject('bat', 'lala'))
  18. session.commit() # transaction is not committed yet
  19. except:
  20. session.rollback() # rolls back the transaction, in this case
  21. # the one that was initiated in method_a().
  22. raise
  23.  
  24. # create a Session and call method_a
  25. session = Session(autocommit=True)
  26. method_a(session)
  27. session.close()

Subtransactions are used by the Session.flush() process to ensure that theflush operation takes place within a transaction, regardless of autocommit. Whenautocommit is disabled, it is still useful in that it forces the Sessioninto a “pending rollback” state, as a failed flush cannot be resumed in mid-operation,where the end user still maintains the “scope” of the transaction overall.

Enabling Two-Phase Commit

For backends which support two-phase operation (currently MySQL andPostgreSQL), the session can be instructed to use two-phase commit semantics.This will coordinate the committing of transactions across databases so thatthe transaction is either committed or rolled back in all databases. You canalso prepare() the session forinteracting with transactions not managed by SQLAlchemy. To use two phasetransactions set the flag twophase=True on the session:

  1. engine1 = create_engine('postgresql://db1')
  2. engine2 = create_engine('postgresql://db2')
  3.  
  4. Session = sessionmaker(twophase=True)
  5.  
  6. # bind User operations to engine 1, Account operations to engine 2
  7. Session.configure(binds={User:engine1, Account:engine2})
  8.  
  9. session = Session()
  10.  
  11. # .... work with accounts and users
  12.  
  13. # commit. session will issue a flush to all DBs, and a prepare step to all DBs,
  14. # before committing both transactions
  15. session.commit()

Setting Transaction Isolation Levels

Isolation refers to the behavior of the transaction at the databaselevel in relation to other transactions occurring concurrently. Thereare four well-known modes of isolation, and typically the Python DBAPIallows these to be set on a per-connection basis, either through explicitAPIs or via database-specific calls.

SQLAlchemy’s dialects support settable isolation modes on a per-Engineor per-Connection basis, using flags at both thecreate_engine() level as well as at the Connection.execution_options()level.

When using the ORM Session, it acts as a facade for engines andconnections, but does not expose transaction isolation directly. So inorder to affect transaction isolation level, we need to act upon theEngine or Connection as appropriate.

See also

create_engine.isolation_level

SQLite Transaction Isolation

PostgreSQL Isolation Level

MySQL Isolation Level

Setting Isolation Engine-Wide

To set up a Session or sessionmaker with a specificisolation level globally, use the create_engine.isolation_levelparameter:

  1. from sqlalchemy import create_engine
  2. from sqlalchemy.orm import sessionmaker
  3.  
  4. eng = create_engine(
  5. "postgresql://scott:tiger@localhost/test",
  6. isolation_level='REPEATABLE_READ')
  7.  
  8. maker = sessionmaker(bind=eng)
  9.  
  10. session = maker()

Setting Isolation for Individual Sessions

When we make a new Session, either using the constructor directlyor when we call upon the callable produced by a sessionmaker,we can pass the bind argument directly, overriding the pre-existing bind.We can combine this with the Engine.execution_options() methodin order to produce a copy of the original Engine that willadd this option:

  1. session = maker(
  2. bind=engine.execution_options(isolation_level='SERIALIZABLE'))

For the case where the Session or sessionmaker isconfigured with multiple “binds”, we can either re-specify the bindsargument fully, or if we want to only replace specific binds, wecan use the Session.bind_mapper() or Session.bind_table()methods:

  1. session = maker()
  2. session.bind_mapper(
  3. User, user_engine.execution_options(isolation_level='SERIALIZABLE'))

We can also use the individual transaction method that follows.

Setting Isolation for Individual Transactions

A key caveat regarding isolation level is that the setting cannot besafely modified on a Connection where a transaction has alreadystarted. Databases cannot change the isolation level of a transactionin progress, and some DBAPIs and SQLAlchemy dialectshave inconsistent behaviors in this area. Some may implicitly emit aROLLBACK and some may implicitly emit a COMMIT, others may ignore the settinguntil the next transaction. Therefore SQLAlchemy emits a warning if thisoption is set when a transaction is already in play. The Sessionobject does not provide for us a Connection for use in a transactionwhere the transaction is not already begun. So here, we need to passexecution options to the Session at the start of a transactionby passing Session.connection.execution_optionsprovided by the Session.connection() method:

  1. from sqlalchemy.orm import Session
  2.  
  3. sess = Session(bind=engine)
  4. sess.connection(execution_options={'isolation_level': 'SERIALIZABLE'})
  5.  
  6. # work with session
  7.  
  8. # commit transaction. the connection is released
  9. # and reverted to its previous isolation level.
  10. sess.commit()

Above, we first produce a Session using either the constructoror a sessionmaker. Then we explicitly set up the start ofa transaction by calling upon Session.connection(), which providesfor execution options that will be passed to the connection before thetransaction is begun. If we are working with a Session thathas multiple binds or some other custom scheme for Session.get_bind(),we can pass additional arguments to Session.connection() in order toaffect how the bind is procured:

  1. sess = my_sesssionmaker()
  2.  
  3. # set up a transaction for the bind associated with
  4. # the User mapper
  5. sess.connection(
  6. mapper=User,
  7. execution_options={'isolation_level': 'SERIALIZABLE'})
  8.  
  9. # work with session
  10.  
  11. # commit transaction. the connection is released
  12. # and reverted to its previous isolation level.
  13. sess.commit()

The Session.connection.execution_options argument is onlyaccepted on the first call to Session.connection() for aparticular bind within a transaction. If a transaction is already begunon the target connection, a warning is emitted:

  1. >>> session = Session(eng)
  2. >>> session.execute("select 1")
  3. <sqlalchemy.engine.result.ResultProxy object at 0x1017a6c50>
  4. >>> session.connection(execution_options={'isolation_level': 'SERIALIZABLE'})
  5. sqlalchemy/orm/session.py:310: SAWarning: Connection is already established
  6. for the given bind; execution_options ignored

New in version 0.9.9: Added theSession.connection.execution_optionsparameter to Session.connection().

Tracking Transaction State with Events

See the section Transaction Events for an overviewof the available event hooks for session transaction state changes.

Joining a Session into an External Transaction (such as for test suites)

If a Connection is being used which is already in a transactionalstate (i.e. has a Transaction established), a Session canbe made to participate within that transaction by just binding theSession to that Connection. The usual rationale for thisis a test suite that allows ORM code to work freely with a Session,including the ability to call Session.commit(), where afterwards theentire database interaction is rolled back:

  1. from sqlalchemy.orm import sessionmaker
  2. from sqlalchemy import create_engine
  3. from unittest import TestCase
  4.  
  5. # global application scope. create Session class, engine
  6. Session = sessionmaker()
  7.  
  8. engine = create_engine('postgresql://...')
  9.  
  10. class SomeTest(TestCase):
  11. def setUp(self):
  12. # connect to the database
  13. self.connection = engine.connect()
  14.  
  15. # begin a non-ORM transaction
  16. self.trans = self.connection.begin()
  17.  
  18. # bind an individual Session to the connection
  19. self.session = Session(bind=self.connection)
  20.  
  21. def test_something(self):
  22. # use the session in tests.
  23.  
  24. self.session.add(Foo())
  25. self.session.commit()
  26.  
  27. def tearDown(self):
  28. self.session.close()
  29.  
  30. # rollback - everything that happened with the
  31. # Session above (including calls to commit())
  32. # is rolled back.
  33. self.trans.rollback()
  34.  
  35. # return connection to the Engine
  36. self.connection.close()

Above, we issue Session.commit() as well asTransaction.rollback(). This is an example of where we take advantageof the Connection object’s ability to maintain subtransactions, ornested begin/commit-or-rollback pairs where only the outermost begin/commitpair actually commits the transaction, or if the outermost block rolls back,everything is rolled back.

Supporting Tests with Rollbacks

The above recipe works well for any kind of database enabled test, exceptfor a test that needs to actually invoke Session.rollback() withinthe scope of the test itself. The above recipe can be expanded, suchthat the Session always runs all operations within the scopeof a SAVEPOINT, which is established at the start of each transaction,so that tests can also rollback the “transaction” as well while stillremaining in the scope of a larger “transaction” that’s never committed,using two extra events:

  1. from sqlalchemy import event
  2.  
  3.  
  4. class SomeTest(TestCase):
  5.  
  6. def setUp(self):
  7. # connect to the database
  8. self.connection = engine.connect()
  9.  
  10. # begin a non-ORM transaction
  11. self.trans = connection.begin()
  12.  
  13. # bind an individual Session to the connection
  14. self.session = Session(bind=self.connection)
  15.  
  16. # start the session in a SAVEPOINT...
  17. self.session.begin_nested()
  18.  
  19. # then each time that SAVEPOINT ends, reopen it
  20. @event.listens_for(self.session, "after_transaction_end")
  21. def restart_savepoint(session, transaction):
  22. if transaction.nested and not transaction._parent.nested:
  23.  
  24. # ensure that state is expired the way
  25. # session.commit() at the top level normally does
  26. # (optional step)
  27. session.expire_all()
  28.  
  29. session.begin_nested()
  30.  
  31. # ... the tearDown() method stays the same