0

Below is my code for applicationContext.xml and my serviceImpl class.

I have added and @ service and @Transactional added to my code.

But the transaction is not rolling back on exception. It inserts into table 1 even thought table2 insert fails.

<?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 class pseudo

@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());
    }


    } 


}

Exception class

package com.tax.data.exception;

public class DataException extends Exception {

    private static final long serialVersionUID = 1553804688073838262L;

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


}
Geek
  • 3,187
  • 15
  • 70
  • 115

1 Answers1

0

If you're calling this method from within this service, then this might be the classic issue of calling a @Transactional annotated method from within the same class. This scenario does not utilize the proxy class that is created containing the functional annotated @Transactional method.

See this other StackOverflow question for a related explanation.

The solution is to simply factor out this method into another class and inject into the bean you want to call. You should be able to call the method that way since it will be wrapped in the transactional proxy object.

Dovmo
  • 8,121
  • 3
  • 30
  • 44