简体   繁体   English

@Transactional 注释不起作用springboot

[英]@Transactional annotation is not working springboot

I'm using springboot 2.3.0 with mongodb.我正在使用带有 mongodb 的 springboot 2.3.0。 @Transactional annotation is not working for me. @Transactional注释对我不起作用。 It is not able to rollback the transaction in case of any exception.如果出现任何异常,它无法回滚事务。 ps:Using mongoTemplate it is working. ps:使用 mongoTemplate 它正在工作。

Any help will be highly appreciated.任何帮助将不胜感激。

My controller class:我的 controller class:

@PostMapping(value = "/employee/{employeeId}")
public String createEmployee(@PathVariable(value = "employeeId") String employeeId, @RequestBody EmployeePayload payload) {
    
    employeeService.createEmployee(employeeId, payload);
    return "employee successfully added";
}

My serviceImpl class我的服务Impl class

 @Override @Transactional(rollbackFor = {ArithmeticException.class}) public void createEmployee(String employeeId, EmployeePayload payload) { Employee employee = new Employee("1", "Robert", "24"); Document mongoDocument = new Document(); mongoTemplate.getConverter().write(employee, mongoDocument); MongoDatabaseFactory mongoDatabaseFactory = new SimpleMongoClientDatabaseFactory(properties.getUri() + properties.getDataBase() + properties.getDbFilter()); MongoDatabaseUtils.getDatabase(properties.getDataBase(), mongoDatabaseFactory).getCollection("employee").insertOne(mongoDocument); System.out.println(7 / 0); //To throw an exception }

My MongoConfig class我的 MongoConfig class

@Configuration public class MongoDBConfig {

    @Bean
    MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) {
        return new MongoTransactionManager(dbFactory);
    } 
}

You are missing @EnableTransactionManagement annotation.您缺少@EnableTransactionManagement注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM