1 Understand Transaction
1) Introduce Spring's transaction manager
a JDBC transactions
<bean id="transactionManager" class="org.springframework.jdbc.
datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
b Hibernate transactions
<bean id="transactionManager" class="org.springframework.
orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
2 Programing transaction in Spring
One approach to adding transaction to your code is to programmly add transactional boundary using transiationTemplate class.
Programming is good when you want complete control over transactional boundary.but you have to use spring specific class.In most case ,your tansactional needs will not require such precise control over transactional boundaries.That is why you will typically choolse to declare transaction support
public void enrollStudentInCourse() {
transactionTemplate.execute(
new TransactionCallback() {
public Object doInTransaction(TransactionStatus ts) {
try {
// do stuff Runs within doInTransaction()
} catch (Exception e) {
ts.setRollbackOnly(); //Calls setRollbackOnly() to roll Calls setRollbackOnly() //to roll back
}
return null; //If successful, transaction is committed
}
}
);
}
<bean id="transactionTemplate" class="org.springframework.
transaction.support.TransactionTemplate">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
</bean>
<bean id="courseService"
class="com.springinaction.training.service.CourseServiceImpl">
<property name=" transactionTemplate">
<ref bean=" transactionTemplate"/>
</property>
</bean>
3 Declaring transactions
Spring's support for declarative transaction management is implementedd through Spirng's AOP framework.
<bean id="courseService" class="org.springframework.transaction.
interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>
com.springinaction.training.service.CourseService
</value>
</list>
</property>
<property name="target">
<ref bean="courseServiceTarget"/> //Bean being proxied
</property>
<property name="transactionManager">
<ref bean="transactionManager"/> //Transaction manager
</property>
<property name="transactionAttributeSource">
<ref bean="attributeSource"/> //Transaction attribute source
</property>
</bean>
1) Understanding transaction attributes
In Spring transaction attribute is a description of how transaction policies should be
applied to a methods
a Propagation behavior
Propagation behavior What it means
PROPAGATION_MANDATORY indicate that the method must run within a transaction.If no transaction is in progress
an exception will be thrown
PROPAGATION_NESTED
PROPAGATION_NEVER indicate that the method can not run withi a transaction. if a transaction exist an exception will be thrown.
PROPAGATIOM_NOT_SUPPORT Indicates that the method should not run within a transaction. If an existing transaction is in progress, it will be suspended for the
duration of the method.
PROPAGATION_REQUIRED indicate that the current method must run within a transaction.if an existing transaction is in progress,the ,method will run with the transaction
otherwise a new transaction will be started
PROPAGATION_REQUIRENEW indicates that the current must run within its own
transaction.A new transaction is started and an existing transaction will be suspend
PROPAGATION_SUPPORT indicate the current mehtod does not require a transaction.but may run if on is already in progress
b Isolation levels
Isolation level What it means
ISOLATION_DEFAULT Using the defaul isolation level of the underlying database
ISOLATION_READ_UNCOMMITTED Allows you read change that have not yet been commit
May result in dirty read,phantom read,nonrepeatable read
ISOLATION_READ_COMMITTED Allows reads from concurrent transactions that have
bean committed.Dirty read are prevent.but platform and norepeatable reads may still occur.
ISOLATIOM_REPEATABLE_READ Multiple read the same field will yield the same result ,unless changed by the transaction itself.Dirty reads ,nonrepeatable are all prevented
phantom may still occur
ISOLATION_SERIALIZABLE This fully ACID-compliant isolation level ensusme that dirty read,unrepeatable read ,phantom read are all prevented.And this is the most slowest isolation
since it is typically accomplished by doing full table lock on the tables in the transaction.
c Read-Only
If a transaction performs only read operation against the underlying datastore.when a transaction begin ,it only make sense to declare a transaction as read only on mehtods with
propagation behavior which start a new transaction.
Furthermore ,if you are Hibernate as persistence mechanism,declaring a transaction as read only will reult in Hibernate flush mode being set to FLUST_NEVER.this tell hibernate to avoid synchroniztion of objects with database.
d Transaction timeout
Suppose that your transaction becomes unexpectedly long-running transaction.Because transaction may invole locks on the underlying database.Instead of waiting it out ,you can delcare a transaction to automaitically roll back.
because timeout clock begin ticking when a transaction start. it only make sense to declare a transaction timeout on methods with propagation behavior that start a new transaction.
2) Declaring a simple transaction policy
<bean id="myTransactionAttribute"
class="org.springframework.transaction.interceptor.
DefaultTransactionAttribute">
<property name="propagationBehaviorName">
<value>PROPAGATION_REQUIRES_NEW</value>
</property>
<property name="isolationLevelName">
<value>ISOLATION_REPEATABLE_READ</value>
</property>
</bean>
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.
MatchAlwaysTransactionAttributeSource">
<property name="transactionAttribute">
<ref bean="myTransactionAttribute"/>
</property>
</bean>
4 Declaring transactions by method name
1) Using NameMatchTransactionAttributeSource
The properties property of NameMatchTransactionAttributeSource maps mehtod to a transaction property descriptor. the property descriptor takes the following form:
Propagation,isolation,readOnly,-Exception,+Exception
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.
NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="enrollStudentInCourse">
PROPAGATION_REQUIRES_NEW
</prop>
</props>
</property>
</bean>
2) Specifying the transaction Isolation level
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.
NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="enrollStudentInCourse">
PROPAGATION_REQUIRES_NEW,ISOLATION_REPEATABLE_READ
</prop>
</props>
</property>
</bean>
3) Using real-only transaction
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.
NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="getCompletedCourses">
PROPAGATION_REQUIRED,ISOLATION_REPEATABLE_READ,readOnly
</prop>
</props>
</property>
</bean>
4)Specifying rollback rules
You can sepcify that a transaction be rollback on specify checked exception
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.
NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="enrollStudentInCourse">
PROPAGATION_REQUIRES_NEW,ISOLATION_REPEATABLE_READ,
-CourseException
</prop>
</props>
</property>
</bean>
Exception can be marked as negative(-) or postive(+)
Negative exception will trigger the roll back if the exception (or sublclass of it) is thrown.Postive exception on the other hand indicate that the transacton should be commit
even if the exception is thrown
5)Using wildcard matches
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.
NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="get*">
PROPAGATION_SUPPORTS
</prop>
</props>
</property>
</bean>
6 Short-cut name match transaction
<bean id="courseService" class="org.springframework.transaction.
interceptor.TransactionProxyFactoryBean">
<property name="transactionProperties">
<props>
<prop key="enrollStudentInCourse">
PROPAGATION_REQUIRES_NEW
</prop>
</props>
</property>
</bean>
分享到:
相关推荐
这个名为"springTransaction.rar"的压缩包文件包含了一个关于Spring事务管理的小型示例,旨在演示如何使用Spring的事务传播机制来处理数据库操作,特别是转账功能的实现。 首先,让我们了解一下什么是事务。在...
在实际项目中,`springtransaction`工程可能是包含了一个完整的示例,用于演示如何在MyEclipse环境中配置和使用Spring的事务管理功能。开发者可以通过导入此工程,学习和实践Spring事务管理的配置与使用,从而更好地...
"spring-transaction.jar"正是提供了Spring事务管理的类库,它包含了一系列用于处理事务的接口、类和配置元素,使得开发者能够方便地实现事务控制。 一、Spring 事务管理概述 Spring事务管理分为编程式事务管理和...
本篇主要聚焦于"Spring 常用 Transaction Annotation",即声明式事务管理,这是一种更简洁、易于维护的事务控制方式。 首先,Spring的声明式事务管理基于AOP(面向切面编程),它允许我们在不修改业务代码的情况下...
12. Spring Transaction Management 13. Spring Batch 14. Spring NoSQL and Big Data 15. Spring Java Enterprise Services and Remoting Technologies 16. Spring Messaging 17. Spring Integration 18. Spring ...
使用 Spring Transaction 支持的简单 Spring MVC 应用程序,带有 @Transactional 注释和 JPA。 配置为与 Weblogic 事务管理器集成。 此示例应用程序仅用作配置了 Spring Transaction 支持的 Spring MVC 应用程序的...
org.springframework.transaction-3.2.2.RELEASE最新版本
### Spring中的Transaction事务传播行为种类详解 #### 一、引言 在开发基于Spring框架的应用程序时,事务管理是确保数据一致性的重要手段之一。Spring框架提供了丰富的事务管理功能,其中包括了事务传播行为...
6. **Spring Transaction**:事务管理模块提供了声明式和编程式的事务管理,确保了在分布式环境下的数据一致性。 7. **Spring Aspects**:此模块提供了AOP的扩展,支持自定义切面和通知类型,增强了Spring的面向切...
在Java企业级应用开发中,Spring框架以其强大的功能和灵活性被广泛应用,特别是在事务管理方面。Spring提供了全面的事务管理解决方案,使得开发者可以方便地控制事务的边界,保证数据的一致性和完整性。本篇将深入...
7. **Spring Transaction**: 提供了一致的事务管理接口,支持编程式和声明式事务管理。这使得事务管理可以跨不同的数据访问技术进行。 8. **Spring MVC**: 是Spring提供的用于构建Web应用的模型-视图-控制器(Model...
SpringTransaction是Spring框架的事务管理模块,它提供了一种声明式和编程式的事务管理方式。声明式事务管理允许我们在配置文件中定义事务边界,而无需在代码中显式控制事务开始和结束。编程式事务管理则通过...
6. **Spring Transaction**:提供了一种声明式和编程式事务管理机制,可以在多个数据库操作之间确保数据的一致性。 7. **Spring Web**:针对Web开发的模块,包含Spring MVC(Model-View-Controller)和Spring ...
3. **Spring Transaction** (spring-tx-4.1.6.RELEASE.jar): 事务管理是Spring的核心功能之一,它允许开发者声明性地管理事务,提供编程式和声明式的事务处理,支持多种事务API如JTA、JDBC、Hibernate等。...
- **功能简介**:包含了 Spring DAO、Spring Transaction 进行数据访问的所有类。 - **应用场景**:适用于需要进行数据访问操作的项目。 - **依赖关系**:依赖于 `spring-core.jar`、`spring-beans.jar`、`spring-...
- Spring Data JPA可以无缝地与Spring Boot、Spring MVC、Spring Transaction管理等组件集成,为开发者提供了完整的解决方案,降低了系统的复杂性。 然而,尽管Spring Data JPA带来了诸多便利,但在实际使用中也会...
7. **Spring Transaction**:提供了统一的事务管理接口,无论是本地事务还是分布式事务,都能进行统一的管理和控制。`@Transactional`注解使得事务管理变得简单。 8. **Spring Aspects**:提供了AOP的实现,包括...
Spring Transaction Management 事务管理是保证数据一致性的重要手段,Spring 提供了全面的事务管理功能,可以方便地应用于不同的业务场景中。 #### 12. Spring Batch 对于需要处理大量数据的任务,Spring Batch...
在思维导图"Spring Transaction.twd"中,可能包含了Spring事务管理的各个概念和它们之间的关系,如事务的ACID属性(原子性、一致性、隔离性和持久性),事务管理器,以及声明式和编程式事务管理的实现方式。...
6. **Integration with Spring Transaction Management**:Spring Data JPA与Spring的事务管理无缝集成,可以方便地进行事务控制。 在实际使用中,我们需要配置Spring Data JPA,这通常涉及到以下步骤: 1. 添加...