繁体   English   中英

交易未回滚Spring

[英]Transaction not rolling back Spring

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

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

但是交易不会因异常而回滚。 即使表2插入失败,它也会插入表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);
    }


}

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

有关相关说明,请参见另一个StackOverflow问题

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM