Strolch Transactions play a central role in a Strolch agent. A transaction is opened for a realm, and grants
access to the model of the agent. Transactions are implemented as a Java try-with-resources
by
implementing the AutoCloseable
interface. This makes it trivial to understand the scope of a
transaction.
Transactions handle the following:
- Opening and closing database connections
- Releasing locks to strolch elements, if
tx.lock(StrolchRootElement)
- Performing Commands correctly
- exception handling
- auditing
- updating observers
When a transaction is opened, it is by default read-only, i.e. does not perform any commands when it is
closed. Should the TX perform commands, then it is important to call tx.commitOnClose()
, but
only at the end of the work, so that exception handling can properly work if something goes wrong.
StrolchTransaction
offers a myriad of methods:
- accessing the Resources, Orders and Activities
- accessing the element maps
- do queries
- get copies of templates
- add commands for execution
Transactions are opened by accessing the realm, but there are convenience methods depending on the use-case:
- In Services: by calling one of the
openTx()
-methods - In Commands: Transactions are already open, use method
tx()
to get instance - REST API:
RestfulStrolchComponent.openTx()
Important is to always open the transaction as a try-with-resource block and to define if the TX should commit, or not:
try (StrolchTransaction tx = openTx(...)) { // do work // either or: tx.doNothingOnClose(); // can also be omitted, as default tx.commitOnClose(); }