【问题标题】:Transaction not rolling back Spring事务不回滚 Spring
【发布时间】:2018-07-15 19:31:39
【问题描述】:

下面是我的 applicationContext.xml 代码和我的 serviceImpl 类。

我已将 @service 和 @Transactional 添加到我的代码中。

但事务不会因异常而回滚。即使 table2 插入失败,它也会插入表 1。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" 
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

<!-- Persistence layer -->
<context:annotation-config />

<context:component-scan base-package="com.tax.data" />

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="oracleds" /> 
   </bean>
<tx:annotation-driven transaction-manager="txManager" />

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="oracleds" />
<property name="configLocation" value="classpath:config/myBatis-config.xml" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tax.data.persistence.mapper" />
</bean>

<bean id="oracleds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="${ORACLE_DB_URL}" />
<property name="username" value="${ORACLE_DB_USER}" />         
<property name="password" value="${ORACLE_DB_PWD}" />       
</bean>  

</beans>

服务类伪

@Service
public class EditTaxServiceImpl implements taxService{
private static final Logger LOG = LogManager.getLogger(EditTaxServiceImpl.class);
@Autowired
private TaxMapper taxMapper;

//..//


@Override
   @Transactional(propagation = Propagation.REQUIRED, rollbackFor = DataException.class)
   public void insertUserTaxRequest(UserRequest userRequest, TaxProcess taxProcess) throws DataException{      
       UserInfo userInfo = new UserInfo();
       InterfaceInfo interfaceInfo = new InterfaceInfo();
       TaxInfo taxInfo = new TaxInfo();    
    try{
       ///..///

        taxMapper.insertUserInfo(userInfo);

        taxMapper.insertTaxInfo(taxInfo);

        taxMapper.insertTaxEditInfo(taxInfo);


    } catch (Exception ex) {

        throw new DataException("EditTaxServiceImpl Service : Error in inserting EditTaxServiceImpl"+  ex.getMessage());
    }


    } 


}

异常类

package com.tax.data.exception;

public class DataException extends Exception {

    private static final long serialVersionUID = 1553804688073838262L;

    public DataException( String message) {
        super(message);
    }


}

【问题讨论】:

  • 我认为您需要将datasource 添加到txManager,类似这样的&lt;property name="dataSource" ref="oracleds"/&gt;
  • 你为什么使用JtaTransactionManager?您正在使用不是 XA 能力的 DriverManagerDataSource。你也有一个资源,为什么要使用它?请改用DataSourceTransactionManager 并将数据源注入其中。
  • @M. Deinum 我已经添加了 DataSourceTransactionManager 并更新了注入数据源的代码,但回滚仍然不起作用。
  • 您正在捕获此异常并使用它而不是传播异常
  • 你能发DataException的代码吗?

标签: java spring spring-mvc spring-transactions


【解决方案1】:

如果您在此服务中调用此方法,那么这可能是从同一类中调用@Transactional 注释方法的经典问题。此方案不使用创建的包含功能注释 @Transactional 方法的代理类。

相关解释请见this other StackOverflow question

解决方案是简单地将这个方法分解到另一个类中并注入到您要调用的 bean 中。您应该能够以这种方式调用该方法,因为它将包装在事务代理对象中。

【讨论】:

    猜你喜欢
    • 2017-03-03
    • 2012-03-10
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    相关资源
    最近更新 更多