Spring Propagation needs_new不起作用

时间:2017-06-17 03:36:22

标签: spring spring-transactions

我有以下场景,我有2个服务层负责将数据插入到emp表和dept表中。

我已经在dept表上写了失败插入的查询,即deptno不能接受deptno,长度为> 2,因此由于DataIntegrity违规而不会插入到db中。我不希望我以前的交易依赖于此交易。所以使用了REQUIRES_NEW传播级别。

这是我的代码。

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

下面是Dao图层

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

}

从主类

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

以下是我的申请背景:

   <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>

当我尝试执行时,两个事务都回滚。但我不希望我的第一笔交易被回滚。

我在这里缺少的是什么导致两笔交易都回滚? 我该如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

如果内部转换通过requires_new传播回滚它不应该影响外部转换,那么你对事务范围是对的,因为

  

PROPAGATION_REQUIRES_NEW为给定范围启动新的独立“内部”事务。此事务将完全独立于外部事务提交或回滚,具有自己的隔离范围,自己的锁定等。外部事务将在内部事务开始时暂停,并在内部事务开始时恢复完成。 ...

但是你应该处理你的内部方法抛出的异常,如果你没有处理它们,它将回滚外部事务。 Spring框架事务将仅回滚RunTimeException不用于其他已检查的激活,如SqlException。

相关问题