spring transaction

本文深入探讨了Spring框架中事务管理的两种主要方式:编程式和声明式。首先介绍了如何通过配置文件实现事务管理,包括使用TransactionTemplate和TransactionProxyFactoryBean。接着详细解释了事务属性的概念,如传播行为、隔离级别、只读操作等,并提供了通过XML配置和注解来声明事务的方法。最后,通过实例展示了如何为特定方法设置不同的事务属性,包括默认属性和自定义属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值