我正在使用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()
,但确保其中一个元素具有在TableB
中无法存储的extraData
。然后,我检查数据库,以确保没有元素被存储在数据库中。插入失败了,但没有回滚。
整个代码是Spring栈,从测试一直到
storeElements
方法。运行调试,我发现除了构造函数以外,没有调用任何
DataSourceTransactionManager
方法,当Spring上下文加载时创建它。我在这里缺少什么?