声明式事务管理

基于Spring的AOP的声明式事务管理

这种管理方式只需要保证事务层方法命名有一定的规律,通过配置即可实现。

applicationContext.xml的配置如下:

<!-- 事务管理配置 -->
<bean id="transactionManager"
	class="org.springframework.orm.hibernate4.HibernateTransactionManager">
	<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<aop:aspectj-autoproxy proxy-target-class="true"/>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<tx:attributes>
		<tx:method name="find*" propagation="REQUIRES_NEW" />
		<tx:method name="save*" propagation="REQUIRED"
			rollback-for="BaseException,DataBaseException,Exception" />
		<tx:method name="update*" propagation="REQUIRED"
			rollback-for="BaseException,DataBaseException" />
		<tx:method name="*delete*" propagation="REQUIRED"
			rollback-for="BaseException,DataBaseException" />
	</tx:attributes>  
</tx:advice>
<aop:config>
	<aop:advisor pointcut="execution(* com.dhcc.phms.service.sys..*Service*.*(..))"
		advice-ref="txAdvice" />
</aop:config>

基于@Transational的声明式事务管理

这种方式的事务管理是基于Spring注解实现的,只需要在需要管理的事务层上加上该注解即可。

配置注解自动扫描:

<!-- 事务管理器 -->
<bean id="transactionManager" 
	class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory">
	<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 对标注@Transaction注解的Bean进行事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
添加注解:

@Transactional
@Service
public class UserService {
	@Resource
	private UserDao userDao;	
	
	public Integer findCount(){
		return userDao.findAllUser().size();
	}
}
注意:注解式声明只能作用于public类,非public类事务管理不生效

注解最好用在实现类上。






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值