4.4.5.4. Transaction Parameters
- Transaction Timeout
-
You can set a timeout in seconds for created transaction. When the timeout is exceeded, the transaction is interrupted and rolled back. Transaction timeout effectively limits the maximum duration of a database request.
When transactions are managed programmatically, the timeout is specified by passing
TransactionParams
object to thePersistence.createTransaction()
method. For example:Transaction tx = persistence.createTransaction(new TransactionParams().setTimeout(2));
In case of declarative transactions management, use the
timeout
parameter of the@Transactional
annotation:@Transactional(timeout = 2) public void someServiceMethod() { ...
The default timeout can be defined using the cuba.defaultQueryTimeoutSec application property.
- Read-only Transactions
-
A transaction can be marked as read-only if it is intended only for reading data from the database. For example, all
load
methods of DataManager use read-only transactions by default. Read-only transactions yield better performance because the platform does not execute code that handles possible entity modifications.BeforeCommit
transaction listeners are not invoked as well.WarningIf the persistence context of a read-only transaction contains modified entities,
IllegalStateException
will be thrown on attempt to commit the transaction. It means that you should mark a transaction as read-only only when you are sure that it doesn’t modify any entity.When transactions are managed programmatically, the read-only sign is specified by passing
TransactionParams
object to thePersistence.createTransaction()
method. For example:Transaction tx = persistence.createTransaction(new TransactionParams().setReadOnly(true));
In case of declarative transactions management, use the
readOnly
parameter of the@Transactional
annotation:@Transactional(readOnly = true) public void someServiceMethod() { ...