【问题标题】:Transaction not getting rolled back with DataSourceTransactionManager and JDBCTemplate使用 DataSourceTransactionManager 和 JDBCTemplate 未回滚事务
【发布时间】:2017-04-25 18:27:12
【问题描述】:

这里是详细信息 -

使用 JDBCTemplate 执行查询。 spring应用上下文文件中所需类的配置如下-

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="mydataSource" />
</bean> 
<bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="mydataSource" />
    <qualifier value="jdbcTransactionManager"/>     
</bean>
<tx:annotation-driven transaction-manager="jdbcTransactionManager"/>
<bean class="org.springframework.transaction.aspectj.AnnotationTransactionAspect" factory-method="aspectOf">
    <property name="transactionManager" ref="datamigrationTransactionManager" />
</bean> 

请注意,在同一个配置文件中,HibernateTemplate 也配置了适当的事务管理器声明,如下所示 -

<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="mydataSource" />
    <property name="sessionFactory" ref="refSessionFactory" />
     <qualifier value="hibernateTransactionManager"/>
  </bean>
  <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
  <bean class="org.springframework.transaction.aspectj.AnnotationTransactionAspect" factory-method="aspectOf">
    <property name="transactionManager" ref="hibernateTransactionManager" />
    </bean>

我在服务类中为方法使用@Transactional 注释,并正确指定了在 spring 配置文件中声明的 jdbc 事务管理器的名称,我在方法中执行 3 条语句,注释如下 -

@Transactional(value = "jdbcTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class)

并按以下顺序执行语句 - (1)对表1执行插入查询 (2)对表2执行插入查询 (3) 使用事务注解对表 3 方法执行更新查询如下 -

@Transactional(value = "jdbcTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class)
    private boolean executeDBOperations(List<Integer> listIdData, List<String> listStringIds, int index) throws Exception
    {           
        List<Integer> idsInBatch = jdbcDAO.getIdsInBatch(index);        
        jdbcDAO.fetchAndInsertMainTableRecords(listIdData);     
        jdbcDAO.fetchAndInsertAssociatedTableRecords(listStringIds);        
        jdbcDAO.updateAssociatedTable2Values(idsInBatch);       
        jdbcDAO.deleteSourceRecords(index);     
        return true;
    }

当更新查询 -> jdbcDAO.updateAssociatedTable2Values(idsInBatch); 来自上述方法失败时,理想情况下,在前 2 次调用期间插入的记录应回滚。但它没有发生。当然删除操作不会执行,因为更新操作失败。另请注意与更新查询引发的数据相关的异常。只是数据问题的原因是更新不会发生,我已经引入了该错误来验证该方法的提交和回滚行为。

现在根据实现和规范,如果任何 db 操作/调用失败,其余操作将在同一方法中执行 应该回滚。但它不起作用。我正在使用 Oracle DB。

我检查了一些关于 SO 的问题,这些问题与使用 JDBCTemplate 的事务以及在同一个 Spring 应用程序中使用 2 个事务管理器有关 上下文,但我没有得到相同的具体答案。

【问题讨论】:

  • 不要描述你的代码。发表它。并发布异常的堆栈跟踪。
  • 我添加了带有 Transactional 属性注释的方法代码。正如我所告知的,更新查询异常与我为验证方法执行的提交/回滚行为而引入的数据有关。您能否就该问题提供任何指示?

标签: spring jdbctemplate spring-transactions spring-annotations transactionmanager


【解决方案1】:

公开交易方法。你可以看看这个帖子,里面有更多信息:Spring transactions not working + JAX WS + JDBC

【讨论】:

    猜你喜欢
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 2010-11-07
    • 2016-01-08
    • 2011-06-10
    相关资源
    最近更新 更多