`
zenghuiss
  • 浏览: 26034 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring Transaction Attributes

 
阅读更多
What are transaction attributes?

Spring transactions allow setting up the propagation behavior, isolation, timeout and read only settings of a transaction. Before we delve into the details, here are some points that need to be kept in mind

Isolation level and timeout settings get applied only after the transaction starts.
Not all transaction managers specify all values and may throw exception with some non default values
Propagation

PROPAGATION_REQUIRED
This attribute tells that the code needs to be run in a transactional context. If a transaction already exists then the code will use it otherwise a new transaction is created. This is the default and mostly widely used transaction setting.

PROPAGATION_SUPPORTS
If a transaction exists then the code will use it, but the code does not require a new one. As an example, consider a ticket reservation system. A query to get total seats available can be executed non-transactionally. However, if used within a transaction context it will deduct tickets already selected and reduce them from the total count, and hence may give a better picture. This attribute should be used with care especially when PROPAGATION_REQUIRED or PROPAGATION_REQUIRES_NEW is used within a PROPAGATION_SUPPORTS context.

PROPAGATION_MANDATORY
Participates in an existing transaction, however if no transaction context is present then it throws a TransactionRequiredException

PROPAGATION_REQUIRES_NEW
Creates a new transaction and if an existing transaction is present then it is suspended. In other words a new transaction is always started. When the new transaction is complete then the original transaction resumes. This transaction type is useful when a sub activity needs to be completed irrespective of the containing transaction. The best example of this is logging. Even if a transaction roll backs you still want to preserve the log statements. Transaction suspension may not work out of the box with all transaction managers, so make sure that the transaction manager supports transaction suspension

PROPAGATION_NOT_SUPPORTED
This attribute says that transaction is not supported. In other words the activity needs to be performed non-transactionally. If an existing transaction is present then it is suspended till the activity finishes.

PROPAGATION_NEVER
This attributes says that the code cannot be invoked within a transaction. However, unlike PROPAGATION_NOT_SUPPORTED, if an existing transaction is present then an exception will be thrown

PROPAGATION_NESTED
The code is executed within a nested transaction if existing transaction is present, if no transaction is present then a new transaction is created. Nested transaction is supported out of the box on only certain transaction managers.

Isolation


Isolation is a property of a transaction that determines what effect a transaction has on other concurrent transactions. To completely isolate the transaction the database may apply locks to rows or tables. Before we go through the transaction levels, let us look at some problems that occur when transaction 1 reads data that is being modified by transaction 2.

Dirty Reads- Dirty reads occur when transaction 2 reads data that has been modified by transaction 1 but not committed. The problem occurs when transaction 1 rollbacks the transaction, in which case the data read by transaction 2 will be invalid.
Non Repeatable Reads- Nonrepeatable reads happen when a transaction fires the same query multiple times but receives different data each time for the same query. This may happen when another transaction has modified the rows while this query is in progress.
Phantom Reads - Phantom reads occur when the collection of rows returned is different when a same query is executed multiple times in a transaction. Phantom reads occur when transaction 2 adds rows to a table between the multiple queries of transaction 1.
The following isolation levels are supported by spring

ISOLATION_DEFAULT
Use the isolation level of the underlying database.

ISOLATION_READ_UNCOMMITTED
This is the lowest level of isolation and says that a transaction is allowed to read rows that have been added but not committed by another transaction. This level allows dirty reads, phantom reads and non repeatable reads.

ISOLATION_READ_COMMITTED
This level allows multiple transactions on the same data but does not allow uncommited transaction of one transaction to be read by another. This level, therefore, prevents dirty reads but allows phantom reads and nonrepeatable reads. This is the default isolation setting for most database and is supported by most databases.

ISOLATION_REPEATABLE_READ
This level ensures that the data set read during a transaction remains constant even if another transaction modifies and commits changes to the data. Therefore if transaction 1 reads 4 rows of data and transaction 2 modifies and commits the fourth row and then transaction 1 reads the four rows again then it does not see the modifications made by transaction 2. (It does not see the changes made in the fourth row by the second transaction). This level prevents dirty reads and non repeatable reads but allows phantom reads.

ISOLATION_SERIALIZABLE
This is the highest isolation level. It prevents dirty reads, non repeatable reads and phantom reads. This level prevents the situation when transaction 1 performs a query with a certain where clause and retrieves say four rows, transaction 2 inserts a row that forms part of the same where clause and then transaction 1 reruns the query with the same where clause but still sees only four rows (does not see the row added by the second transaction)


Read Only

The read only attribute specifies that the transaction is only going to read data from a database. The advantage is that the database may apply certain optimization to the transaction when it is declared to be read only. Since read only attribute comes in action as soon as the transaction starts, it may be applied to only those propagation settings that start a transaction. i.e. PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW and PROPAGATION_NESTED.

Timeout

Timeout specifies the maximum time allowed for a transaction to run. This may be required since transactions that run for a very long time may unnecessarily hold locks for a long time. When a transaction reaches the timeout period, it is rolled back. Timeout needs to be specified only on propagation settings that start a new transaction

Rollback Rules

It is also possible to specify that transactions roll back on certain exceptions and do not rollback on other exceptions by specifying the rollback rules.
分享到:
评论

相关推荐

    实验 spring 声明事务

    声明式事务管理的原理在于,当满足特定条件(如上述配置中的方法匹配)时,Spring 会自动开始、提交或回滚事务,无需在业务逻辑代码中显式调用`beginTransaction()`、`commit()`或`rollback()`等事务管理API。...

    spring的部分常用xsd文件,包含mvc,aop,beans,tx,task多个版本

    `spring-tx.xsd`与Spring的事务管理相关,提供了如`<tx:annotation-driven>`元素来自动处理事务,以及`<tx:advice>`、`<tx:attributes>`等元素来定义事务策略。 5. **Spring Task**: `spring-task.xsd`涉及到...

    spring事务与数据库操作

    <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> *" propagation="REQUIRED" rollback-for="Exception"/> *" propagation="REQUIRED" rollback-for="Exception"/> *" ...

    spring4.3.9相关jar包

    使用基于AOP 的Spring特性,如声明型事务管理(Declarative Transaction Management),也要在应用里包含这个jar包。 外部依赖spring-core, (spring-beans,AOP Alliance, CGLIB,Commons Attributes)。 spring-...

    spring的5中事物配置 介绍spring的5中事物配置

    <bean id="userDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <!-- 配置事务管理器 --> <!-- 配置需要代理的方法 --> <!-- 配置事务属性 --> *">...

    Spring-JDBC整合-MySQL8、java8版本

    <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> *" propagation="REQUIRED"/> </tx:attributes> ``` 以上配置表明,所有在com.example.service包及其子包下的方法都将被...

    Spring进阶:Spring的CRUD

    <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> *" propagation="REQUIRED"/> </tx:attributes> <bean id="transactionManager" class="org.springframework.jdbc....

    Spring中文帮助文档

    2.6.4. 将Spring 应用程序上下文部署为JCA adapter 2.6.5. 计划任务 2.6.6. 对Java 5 (Tiger) 支持 2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)...

    Spring-Reference_zh_CN(Spring中文参考手册)

    2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的...

    Spring API

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...

    spring事务与配置

    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <!--配置事务管理器--> <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao"/> <!--配置...

    spring chm文档

    Spring Framework 开发参考手册 Rod Johnson Juergen Hoeller Alef Arendsen Colin Sampaleanu Rob Harrop Thomas Risberg Darren Davison Dmitriy Kopylenko Mark Pollack Thierry Templier Erwin ...

    Spring使用XML配置声明式事务

    <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> *" propagation="REQUIRED" rollback-for="Exception"/> *" propagation="REQUIRED" rollback-for="Exception"/>...

    spring整合hibernate实现事务处理

    在`Spring_1800_Spring_Hibernate_Transaction_Annotation`这个压缩包文件中,很可能包含了使用注解方式实现Spring整合Hibernate事务处理的相关示例代码和配置文件。通过阅读和理解这些代码,你可以更好地掌握这一...

    Spring2.0的配置

    Spring 2.0 的配置是其核心特性之一,它引入了依赖注入(IOC)和面向切面编程(AOP)的概念,极大地简化了Java企业级应用的开发。然而,随着应用规模的扩大,配置文件的数量和复杂度也随之增加,特别是事务配置。在...

    spring框架搭建

    <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> *" propagation="REQUIRED" /> </tx:attributes> ``` 在上述配置中,`tx:method`元素定义了哪些方法应该在事务中运行,`...

    Spring2.0 事务处理

    <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> *" propagation="REQUIRED" /> *" propagation="REQUIRED" /> *" propagation="REQUIRED" rollback-for="Exception" /> ...

    spring中事物配置

    <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> *" propagation="REQUIRED"/> </tx:attributes> ``` 这里,`*`表示所有方法都将在`REQUIRED`事务传播策略下...

    Spring 2.0 开发参考手册

    2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@...

Global site tag (gtag.js) - Google Analytics