Spring @Transactional没有回滚

时间:2016-11-30 13:45:34

标签: java mysql spring spring-jdbc spring-transactions

我正在使用Spring的NamedParameterJdbcTemplate而我正在尝试使用@Transactional来确保两个插入既可以持久保存到数据库,也可以使用{}如果另一个失败,请回来。

该代码旨在与MySql一起运行,并在内存中使用H2进行测试。

它没有工作......第二次插入失败但第一次插入没有回滚。

以下是相关课程:

MySpringConfig.java

package com.MyPackage.spring

@Configuration
@ComponentScan({ "com.MyPackage" })
public class MySpringConfig {

@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate() throws Exception {
    return new NamedParameterJdbcTemplate(dataSource());
}

@Bean
public DataSourceTransactionManager dataSourceTransactionManager() throws Exception {
    return new DataSourceTransactionManager(dataSource());
}


@Bean(name = "customerEntitiesDataSource")
public DataSource dataSource() throws Exception {
    Properties properties = new Properties();
    properties.put("url", "db-url");
    properties.put("username", "uName");
    properties.put("password", "pwd");
    BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
    return dataSource;
}
}

MyDao.java

package com.MyPackage.dao

@Repository
public class MyDao {
    private ObjectMapper mapper = new ObjectMapper();

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    private static String INSERT_ELEMENT_QUERY = "INSERT INTO TableA..." + " VALUES (:param1, :param2)";

    @Transactional(rollbackFor = {Exception.class}, propagation=Propagation.REQUIRED)
    public void storeElements(List<Element> element) {


        Stream<HashMap<String, Object>> hashMapStream = elements.stream().map(
                element -> {
                    HashMap<String, Object> params = new HashMap<>();
                    params.put("param1", element.getParam1());
                    params.put("param2", element.getParam2());
                    return params;
                }
        );
        List<Map<String, Object>> collect = hashMapStream.collect(Collectors.<Map<String, Object>>toList());
        Map<String, Object>[] batchValues = new Map[]{};
        batchValues = collect.toArray(batchValues);

        jdbcTemplate.batchUpdate(INSERT_ELEMENT_QUERY, batchValues);

        computers.stream().forEach(element -> saveExtraData(element));
    }


    private static String INSERT_ELEMENT_EXTRA_DATA_QUERY = "INSERT INTO TableB... Values (:val1, :val2)";

    @Transactional(rollbackFor = {Exception.class}, propagation=Propagation.REQUIRED)
    public void saveExtraData(Element element) {

        Stream<HashMap<String, Object>> hashMapStream = element.getExtraData().stream().map(
                extra -> {
                    HashMap<String, Object> params = new HashMap<>();
                    params.put("val1", extra.getVal1());
                    params.put("val2", extra.getVal2());
                    return params;
                }
        );
        List<Map<String, Object>> collect = hashMapStream.collect(Collectors.<Map<String, Object>>toList());
        Map<String, Object>[] batchValues = new Map[]{};
        batchValues = collect.toArray(batchValues);

        jdbcTemplate.batchUpdate(INSERT_ELEMENT_EXTRA_DATA_QUERY , batchValues);
    }
}

我正在运行的测试是:将元素列表发送到storeElements(),但要确保其中一个元素的extraData太长,无法存储在{{1}中}}。然后检查数据库以确保数据库中没有存储任何元素。

插入失败,但没有回滚。

整个代码是一个Spring堆栈,从测试一直到TableB方法。

运行debug,我发现在加载Spring上下文时,除了构造函数之外,没有调用任何DataSourceTransactionManager方法。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

使用MySpringConfig注释您的@EnableTransactionManagement以启用事务管理。

@Configuration
@ComponentScan({ "com.MyPackage" })
@EnableTransactionManagement
public class MySpringConfig {