Spring配置事务在Dao层和Service层

【转载自 http://www.blogjava.net/hk2000c/archive/2007/11/01/157508.html 】 
【感谢原作者:hk2000c】 
Spring通过AOP实现声明式事务管理。通常通过TransactionProxyFactoryBean设置Spring事务代理。我们需要一个目标对象包装在事务代理中。这个目标对象一般是一个普通Java对象的bean。当我们定义TransactionProxyFactoryBean时,必须提供一个相关的 PlatformTransactionManager的引用和事务属性。 事务属性含有上面描述的事务定义。 
PlatformTransactionManager: 
HibernateTransactionManager需要一个SessionFactory的引用 
JtaTransactionManager 
一.把事务放置在了DAO层: 
Java代码   收藏代码
  1.   
  2. "transactionManager"  
  3.        class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  4.        "sessionFactory">  
  5.               "sessionFactory" />  
  6.          
  7.   
  8.   
  9. "companyDAOTarget"  
  10.        class="com.vstsoft.querycompany.dao.impl.CompanyDAOImpl">  
  11.        "sessionFactory">  
  12.               "sessionFactory" />  
  13.          
  14.   
  15.   
  16. "companyDAOProxy"  
  17.        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  18.        "transactionManager">  
  19.               "transactionManager" />  
  20.          
  21.        "target">  
  22.               "companyDAOTarget" />  
  23.          
  24.        "transactionAttributes">  
  25.                 
  26.               "insert*">PROPAGATION_REQUIRED  
  27.               "delete*">PROPAGATION_REQUIRED  
  28.               "find*">  
  29.               PROPAGATION_REQUIRED,readOnly  
  30.                 
  31.                 
  32.          
  33.   
  34.   
  35. "companyManageTarget"  
  36.        class="com.vstsoft.querycompany.service.impl.CompanyManageTarget">  
  37.        "companyDAO">  
  38.               "companyDAOProxy" />  
  39.          
  40.   
  41.   
  42. "companyManageProxy"  
  43.        class="org.springframework.aop.framework.ProxyFactoryBean">  
  44.        "proxyInterfaces">  
  45.               com.vstsoft.querycompany.service.CompanyManage  
  46.          
  47.        "target">  
  48.               "companyManageTarget" />  
  49.          
  50.   
  51.   
  52. "/company"  
  53.        class="com.vstsoft.querycompany.web.action.CompanyAction" singleton="false">  
  54.        "companyManage">  
  55.               "companyManageProxy" />  
  56.          
  57.   


二.把事务放置在了Service层: 
Java代码   收藏代码
  1.   
  2. "jtaTransactionManager"  
  3.        class="org.springframework.transaction.jta.JtaTransactionManager" />  
  4.   
  5. "companyDAOTarget"  
  6.        class="com.vstsoft.querycompany.dao.impl.CompanyDAOImpl">  
  7.        "sessionFactory">  
  8.               "sessionFactory" />  
  9.          
  10.   
  11.   
  12. "companyDAOProxy"  
  13.        class="org.springframework.aop.framework.ProxyFactoryBean">  
  14.        "proxyInterfaces">  
  15.               com.vstsoft.querycompany.dao.CompanyDAO  
  16.          
  17.        "target">  
  18.               "companyDAOTarget" />  
  19.          
  20.   
  21.   
  22. "companyManageTarget"  
  23.        class="com.vstsoft.querycompany.service.impl.CompanyManageTarget">  
  24.        "companyDAO">  
  25.               "companyDAOProxy" />  
  26.          
  27.   
  28.   
  29. "companyManageProxy"  
  30.        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  31.        "transactionManager">  
  32.               "jtaTransactionManager" />  
  33.          
  34.        "transactionAttributes">  
  35.                 
  36.                      "set*">PROPAGATION_REQUIRED  
  37.                      "find*">PROPAGATION_REQUIRED,readOnly  
  38.                 
  39.          
  40.        "target">  
  41.               "companyManageTarget" />  
  42.          
  43.   
  44.   
  45. "/company"  
  46.        class="com.vstsoft.querycompany.web.action.CompanyAction" singleton="false">  
  47.        "companyManage">  
  48.               "companyManageProxy" />  
  49.          
  50.   


例如:在service层的接口实现CompanyManageImpl里面有个setData方法:按顺序执行对数据库的数据查询,数据删除,数据插入数据库,如果哪一步出异常(运行时异常),以上三个操作都将事务回滚。

你可能感兴趣的:(Spring)