Spring配置事务在DAO层和Service层

Spring通过AOP实现声明式事务管理。通常通过TransactionProxyFactoryBean设置Spring事务代理。我们需要一个目标对象包装在事务代理中。这个目标对象一般是一个普通Java对象的bean。当我们定义TransactionProxyFactoryBean时,必须提供一个相关的 PlatformTransactionManager的引用和事务属性。 事务属性含有上面描述的事务定义。
PlatformTransactionManager:
HibernateTransactionManager需要一个SessionFactory的引用
JtaTransactionManager
一.把事务放置在了DAO层:

       class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      
              
      



       class="com.vstsoft.querycompany.dao.impl.CompanyDAOImpl">
      
              
      



       class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      
              
      

      
              
      

      
              
              PROPAGATION_REQUIRED
              PROPAGATION_REQUIRED
              
              PROPAGATION_REQUIRED,readOnly
              

              

      



       class="com.vstsoft.querycompany.service.impl.CompanyManageTarget">
      
              
      



       class="org.springframework.aop.framework.ProxyFactoryBean">
      
              com.vstsoft.querycompany.service.CompanyManage
      

      
              
      



       class="com.vstsoft.querycompany.web.action.CompanyAction" singleton="false">
      
              
      


二.把事务放置在了Service层:

       class="org.springframework.transaction.jta.JtaTransactionManager" />

       class="com.vstsoft.querycompany.dao.impl.CompanyDAOImpl">
      
              
      



       class="org.springframework.aop.framework.ProxyFactoryBean">
      
              com.vstsoft.querycompany.dao.CompanyDAO
      

      
              
      



       class="com.vstsoft.querycompany.service.impl.CompanyManageTarget">
      
              
      



       class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      
              
      

      
              
                     PROPAGATION_REQUIRED
                     PROPAGATION_REQUIRED,readOnly
              

      

      
              
      



       class="com.vstsoft.querycompany.web.action.CompanyAction" singleton="false">
      
              
      


在service层的接口实现CompanyManageImpl里面有个setData方法:按顺序执行数据查询,数据删除,数据插入数据库行为,如果哪一步出异常(运行时异常),事务回滚,只有所有行为都没成功,事务提交。

你可能感兴趣的:(spring)