Spring@Transactional回滚不工作
我希望在抛出RuntimeException时回滚在Spring@Transactional回滚不工作,spring,spring-mvc,Spring,Spring Mvc,我希望在抛出RuntimeException时回滚在CancelReturnApplication方法中创建的事务,如果没有抛出RuntimeException,则将其提交。但是我发现即使抛出RuntimeException,事务也不会回滚。对于测试perpose,我更改代码使其始终抛出EntityNotFoundException,并使用以下测试方法对其进行测试。运行测试后,我检查数据库,发现退款应用程序数据被删除,这意味着事务没有回滚,并且@Transactional注释不起作用 @Rest
CancelReturnApplication
方法中创建的事务,如果没有抛出RuntimeException,则将其提交。但是我发现即使抛出RuntimeException,事务也不会回滚。对于测试perpose,我更改代码使其始终抛出EntityNotFoundException,并使用以下测试方法对其进行测试。运行测试后,我检查数据库,发现退款应用程序数据被删除,这意味着事务没有回滚,并且@Transactional
注释不起作用
@RestController
@RequestMapping("/Api/Order")
public class OrderController {
private OrderService service;
private RefundService refundService;
@AsCustomer
@DeleteMapping(value = "/{orderID}/RefundApplication")
@Transactional(rollbackFor = RuntimeException.class)
public Map cancelRefundApplication(@SessionAttribute("user") User user,
@PathVariable("orderID") String orderID) {
Order order = service.getOrderByID(orderID);
RefundApplication application = refundService.get(orderID);
order.setState(Order.STATE_PAYED);
refundService.delete(orderID);
service.updateOrder(order);
throw new EntityNotFoundException("test");
}
...
选中在测试中是否包含
TransactionalTestExecutionListener
,如果没有添加:@TestExecutionListeners(listeners={TransactionalTestExecutionListener.class})
需要通过使用@EnableTransactionManagement
注释config类来手动启用事务。您需要向测试添加@Transactional
注释,您还可以提供属性
文件和数据配置类
@shahaf如果我将@Transactional
添加到测试方法中,即使没有引发异常,它也将始终回滚,这不是我想要的。我已经更新了问题以提供DataConfig类。我显然是想使用相同的注释@Transactional(rollboor=RuntimeException.class)
,现在也更改您的数据库用户名和密码@我发现我粘贴了错误的测试方法。我再次更新了问题。谢谢你提醒我。你在哪里启用了交易?
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {WebConfig.class, RootConfig.class, DataConfig.class})
@WebAppConfiguration
class OrderControllerTest {
@Autowired
OrderController controller;
@Autowired
UserService userService;
@Autowired
OrderService orderService;
@Autowired
AppWideExceptionHandler exceptionHandler;
private User customer;
private User seller;
private HashMap<String, Object> sessionAttrs;
private ResultMatcher success = jsonPath("$.code")
.value("0");
private MockMvc mockMvc;
@Test
void cancelRefundApplication() throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
String path = String.format("/Api/Order/%s%d0001/RefundApplication"
, simpleDateFormat.format(new Date()), customer.getID());
mockMvc.perform(delete(path)
.characterEncoding("UTF-8")
.sessionAttrs(sessionAttrs))
.andDo(print())
.andExpect(success);
}
...
@Configuration
@MapperScan("youshu.mapper")
public class DataConfig {
@Bean
public DataSource dataSource() {
// org.apache.ibatis.logging.LogFactory.useLog4J2Logging();
PooledDataSource pds = new PooledDataSource();
pds.setDriver("com.mysql.cj.jdbc.Driver");
pds.setUsername(...);
pds.setPassword(...);
pds.setUrl("jdbc:mysql://XXXX");
return pds;
}
@Bean
public JdbcOperations jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setTypeAliasesPackage("youshu.entity");
return sessionFactory.getObject();
}
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public SqlSessionTemplate sqlSession(SqlSessionFactory factory){
return new SqlSessionTemplate(factory);
}
}