Ticket #9206: extra_v2.diff

File extra_v2.diff, 2.6 KB (added by Richard Davies <richard.davies@…>, 16 years ago)

New draft of extra piece

  • docs/topics/db/transactions.txt

     
    166166
    167167.. _information on MySQL transactions: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-transactions.html
    168168
    169 Transactions and savepoints in PostgreSQL 8
    170 ===========================================
     169Database exceptions within PostgreSQL transactions
     170==================================================
    171171
    172 When a call to a PostgreSQL 8 cursor raises an exception, all subsequent SQL
     172When a call to a PostgreSQL cursor raises an exception, all subsequent SQL
    173173in the same transaction fails with the error "current transaction is aborted,
    174174queries ignored until end of transaction block". Whilst simple use of save()
    175175is unlikely to raise an exception in PostgreSQL, there are many more advanced
    176176usage patterns which might: for example, saving objects with unique fields,
    177177saving using the force_insert/force_update flag, or invoking custom SQL.
     178There are three possible solutions to this issue:
    178179
    179 In any of these cases, you can wrap the command which may throw
    180 IntegrityError inside savepoints, which will then allow subsequent commands
    181 to proceed. Example::
     180    * With any version of PostgreSQL, you can rollback to the start of the
     181      transaction if you experience an IntegrityError.
    182182
    183     try:
    184       sid = transaction.savepoint()
    185       x.save()
    186       transaction.savepoint_commit(sid)
    187     except IntegrityError:
    188       transaction.savepoint_rollback(sid)
    189       raise
     183    * With PostgreSQL 8 or later, you can wrap the command which may throw
     184      IntegrityError inside savepoints, which will then allow subsequent
     185      commands to proceed.  Example::
    190186
    191 Savepoints are not implemented in PostgreSQL 7. If you experience an
    192 IntegrityError when using PostgreSQL 7, you will need to rollback to the
    193 start of the transaction.
     187          try:
     188            sid = transaction.savepoint()
     189            x.save()
     190            transaction.savepoint_commit(sid)
     191          except IntegrityError:
     192            transaction.savepoint_rollback(sid)
     193            raise
     194
     195.. versionadded:: 1.1
     196
     197    * With PostgreSQL 8.2 or later, there is an advanced option to run
     198      PostgreSQL with :ref:`database-level autocommit <ref-databases>`.
     199      With this option there is no constantly open transaction, so
     200      savepoints are not required to continue after catching an exception.
     201      Please see the documentation for that feature, which behaves
     202      differently from the standard ``autocommit`` decorator.
Back to Top