If operations occur that are not part of a transaction, then reversing operations must be applied to undo the changes. The reversing operations are performed to reverse the effects of the unwanted operations. This recipe shows how to do that.
Open the BPEL process that performs operations that cannot be rolled back as part of a transaction, and instead requires reversing operations to be applied in the case of a processing failure.
invoke
activity) that has a corresponding reversing operation, wrap it in a scope
activity by dragging a scope from the Component Palette and dropping it just after the operation that requires reversing:assign
and an invoke
) into the compensation handler to reverse the operations in the corresponding scope:assign
and an invoke
) into the compensation handler to reverse the operations in the corresponding scope.When a fault
is caught by catch
, compensate
it will cause all the operations that require reversing to be reversed.
Compensate
can only be called from within a catch
block. When it is called, it starts with the most recently completed scope
and calls the compensation handler
for that scope
if it has one. It then looks for the previously completed scope
and calls the compensation handler
for that scope
. In this way, the reversing operations are applied in the reverse order to the original operations. If a scope was not completed, its compensation handler will not be invoked.
If an operation throws a fault
, we can catch
it and call the Compensate activity. Operations may indicate failure by returning a failure status rather than throwing a fault
. In this case, by placing a Throw activity inside the scope
for which the operation failed, we can avoid the reversing operation being invoked for that scope
.
Compensation occurs outside of transaction boundaries. So a BPEL process may be spread across several transactions, but compensation ignores this and continues to invoke compensation handlers for completed scopes regardless of the transaction context in which they were completed.