4D v16.3Suspending transactions |
||
|
4D v16.3
Suspending transactions
Suspending transactions
Suspending a transaction is useful when you need to perform, from within a transaction, certain operations that do not need to be executed under the control of this transaction. For example, imagine the case where a customer places an order, thus within a transaction, and also updates their address. Next the customer changes their mind and cancels the order. The transaction is cancelled, but you do not want the address change to be reverted. This is a typical example where suspending the transaction is useful. Three commands are used to suspend and resume transactions:
This example illustrates the need for a suspended transaction. In an Invoices database, we want to get a new invoice number during a transaction. This number is computed and stored in a [Settings] table. In a multi-user environment, concurrent accesses must be protected; however, because of the transaction, the [Settings] table could be locked by another user even though this data is independent from the main transaction. In this case, you can suspend the transaction when accessing the table. //Standard method that creates an invoice The GetInvoiceNum method suspends the transaction before executing. Note that this code will work even when the method is called from outside of a transaction: //GetInvoiceNum project method When a transaction is suspended, the following principles are implemented:
These implementations are summarized in the following graphic: Values edited during transaction A (ID1 record gets Val11) are not available in a new transaction (B) created during the "suspended" period. Values edited during the "suspended" period (ID2 record gets Val22 and ID3 record gets Val33) are saved even after transaction A is cancelled. Specific features have been added to handle errors:
The existing In transaction command returns True when a transaction has been started, even if it is suspended. To find out whether the current transaction is suspended, you need to use the new Transaction active command, which returns False in this case. Both commands, however, also return False if no transaction has been started. You may then need to use the existing Transaction level command, which returns 0 in this context (no transaction started). The following graphic illustrates the various transaction contexts and the corresponding values returned by the transaction commands:
See also
|
PROPERTIES
Product: 4D
HISTORY
Created: 4D v15 R4 ARTICLE USAGE
4D Language Reference ( 4D v16) |