Spring declarative transaction should be one of the most widely used features in Spring applications, which greatly simplifies database operations. Currently, Spring supports two configuration methods for declarative transactions: XML and annotation.
For configuration, the XML configuration will be troublesome, especially for the configuration of transaction attributes. Therefore, I would like to add a description of the configuration in this section and share it with you, hoping to help you.
The following is a common method for Spring XML transaction configuration, which is also the way in which we use XML transaction configuration. This article takes this configuration as an example to describe it.
<Bean id = "transactionInterceptor"
Class = "org. springframework. transaction. interceptor. TransactionInterceptor">
<Property name = "transactionManager" ref = "transactionManager"/>
<Property name = "transactionAttributes">
<Props>
<Prop key = "query *"> PROPAGATION_REQUIRED, readOnly </prop>
<Prop key = "is *"> PROPAGATION_REQUIRED, readOnly </prop>
<Prop key = "find *"> PROPAGATION_REQUIRED, readOnly </prop>
<Prop key = "get *"> PROPAGATION_SUPPORTS, readOnly </prop>
<Prop key = "load *"> PROPAGATION_REQUIRED, readOnly </prop>
<Prop key = "*"> PROPAGATION_REQUIRED </prop>
</Props>
</Property>
</Bean>
Configuration details are as follows:
TransactionInterceptor is a declarative transaction interceptor. Callback can be performed by the Spring AOP interceptor.
TransactionAttributes is a description of the transaction attribute configuration, which is also a place that people think is not easy to remember
Its configuration rules are as follows: because they all have prefix identifiers, the order can be modified.
(PROPAGATION_NAME, ISOLATION_NAME, readOnly, timeout_NNNN, + prediction1,-prediction2)
The PROPAGATION level of the PROPAGATION_REQUIRED transaction (starting with PROPAGATION _). The configurable values are described as follows:
PROPAGATION_REQUIRED: use the current transaction. If no transaction exists, start the new transaction.
PROPAGATION_SUPPORTS is used when there is a transaction, and no transaction is needed
PROPAGATION_MANDATORY is used when a transaction exists. If no transaction exists, an error is returned (transactions must be used)
PROPAGATION_REQUIRES_NEW if a transaction exists, the original transaction is suspended and the new transaction is started (new transactions are required)
PROPAGATION_NOT_SUPPORTED does not use transactions. If a transaction exists, the transaction is suspended.
PROPAGATION_NEVER cannot have a transaction. If there is a transaction currently, an error is reported (no transaction is required)
PROPAGATION_NESTED is the same as PROPAGATION_REQUIRED
ISOLATION_DEFAULT transaction ISOLATION level (beginning with ISOLATION)
ISOLATION_DEFAULT uses the default Database Transaction
ISOLATION_READ_UNCOMMITTED
ISOLATION_READ_COMMITTED
ISOLATION_REPEATABLE_READ
ISOLATION_SERIALIZABLE
ReadOnly
If this parameter is set, the transaction is read-only. If this parameter is not set, the transaction is read.
TIMEOUT_DEFAULT transaction timeout settings (starting with timeout)
The Unit is seconds. For example, timeout_20 indicates that the transaction timeout time is 20 seconds.
+ Exception indicates that rollback is performed when this Exception is encountered.
-Exception: If this Exception is encountered, no rollback is performed.
TransactionManager is the Transaction Manager, which provides different implementations for different persistent layers. The interfaces are org. springframework. transaction. PlatformTransactionManager.
The advantage of the XML transaction configuration method is that you do not need to change any code. I hope this configuration will be helpful to you.