2.4. PlatformTransactionManager Interface
Overview
The
PlatformTransactionManager
interface is the key abstraction in the Spring transaction API, providing the classic transaction client operations: begin, commit and rollback. This interface thus provides the essential methods for controlling transactions at run time.
Note
The other key aspect of any transaction system is the API for implementing transactional resources. But transactional resources are generally implemented by the underlying database, so this aspect of transactional programming is rarely a concern for the application programmer.
PlatformTransactionManager interface
Example 2.1, “The PlatformTransactionManager Interface” shows the definition of the
org.springframework.transaction.PlatformTransactionManager
interface.
Example 2.1. The PlatformTransactionManager Interface
package org.springframework.transaction; public interface PlatformTransactionManager { TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException; void commit(TransactionStatus status) throws TransactionException; void rollback(TransactionStatus status) throws TransactionException; }
TransactionDefinition interface
The
TransactionDefinition
interface is used to specify the characteristics of a newly created transaction. It is used to specify the isolation level and the propagation policy of the new transaction. For more details, see Section 5.3, “Propagation Policies”.
TransactionStatus interface
The
TransactionStatus
interface can be used to check the status of the current transaction (that is, the transaction associated with the current thread) and to mark the current transaction for rollback. It is defined as follows:
public interface TransactionStatus extends SavepointManager { boolean isNewTransaction(); boolean hasSavepoint(); void setRollbackOnly(); boolean isRollbackOnly(); void flush(); boolean isCompleted(); }
Using the PlatformTransactionManager interface
The
PlatformTransactionManager
interface defines the following methods:
-
getTransaction()
- Create a new transaction and associate it with the current thread, passing in a
TransactionDefinition
object to define the characteristics of the new transaction. This is analogous to the begin() method of many other transaction client APIs. -
commit()
- Commit the current transaction, making permanent all of the pending changes to the registered resources.
-
rollback()
- Roll back the current transaction, undoing all of the pending changes to the registered resources.
Generally, you do not use the
PlatformTransactionManager
interface directly. In Apache Camel, you typically use a transaction manager as follows:
- Create an instance of a transaction manager (there are several different implementations available in Spring—see Section 2.5, “Transaction Manager Implementations”).
- Pass the transaction manager instance either to a Apache Camel component or to the
transacted()
DSL command in a route. The transactional component or thetransacted()
command is then responsible for demarcating transactions (see Chapter 5, Transaction Demarcation).