Appendix: Data Source Transaction Scoping

Any time a data client such as a KnowledgeKube model connects to a data source and interacts with its contents, this event is called a Transaction. The period from when the connection is opened until when it is closed is known as the Scope of that transaction.

When an expression that interacts with a data source is executed, it is automatically assigned to a dedicated transaction. This is known as an implicit transaction. When executed, every such expression will exist in a self-contained transaction, even if expressions that run immediately before or after it connect to the exact same data. Because these transactions are independent of one another, there is no easy way to stop them being executed when one of them encounters an error.

For example, a commercial airline provides an online booking service that lets customers book multiple tickets for flights. Because seats are allocated according to the order in which they are booked, it is important for passengers travelling together to have their seats booked as a block. Otherwise, in the time between transactions on behalf of members of a group, another passenger not in that group might initiate their own transaction, taking the desired seat and separating the group.

Here, a family of three has been separated by another customer's transaction occurring between two of their own.

If all members of a group have their ticket requests processed in a single transaction, anyone not in that group would have their transaction processed after the group's booking concludes.

The family's seating requests are processed together, ensuring no other transactions can affect their booking.

In addition to helping reduce errors, grouping related data source expressions into transactions can help reduce the overhead of applications that frequently interact with databases.

When commands are grouped into a single transaction like this, the result is known as an explicit transaction. KnowledgeKube allows you to create explicit transactions by using one of the following types of compound statement:

Expression Block Type Description Further Reading
WithDB Performs a series of operations on a data source within a single transaction. If any of these operations fail, the transaction will attempt to continue with the remaining operations. Appendix: WithDB Statements
WithDBTrans Performs a series of operations on a data source within a single transaction. If any of these operations fail, the transaction will roll back any changes it has made up to that point and terminate, returning the data source to the state it was in before the transaction began. Appendix: WithDBTrans Statements.

A single transaction is only able to interact with one data source. If you attempt to interact with more than one data source within a single transaction, that transaction will fail at the point the second data source is referenced. As with all errors, in the case of WithDBTrans this will cause the entire transaction to roll back.