Spring事务配置:
1.在Spring配置文件引入tx
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http:
http:
http:
http:
http:
http:
http:
2.配置基于注解的声明式事务管理
spring不负责事务的处理,把事务交给各事务处理器
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory">property>
bean>
<tx:annotation-driven transaction-manager="txManager"/>
3.在要使用事务管理的类或者方法上增加代码@Transactional,Spring官方团队建议不要在接口使用。在类上使用@Transactional,类中的所有public方法都将使用事务
@Transactional
public class Txtest implements TestService { }
在public方法上使用@Transactional,则该方法使用事务;非public方法使用@Transactional不会报错,但也不会使用事务,相当于“白做”
如果在类上使用@Transactional,但是类中的某个方法不想使用事务,则可以使用
@Transactional
public class Txtest implements TestService {
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public List