交易没有回滚Spring

时间:2018-02-05 20:41:41

标签: java spring spring-mvc spring-transactions

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

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

但是事务并没有在异常上回滚。它插入到表1中,甚至认为table2插入失败。

<?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);
    }


}

1 个答案:

答案 0 :(得分:0)

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

有关相关说明,请参阅this other StackOverflow question

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