Spring Propagation requires_new not working

I have the following scenario where I had 2 service layers taking care of inserting data in to emp table and dept table.

I have written query to fail insert on dept table i.e deptno cannot accept deptno with length > 2, hence it will not inserted into db due to DataIntegrity violation. I don’t want my previous transaction to be dependent on this transaction. So used REQUIRES_NEW propagation level.

Here is my code.

@Component
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    @Autowired
    private DeptService deptService;

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean createEmployee() {
        employeeDao.insertEmployee();
        deptService.createDept();
        return false;
    }
}

@Component
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean createDept() {
        deptDao.insertDepartment();
        return false;
    }
}

Below is Dao layer

@Component
public class EmployeeDaoImpl implements EmployeeDao {

    private String empInsert = "INSERT INTO emp VALUES ('1000','Ravi','CLERK','7782','1990-01-01','1235.00',NULL,'50');";

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public boolean insertEmployee() {

        int n = jdbcTemplate.update(empInsert);
        return true;
    }
}

@Component
public class DeptDaoImpl implements DeptDao {

    private String deptInsert = "INSERT INTO dept VALUES ('500','MATERIAL','ALASKA');";

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public boolean insertDepartment(){
        jdbcTemplate.update(deptInsert);
        return true;
    }

}

The following code is called from main class

public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("txrequirednew.xml");
        EmployeeService es = (EmployeeService) context.getBean("employeeServiceImpl");
        es.createEmployee();
    }

Below is my application context:

   <beans>

     <context:annotation-config/>
        <context:component-scan base-package="txrequirednew">
    </context:component-scan>

        <bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/tejadb"></property>
            <property name="username" value="root"/>
            <property name="password" value="sai"/>
        </bean>

        <!-- Dao Configurations-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>

        <tx:annotation-driven transaction-manager="transactionManager" />

        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource" />
        </bean>

    </beans>

When I try to execute , both the transactions are roll back. But I don’t want my first transaction to be rollback.

What is that I am missing here and that’s causing rollback on both transactions?
How can I fix this.

Miner Asked on June 17, 2017 in General Stack.
Add Comment


  • LATEST ANSWERS

  • 1 Answer(s)

    I’m having the same issue, did you ever resolve this?

    Default Answered on June 19, 2017.
    Add Comment

    Write your answer

    By posting your answer, you agree to the privacy policy and terms of service.