4.4.5.4. Transaction Timeout

You can set timeout in seconds for created transaction. When the timeout is exceeded, transaction will be 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 the Persistence.createTransaction(). method. For example:

Transaction tx = persistence.createTransaction(new TransactionParams().setTimeout(2));

In case of declarative transactions management, timeout parameter of the @Transactional annotation can be used, for example:

@Transactional(timeout = 2)
public void someServiceMethod() {
...

The default timeout can be defined using cuba.defaultQueryTimeoutSec application property.

Implementation Specifics for Different DBMS

PostgreSQL

Unfortunately, JDBC driver for PostgreSQL does not support the setQueryTimeout() method of the java.sql.Statement interface. For this reason an extra command set local statement_timeout to {value} is executed in DB at the beginning of each transaction, for which timeout has been set using any approach, including a non-zero value of cuba.defaultQueryTimeoutSec. In this case, the DB server will interrupt the request itself if the timeout is exceeded.

We recommend the following to decrease the load generated by these extra operators:

  • Default timeout should be set not on Middleware using cuba.defaultQueryTimeoutSec property, but on PostgreSQL server in the file postgresql.conf. For example, statement_timeout = 3000 (in milliseconds).

  • For methods which need a longer timeout (reports and similar) the timeout should be specified explicitly in transaction parameters.

Microsoft SQL Server

JTDS driver supports the setQueryTimeout() method of the java.sql.Statement interface, so you can just set the standard EntityManager property javax.persistence.query.timeout, which will automatically apply to JDBC queries.