Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring@Transactional回滚不工作_Spring_Spring Mvc - Fatal编程技术网

Spring@Transactional回滚不工作

Spring@Transactional回滚不工作,spring,spring-mvc,Spring,Spring Mvc,我希望在抛出RuntimeException时回滚在CancelReturnApplication方法中创建的事务,如果没有抛出RuntimeException,则将其提交。但是我发现即使抛出RuntimeException,事务也不会回滚。对于测试perpose,我更改代码使其始终抛出EntityNotFoundException,并使用以下测试方法对其进行测试。运行测试后,我检查数据库,发现退款应用程序数据被删除,这意味着事务没有回滚,并且@Transactional注释不起作用 @Rest

我希望在抛出RuntimeException时回滚在
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);
    }

}