spring事物-@Transactional

 

 

@Transactional 

 

转载其他人的文章,算作笔记。转载:http://blog.csdn.net/defonds/article/details/6888311

 

    除了使用 XML 配置管理之外,AOP 还有一种事务管理的方式:使用 @Transactional 注解事务管理。
        其中的类定义:

  1. <!-- the service class that we want to make transactional -->  
  2. @Transactional  
  3. public class PurchaseServiceImpl implements PurchaseService {  
  4.   
  5.     Foo getFoo(String fooName);  
  6.   
  7.     Foo getFoo(String fooName, String barName);  
  8.   
  9.     void insertFoo(Foo foo);  
  10.   
  11.     void updateFoo(Foo foo);  
  12. }  



        当上述的 POJO 定义在 Spring IoC 容器里时,上述 bean 实例仅仅通过一行 xml 配置就可以使它具有事务性的。如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xsi:schemaLocation="  
  7.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd  
  9.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  10.   
  11.   <!-- this is the service object that we want to make transactional -->  
  12.   <bean id="purchaseService" class="com.defonds.netbuy.purchase.service.PurchaseServiceImpl"/>  
  13.   
  14.   <!-- enable the configuration of transactional behavior based on annotations -->  
  15.   <tx:annotation-driven transaction-manager="txManager"/>  
  16.   
  17.   <!-- a PlatformTransactionManager is still required -->  
  18.   <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  19.     <!-- (this dependency is defined somewhere else) -->  
  20.     <property name="dataSource" ref="dataSource"/>  
  21.   </bean>  
  22.   
  23.   <!-- other <bean/> definitions here -->  
  24.     
  25.   
  26. </beans>  



        如果是 iBatis 框架,可以参考上篇博客《Spring 下事务管理 - 使用 AOP XML 配置管理(iBatis 为例)》里的相关 dao 支持。
        @Transactional 注解应该只被应用到 public 可见度的方法上。 如果你在 protected、private 或者 package-visible 的方法上使用 @Transactional 注解,它也不会报错, 但是这个被注解的方法将不会展示已配置的事务设置。
        @Transactional 注解可以被应用于接口定义和接口方法、类定义和类的 public 方法上。然而,请注意仅仅 @Transactional 注解的出现不足于开启事务行为,它仅仅 是一种元数据,能够被可以识别 @Transactional 注解和上述的配置适当的具有事务行为的beans所使用。上面的例子中,其实正是 <tx:annotation-driven/>元素的出现 开启 了事务行为。


        Spring团队的建议是你在具体的类(或类的方法)上使用 @Transactional 注解,而不要使用在类所要实现的任何接口上。你当然可以在接口上使用 @Transactional 注解,但是这将只能当你设置了基于接口的代理时它才生效。因为注解是 不能继承 的,这就意味着如果你正在使用基于类的代理时,那么事务的设置将不能被基于类的代理所识别,而且对象也将不会被事务代理所包装(将被确认为严重的)。因此,请接受Spring团队的建议并且在具体的类上使用 @Transactional 注解。
参考文献:《Spring Framework reference 2.0.5》。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值