我是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");
}
}
我希望CisternDao(表tbl_cistern)删除的行在我(故意)从另一个Dao抛出异常之后回滚,这个Dao假设从Parent表中删除数据。
但是,我的回滚无效。表tbl_cistern数据仍然消失了。
我做错了什么? (我使用的是Spring 3.1,Spring AOP -3.1,Hibernate 3.6,Tomcat6)
=============================================== ================================
EDIT。
这是我的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 - 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,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, UserName=dbpejl@localhost, MySQL-AB JDBC Driver] to manual commit
2011-10-12 11:46:27,727 DEBUG - Switching JDBC Connection [jdbc:mysql://localhost:3306/pejldatastorenorway?autoReconnect=true, UserName=dbpejl@localhost, 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 - 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,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,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 - 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 - 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,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 - 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,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 - 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 - 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 - 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,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 - 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 - 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 - 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 - 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,735 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 - 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,737 DEBUG - Completing 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,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 - 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 - 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 - 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,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 - 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 - Initiating transaction rollback
2011-10-12 11:46:28,397 DEBUG - Rolling back JDBC transaction on Connection [jdbc:mysql://localhost:3306/pejldatastorenorway?autoReconnect=true, UserName=dbpejl@localhost, MySQL-AB JDBC Driver]
2011-10-12 11:46:28,397 DEBUG - Rolling back JDBC transaction on Connection [jdbc:mysql://localhost:3306/pejldatastorenorway?autoReconnect=true, UserName=dbpejl@localhost, MySQL-AB JDBC Driver]
2011-10-12 11:46:28,398 DEBUG - Triggering afterCompletion synchronization
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 - 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,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, UserName=dbpejl@localhost, MySQL-AB JDBC Driver] after transaction
2011-10-12 11:46:28,399 DEBUG - Releasing JDBC Connection [jdbc:mysql://localhost:3306/pejldatastorenorway?autoReconnect=true, UserName=dbpejl@localhost, 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, UserName=dbpejl@localhost, MySQL-AB JDBC Driver] after transaction
答案 0 :(得分:2)
尝试更改AOP表达式以包含服务接口(如果未涵盖这些接口)。我遇到的问题是我的服务接口与服务实现位于不同的包中,如果它们不在AOP表达式中,则不会启动/提交/滚动事务。
此外,如果这是一个webapp,请考虑删除AOP以支持Spring OpenSessionInViewFilter,它会在请求进入时打开会话,并在返回/抛出后提交/回滚。
最后,我使用具有强大Spring支持的IntelliJ IDE - 您可以在XML文件中单击AOP声明并查看模式匹配的所有方法的弹出列表 - 真正有助于调试AOP。
答案 1 :(得分:1)
我认为您需要在上下文中添加<aop:aspectj-autoproxy/>
。这是将方面应用于bean的说明。 aop:config
和tx:advice
只是设置了我认为未应用的方面。
您还可以通过打开spring的信息记录来验证您的类是否正在被考虑并启动/回滚事务。
作为旁注,如果您使用的是Java 1.5或更高版本,则使用注释(@Transactional)而不是基于XML的配置会更好/更容易。
答案 2 :(得分:0)
知道了。或者我应该说问题已解决,而不是真正理解为什么。
从org.springframework.jdbc.datasource.DataSourceTransactionManager更改了我的Transaction bean类
到
org.springframework.orm.hibernate3.HibernateTransactionManager
它的工作。
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> -->
<property name="sessionFactory" ref="mySqlSessionFactory"></property>
<!-- <property name="dataSource" ref="mySqlDataSource"/> -->
</bean>
现在太麻木了,回家了。任何人都可以阐明为什么会如此?