spring-framework / org.springframework.transaction.support / TransactionTemplate

TransactionTemplate

open class TransactionTemplate : DefaultTransactionDefinition, TransactionOperations, InitializingBean

Template class that simplifies programmatic transaction demarcation and transaction exception handling.

The central method is #execute, supporting transactional code that implements the TransactionCallback interface. This template handles the transaction lifecycle and possible exceptions such that neither the TransactionCallback implementation nor the calling code needs to explicitly handle transactions.

Typical usage: Allows for writing low-level data access objects that use resources such as JDBC DataSources but are not transaction-aware themselves. Instead, they can implicitly participate in transactions handled by higher-level application services utilizing this class, making calls to the low-level services via an inner-class callback object.

Can be used within a service implementation via direct instantiation with a transaction manager reference, or get prepared in an application context and passed to services as bean reference. Note: The transaction manager should always be configured as bean in the application context: in the first case given to the service directly, in the second case given to the prepared template.

Supports setting the propagation behavior and the isolation level by name, for convenient configuration in context definitions.

Author
Juergen Hoeller

Since
17.03.2003

See Also
#execute#setTransactionManagerorg.springframework.transaction.PlatformTransactionManager

Constructors

<init>

TransactionTemplate()

Construct a new TransactionTemplate for bean usage.

Note: The PlatformTransactionManager needs to be set before any execute calls.

TransactionTemplate(transactionManager: PlatformTransactionManager)

Construct a new TransactionTemplate using the given transaction manager.

TransactionTemplate(transactionManager: PlatformTransactionManager, transactionDefinition: TransactionDefinition)

Construct a new TransactionTemplate using the given transaction manager, taking its default settings from the given transaction definition.

Inherited Properties

PREFIX_ISOLATION

static val PREFIX_ISOLATION: String

Prefix for the isolation constants defined in TransactionDefinition

PREFIX_PROPAGATION

static val PREFIX_PROPAGATION: String

Prefix for the propagation constants defined in TransactionDefinition

PREFIX_TIMEOUT

static val PREFIX_TIMEOUT: String

Prefix for transaction timeout values in description strings

READ_ONLY_MARKER

static val READ_ONLY_MARKER: String

Marker for read-only transactions in description strings

Functions

afterPropertiesSet

open fun afterPropertiesSet(): Unit

execute

open fun <T : Any> execute(action: TransactionCallback<T>): T

getTransactionManager

open fun getTransactionManager(): PlatformTransactionManager

Return the transaction management strategy to be used.

setTransactionManager

open fun setTransactionManager(transactionManager: PlatformTransactionManager): Unit

Set the transaction management strategy to be used.

Inherited Functions

equals

open fun equals(other: Any?): Boolean

This implementation compares the toString() results.

getIsolationLevel

fun getIsolationLevel(): Int

getName

fun getName(): String

getPropagationBehavior

fun getPropagationBehavior(): Int

getTimeout

fun getTimeout(): Int

hashCode

open fun hashCode(): Int

This implementation returns toString()'s hash code.

isReadOnly

fun isReadOnly(): Boolean

setIsolationLevel

fun setIsolationLevel(isolationLevel: Int): Unit

Set the isolation level. Must be one of the isolation constants in the TransactionDefinition interface. Default is ISOLATION_DEFAULT.

setIsolationLevelName

fun setIsolationLevelName(constantName: String): Unit

Set the isolation level by the name of the corresponding constant in TransactionDefinition, e.g. "ISOLATION_DEFAULT".

setName

fun setName(name: String): Unit

Set the name of this transaction. Default is none.

This will be used as transaction name to be shown in a transaction monitor, if applicable (for example, WebLogic's).

setPropagationBehavior

fun setPropagationBehavior(propagationBehavior: Int): Unit

Set the propagation behavior. Must be one of the propagation constants in the TransactionDefinition interface. Default is PROPAGATION_REQUIRED.

setPropagationBehaviorName

fun setPropagationBehaviorName(constantName: String): Unit

Set the propagation behavior by the name of the corresponding constant in TransactionDefinition, e.g. "PROPAGATION_REQUIRED".

setReadOnly

fun setReadOnly(readOnly: Boolean): Unit

Set whether to optimize as read-only transaction. Default is "false".

setTimeout

fun setTimeout(timeout: Int): Unit

Set the timeout to apply, as number of seconds. Default is TIMEOUT_DEFAULT (-1).

toString

open fun toString(): String

Return an identifying description for this transaction definition.

The format matches the one used by org.springframework.transaction.interceptor.TransactionAttributeEditor, to be able to feed toString results into bean properties of type org.springframework.transaction.interceptor.TransactionAttribute.

Has to be overridden in subclasses for correct equals and hashCode behavior. Alternatively, #equals and #hashCode can be overridden themselves.