基于Spring注释的事务不起作用

时间:2013-04-29 22:41:54

标签: spring spring-annotations

目前我使用的是Spring 3.0.5 jar文件。

当我使用基于XML的方式配置所有内容时,我的事务正常工作,但这是旧的机制。我希望我的交易应该在注释的基础上进行。

我有一个事务要从2个表中删除数据。

这是日志文件,证明我的交易正在为基于XML的配置正确发生

2013-04-29 14:46:33,900 TRACE TransactionSynchronizationManager,http-8080-2:258 - Initializing transaction synchronization
2013-04-29 14:46:33,901 TRACE ***TransactionInterceptor***,http-8080-2:362 - Getting transaction for [com.abc.business.impl.abcBusinessImpl.deleteTitles]
2013-04-29 14:46:33,902  INFO abcDaoImpl,http-8080-2:72 - SqlQueryStr :delete from users where userId = 'xyz' 
2013-04-29 14:46:33,919 DEBUG JdbcTemplate,http-8080-2:434 - Executing SQL query [delete from users where userId = 'xyz' ]
2013-04-29 14:46:33,926 DEBUG DataSourceUtils,http-8080-2:110 - Fetching JDBC Connection from DataSource
2013-04-29 14:46:34,285 DEBUG DataSourceUtils,http-8080-2:114 - Registering transaction synchronization for JDBC Connection
2013-04-29 14:46:34,293 TRACE TransactionSynchronizationManager,http-8080-2:183 - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@41a80e5a] for key [org.apache.tomcat.dbcp.dbcp.BasicDataSource@19e7ce87] to thread [http-8080-2]
2013-04-29 14:46:34,331 TRACE TransactionSynchronizationManager,http-8080-2:139 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@41a80e5a] for key [org.apache.tomcat.dbcp.dbcp.BasicDataSource@19e7ce87] bound to thread [http-8080-2]
2013-04-29 14:46:36,382 TRACE TransactionSynchronizationManager,http-8080-2:139 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@41a80e5a] for key [org.apache.tomcat.dbcp.dbcp.BasicDataSource@19e7ce87] bound to thread [http-8080-2]
2013-04-29 14:46:36,383 TRACE ***TransactionInterceptor***,http-8080-2:391 - Completing transaction for [com.abc.business.impl.abcBusinessImpl.deleteTitles]
2013-04-29 14:46:36,385 TRACE DataSourceTransactionManager,http-8080-2:925 - Triggering beforeCommit synchronization
2013-04-29 14:46:36,386 TRACE DataSourceTransactionManager,http-8080-2:938 - Triggering beforeCompletion synchronization

但是当我切换到基于注释时,我没有看到它是在方法调用之前和之后附加的TransactionInterceptor。因此,它无法回滚。

请在下面找到基于注释的配置。

<context:component-scan base-package="com.abc.business" />
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <!-- (this dependency is defined somewhere else) -->
  <property name="dataSource" ref="dataSource"/>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="fetch*"  read-only="true" propagation="SUPPORTS" />
        <tx:method name="delete*" propagation="REQUIRES_NEW" />
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* *.com.abc.business.abcBusiness.*(..))"  />
</aop:config>

并在abcBusinessImpl.java文件中添加了@Transactional属性。

@Service
@Transactional
public class abcBusinessImpl implements abcBusiness {

..
@Transactional(readOnly=false, propagation=Propagation.REQUIRES_NEW, isolation=Isolation.READ_COMMITTED)
public boolean deleteTitles(List<abcTitle> abcTitles)

我们正在使用Spring MVC和基于XML的配置,其余的都是基于注释。希望这不会成为一个问题。

如果您需要任何其他信息,请告诉我。

我需要你的建议/输入来解决这个问题。我一整天用谷歌搜索,但我没有看到任何正确的解决方案。

0 个答案:

没有答案