| 153 | Savepoints |
| 154 | ========== |
| 155 | |
| 156 | A savepoint is a marker within an open transaction that enables partial |
| 157 | rollback to the point when it was created. |
| 158 | |
| 159 | Under the default ``autocommit`` decorator this is not very useful, since |
| 160 | the open transaction is regularly committed in its entirety. However under |
| 161 | ``commit_on_success`` or ``commit_manually`` the open transaction will |
| 162 | build up multiple database operations, so savepoints can provide more |
| 163 | fine-grained rollback than a full ``transaction.rollback()``. Example:: |
| 164 | |
| 165 | from django.db import transaction |
| 166 | |
| 167 | @transaction.commit_manually |
| 168 | def viewfunc(request): |
| 169 | |
| 170 | a.save() |
| 171 | # open transaction now contains a.save() |
| 172 | sid = transaction.savepoint() |
| 173 | |
| 174 | b.save() |
| 175 | # open transaction now contains a.save() and b.save() |
| 176 | |
| 177 | if want_to_keep_b: |
| 178 | transaction.savepoint_commit(sid) |
| 179 | # open transaction still contains a.save() and b.save() |
| 180 | |
| 181 | else: |
| 182 | transaction.savepoint_rollback(sid) |
| 183 | # open transaction now contains only a.save() |
| 184 | |
| 185 | transaction.commit() |
| 186 | |
| 187 | Savepoints are implemented in the PostgreSQL 8 and Oracle backends. |
| 188 | |
172 | | When a call to a PostgreSQL 8 cursor raises an exception, all subsequent SQL |
173 | | in the same transaction fails with the error "current transaction is aborted, |
174 | | queries ignored until end of transaction block". Whilst simple use of save() |
175 | | is unlikely to raise an exception in PostgreSQL, there are many more advanced |
176 | | usage patterns which might: for example, saving objects with unique fields, |
177 | | saving using the force_insert/force_update flag, or invoking custom SQL. |
| 208 | When a call to a PostgreSQL cursor raises an exception (typically |
| 209 | ``IntegrityError``), all subsequent SQL in the same transaction fails with |
| 210 | the error "current transaction is aborted, queries ignored until end of |
| 211 | transaction block". Whilst simple use of ``save()`` is unlikely to raise an |
| 212 | exception in PostgreSQL, there are many more advanced usage patterns which |
| 213 | might: for example, saving objects with unique fields, saving using the |
| 214 | force_insert/force_update flag, or invoking custom SQL. Example:: |
| 235 | b.save() # throws exception due to unique field |
| 236 | except: |
| 237 | transaction.rollback() |
| 238 | c.save() # succeeds, but a.save() may have been undone |
| 239 | |
| 240 | Note that under the ``commit_on_success`` or ``commit_manually`` |
| 241 | decorators the transaction rollback will also undo ``a.save()``. |
| 242 | |
| 243 | Savepoint rollback |
| 244 | ------------------ |
| 245 | |
| 246 | With PostgreSQL 8 or later, you can wrap just the command which may raise an |
| 247 | exception inside savepoints, isolating the rollback. Example:: |
| 248 | |
| 249 | a.save() # succeeds, and never undone by savepoint rollback |
| 250 | try: |
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. |
| 258 | Note that ``a.save()`` is never undone. |
| 259 | |
| 260 | Database-level autocommit |
| 261 | ------------------------- |
| 262 | |
| 263 | .. versionadded:: 1.1 |
| 264 | |
| 265 | With PostgreSQL 8.2 or later, there is an advanced option to run PostgreSQL |
| 266 | with :ref:`database-level autocommit <ref-databases>`. With this option |
| 267 | there is no constantly open transaction, so it is always possible to continue |
| 268 | after catching an exception. Please see the documentation for that feature, |
| 269 | which behaves differently from the standard ``autocommit`` decorator. |
| 270 | Example:: |
| 271 | |
| 272 | a.save() # succeeds |
| 273 | try: |
| 274 | b.save() # throws exception due to unique field |
| 275 | except: |
| 276 | pass |
| 277 | c.save() # succeeds |