Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Handling Transaction Timeouts | Advanced Transaction Scenarios
Transactions in Spring

bookHandling Transaction Timeouts

Deslize para mostrar o menu

In this chapter, you explore how Spring enforces transaction time limits at runtime. You will examine the internal mechanisms Spring uses to monitor and control transaction durations. The focus is on how Spring detects timeouts, what happens when a transaction exceeds its allowed time, and how these processes affect your application's behavior. By understanding these runtime details, you can better anticipate and manage transaction boundaries in your Spring applications.

Passing Timeout Values to the Transaction Manager

In Spring, you control transaction timeouts using the timeout property in transaction definitions. This value tells the transaction manager how long a transaction can run before it is automatically rolled back.

Setting Timeout in Declarative Transactions

  • Use the @Transactional annotation's timeout attribute to specify the maximum duration in seconds;
  • The value is passed to the underlying transaction manager when the transaction begins;
  • If the transaction exceeds this duration, the transaction manager marks it for rollback.

Example:

@Transactional(timeout = 10) // Transaction will timeout after 10 seconds
public void processOrder() {
    // business logic
}

Setting Timeout in Programmatic Transactions

  • Use the TransactionDefinition interface's setTimeout method to configure the timeout;
  • Pass the configured TransactionDefinition to the PlatformTransactionManager when starting a transaction.

Example:

DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setTimeout(5); // Timeout after 5 seconds
TransactionStatus status = transactionManager.getTransaction(def);
try {
    // business logic
    transactionManager.commit(status);
} catch (Exception ex) {
    transactionManager.rollback(status);
}

Internal Mechanisms

  • When a transaction starts, Spring creates a TransactionDefinition object containing the timeout value;
  • The PlatformTransactionManager implementation (such as DataSourceTransactionManager or JpaTransactionManager) receives this object and passes the timeout to the underlying resource (such as JDBC or JPA);
  • For JDBC, the transaction manager sets the timeout on the java.sql.Statement or via connection properties;
  • If the resource layer does not support timeouts natively, Spring tracks the elapsed time and marks the transaction as rollback-only if the timeout is exceeded.

Setting an appropriate timeout helps prevent resource leaks and ensures your application remains responsive under heavy load.

question mark

What does Spring do internally when a transaction timeout is exceeded during execution?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 2. Capítulo 4
some-alt