我已经实现了一个类,我需要将一些数据插入数据库,但如果出现问题,插入必须回滚。一旦我执行了插入,我就会抛出一个异常来测试回滚,但是一旦我检查数据库,行就会被插入,这意味着回滚从未发生。
import org.springframework.transaction.annotation.Transactional;
public class SomeClass{
@Autowired
private JdbcTemplate jdbcTemplate;
@Transactional
public void insertToDb() throws Exception{
String sql = "INSERT STUFF"
jdbcTemplate.update(sql);
throw new Exception();
}
}
Run Code Online (Sandbox Code Playgroud)