Transaction & Savepoints
Transaction are safeguard to ensure than a list of operation on a database are only permanent if they can all succeed as atomic action.
In Clear, the usage of transaction is simple:
In the example above, if one of the method fail, the whole transaction block will be reverted to initial state.
Rollback
You can manually rollback a transaction if something went wrong:
In this case, the block will be returned, nothing will be committed in the database and no error will be thrown
Nested transaction
Nested transaction are not working, but save points are used for that. Let's take an example:
In this case, the output will be:
Since nested transaction are not permitted, rollback will rollback the top-most transaction. Any nested transaction block will perform SQL-wise, only the block content will be executed.
Savepoints
For nested transaction, you may want to use save points:
In this case, the output will be:
As you can see, save points are backed by a transaction block; rollback inside a save point block will rollback the block only and not all the transaction. Any unhandled exception will still rollback the full transaction.
Last updated