Appendix: WithDBTrans Statements
WithDBTrans is a type of compound statement used to wrap one or more expressions related to the modification of a data source, causing those expressions to be executed as part of a single explicit transaction. In any given WithDBTrans statement during run-time, the first expression to connect to a data source - such as a WriteData expression - will initiate a transaction between the model and the target data source. Assuming no error occurs, this transaction will remain active until the last expression inside the WithDBTrans statement has been executed.
The syntax of a WithDBTrans statement is as follows:
WithDBTrans
{
SomeExpressionCode;
};
If any part of the WithDBTrans statement fails to complete, the active transaction will be aborted and all preceding changes to the database made by that transaction will be rolled back. Essentially, if something goes wrong during a transaction, the target database should return to the state it was in when the transaction began.
Rollback only affects expressions that directly modify the contents of a data table, including WriteData and DeleteData. Other types of expression can be executed as part of a transaction, but will not be rolled back if the transaction is aborted. Additionally, the rollback will not affect any changes to the database made using the RunStoredProcedure function.
The following diagram shows an example WithDBTrans transaction that attempts to write three new values into a database. The second value is invalid, meaning the expression responsible for writing that value fails. As soon as this happens, KnowledgeKube rolls back all changes made since the start of the transaction, then terminates the transaction itself. As a result, the value written away by the first expression is removed, restoring the database to its initial state:
A WithDBTrans transaction that experiences an error. The transaction fails to complete and the database is rolled back.