我的服务应该将数据保存到父数据库表和子数据库表中,并在发生错误时进行回滚。我尝试使用硬编码的RuntimeException
强制执行错误,发现事务无论如何都会被提交。我错过了什么?我使用的是Spring Boot2,包括spring-boot-starter-jdbc
依赖。
数据库为Oracle 11g。
主要配置:
@SpringBootApplication
@EnableTransactionManagement
public class MyApplication extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
服务层:
@Service
public class MyBean {
private final NamedParameterJdbcTemplate jdbcTemplate;
private final MyDAO myDao;
@Autowired
public MyBean (NamedParameterJdbcTemplate jdbcTemplate, MyDAO myDao) {
this.jdbcTemplate = jdbcTemplate;
this.myDao= myDao;
}
@Override
@Transactional
public void saveData(...){
myDao.saveData(jdbcTemplate, ...);
}
}
DAO:
public void saveData(jdbcTemplate, ...){
saveDataInParentDatatable(jdbcTemplate, ...);
saveDataInChildDatatable(jdbcTemplate, ...);
}
private void saveDataInChildDatatable(jdbcTemplate, ...){
throw new RuntimeException();
}
发布于 2018-06-01 18:00:12
试试这个:
@Transactional(propagation = Propagation.MANDATORY, rollbackFor = Exception.class)
发布于 2018-06-01 17:03:40
推荐使用:
@Transactional(rollbackFor = {Exception.class, RuntimeException.class})
https://stackoverflow.com/questions/50639361
复制相似问题