休眠-使用Spring回滚事务-AOP

前端之家收集整理的这篇文章主要介绍了休眠-使用Spring回滚事务-AOP 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我对AOP-Spring非常陌生.我陷入交易问题(使用AOP测试回滚).我想我可能在做一些基本错误的事情.或与现有配置冲突.

我的Spring配置文件

<beans...>
     <!-- This TX is I am interested in -->      
     <bean id="transactionManager"
           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
               <property name="dataSource" ref="MysqLDataSource"/>  
     </bean>

      <bean id="registriesTransactionManager"
           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="registriesDataSource"/>
     </bean> <!-- There are 2 datasorce for AS400 and a transaction for one of them-->

     <aop:config>
         <aop:advisor pointcut="execution(* se.unox.pejl.service.PejlAnalysisService.* (..))" advice-ref="txMysqLAdvice"/>                    
         <! --- + many more --> 
     </aop:config>

     <tx:advice id="txMysqLAdvice" transaction-manager="transactionManager">
       <tx:attributes>
         <tx:method name="get*" read-only="true" />
         <tx:method name="*" />             
         <tx:method name="cleanPejlValues" propagation="required" rollback-for="Throwable"/>    
       </tx:attributes>
    </tx:advice> 

</beans>

我的服务实施

public class PejlAnalysisServiceImpl implements PejlAnalysisService,InitializingBean     {
    @Override
    public void cleanPejlValues() {
         List<String> idsToDelete= pejlDataDao.getPejlIds(Calendar.getInstance().getTime(),pejlType);

         logger.debug("TRYING TO DELETE these IDS: " + idsToDelete);

         numDeletedValues += cisternDao.deleteCistern(idsToDelete);

         logger.debug("DELETED " + numDeletedValues);
         numDeletedValues += pejlDataDao.deletePejlValues(idsToDelete);
     }
 }

我的DAO是我抛出的异常

public class PejlDataDaoMysqLImpl extends GenericDaoImpl<PejlDataInValue,String> implements PejlDataDao {

  @Override
  public int deletePejlValues(List<String> pejlIds)      {          
    throw new RuntimeException("THROW INTENTIONALLY");          
  }
}

我希望(有意地)从另一个Dao抛出异常(假定是要从Parent表删除数据)后回退CisternDao(表tbl_cistern)删除的行.

但是,我的回滚不起作用.表tbl_cistern数据仍然不存在.

我究竟做错了什么? (我正在使用Spring 3.1,Spring AOP -3.1,Hibernate 3.6,Tomcat6)

================================================== ============================

编辑.

这是我的AOP StackTrace.它确实说回滚,但是我从第一个表开始的行仍然消失了.它是一个现有的应用程序.想知道DomainServiceImpl TX是否会干扰.

2011-10-12 11:46:27,726 DEBUG - Creating new transaction with name [se.unox.pejl.service.impl.PejlAnalysisServiceImpl.cleanPejlValues]: PROPAGATION_required,ISOLATION_DEFAULT,-Throwable
2011-10-12 11:46:27,726 DEBUG - Acquired Connection [jdbc:MysqL://localhost:3306/pejldatastorenorway?autoReconnect=true,UserName=dbpejl@localhost,MysqL-AB JDBC Driver] for JDBC transaction
2011-10-12 11:46:27,727 DEBUG - Switching JDBC Connection [jdbc:MysqL://localhost:3306/pejldatastorenorway?autoReconnect=true,MysqL-AB JDBC Driver] to manual commit
2011-10-12 11:46:27,727 DEBUG - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@6113e0] for key [org.apache.commons.dbcp.BasicDataSource@ee003d] to thread [http-8080-1]
2011-10-12 11:46:27,727 DEBUG - Initializing transaction synchronization
2011-10-12 11:46:27,728 DEBUG - Getting transaction for [se.unox.pejl.service.impl.PejlAnalysisServiceImpl.cleanPejlValues]
2011-10-12 11:46:27,729 DEBUG - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@6113e0] for key [org.apache.commons.dbcp.BasicDataSource@ee003d] bound to thread [http-8080-1]
2011-10-12 11:46:27,729 DEBUG - Participating in existing transaction
2011-10-12 11:46:27,729 DEBUG - Getting transaction for [se.unox.pejl.service.impl.DomainServiceImpl.getDomainData]
2011-10-12 11:46:27,730 DEBUG - Bound value [org.springframework.orm.hibernate3.SessionHolder@160088f] for key [org.hibernate.impl.SessionFactoryImpl@1a0280d] to thread [http-8080-1]
2011-10-12 11:46:27,730 DEBUG - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@160088f] for key [org.hibernate.impl.SessionFactoryImpl@1a0280d] bound to thread [http-8080-1]
2011-10-12 11:46:27,734 DEBUG - Completing transaction for [se.unox.pejl.service.impl.DomainServiceImpl.getDomainData]
2011-10-12 11:46:27,734 DEBUG - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@6113e0] for key [org.apache.commons.dbcp.BasicDataSource@ee003d] bound to thread [http-8080-1]
2011-10-12 11:46:27,734 DEBUG - Participating in existing transaction
2011-10-12 11:46:27,734 DEBUG - Getting transaction for [se.unox.pejl.service.impl.DomainServiceImpl.getDomainData]
2011-10-12 11:46:27,735 DEBUG - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@160088f] for key [org.hibernate.impl.SessionFactoryImpl@1a0280d] bound to thread [http-8080-1]
2011-10-12 11:46:27,735 DEBUG - Completing transaction for [se.unox.pejl.service.impl.DomainServiceImpl.getDomainData]
2011-10-12 11:46:27,735 DEBUG - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@6113e0] for key [org.apache.commons.dbcp.BasicDataSource@ee003d] bound to thread [http-8080-1]
2011-10-12 11:46:27,735 DEBUG - Participating in existing transaction
2011-10-12 11:46:27,735 DEBUG - Getting transaction for [se.unox.pejl.service.impl.DomainServiceImpl.getDomainData]
2011-10-12 11:46:27,737 DEBUG - Completing transaction for [se.unox.pejl.service.impl.DomainServiceImpl.getDomainData]
2011-10-12 11:46:27,737 DEBUG - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@160088f] for key [org.hibernate.impl.SessionFactoryImpl@1a0280d] bound to thread [http-8080-1]
2011-10-12 11:46:27,789 DEBUG - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@160088f] for key [org.hibernate.impl.SessionFactoryImpl@1a0280d] bound to thread [http-8080-1]
2011-10-12 11:46:27,789 DEBUG - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@160088f] for key [org.hibernate.impl.SessionFactoryImpl@1a0280d] bound to thread [http-8080-1]
2011-10-12 11:46:28,396 DEBUG - Completing transaction for [se.unox.pejl.service.impl.PejlAnalysisServiceImpl.cleanPejlValues] after exception: java.lang.RuntimeException: THROWN INTENTIONALLY
2011-10-12 11:46:28,396 DEBUG - Applying rules to determine whether transaction should rollback on java.lang.RuntimeException: THROWN INTENTIONALLY
2011-10-12 11:46:28,396 DEBUG - Winning rollback rule is: RollbackRuleAttribute with pattern [Throwable]
2011-10-12 11:46:28,396 DEBUG - Triggering beforeCompletion synchronization
2011-10-12 11:46:28,397 DEBUG - Removed value [org.springframework.orm.hibernate3.SessionHolder@160088f] for key [org.hibernate.impl.SessionFactoryImpl@1a0280d] from thread [http-8080-1]
2011-10-12 11:46:28,397 DEBUG - Initiating transaction rollback
2011-10-12 11:46:28,397 DEBUG - Rolling back JDBC transaction on Connection [jdbc:MysqL://localhost:3306/pejldatastorenorway?autoReconnect=true,MysqL-AB JDBC Driver]
2011-10-12 11:46:28,398 DEBUG - Triggering afterCompletion synchronization
2011-10-12 11:46:28,398 DEBUG - Clearing transaction synchronization
2011-10-12 11:46:28,398 DEBUG - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@6113e0] for key [org.apache.commons.dbcp.BasicDataSource@ee003d] from thread [http-8080-1]
2011-10-12 11:46:28,399 DEBUG - Releasing JDBC Connection [jdbc:MysqL://localhost:3306/pejldatastorenorway?autoReconnect=true,MysqL-AB JDBC Driver] after transaction
2011-10-12 11:46:28,399 DEBUG - Returning JDBC Connection to DataSource
2011-10-12 11:46:28,399 DEBUG - Returning JDBC Connection to DataSource
2011-10-12 11:30:36,577 DEBUG - Releasing JDBC Connection [jdbc:MysqL://localhost:3306/pejldatastorenorway?autoReconnect=true,MysqL-AB JDBC Driver] after transaction
最佳答案
尝试更改您的AOP表达式,使其也包括服务接口(如果未涵盖).我遇到的问题是我的服务接口与服务实现位于不同的包中,并且如果AOP表达式未涵盖它们,则不会启动/提交/滚动事务.

另外,如果这是一个Web应用程序,请考虑放弃AOP,改用Spring的OpenSessionInViewFilter,它在请求进入时打开一个会话,并在返回/引发后将其提交/回滚.

最后,我使用了对Spring的强大支持的IntelliJ IDE-您可以单击XML文件中的AOP声明,并查看该模式匹配的所有方法的弹出列表-确实有助于调试AOP.

原文链接:https://www.f2er.com/spring/531743.html

猜你在找的Spring相关文章