I have a Javers implementation in my Spring Boot application. Mongo 4.4 is used as a database. Starting with MongoDB 4.4, you can create documents in transactions.
I have simulated an exception when creating an object. As expected the object was not created in the database, but a new snapshot was added to the jv_snapshots collection.
Does Javers support transactions for MongoDB?
Repository:
@Repository
@JaversSpringDataAuditable
public interface ProjectRepository extends MongoRepository<Project, UUID> {
}
Service:
@Service
@RequiredArgsConstructor
public class ProjectServiceImpl extends ProjectService {
private final ProjectRepository projectRepository;
@Transactional
@Override
public Project create(Project project) {
Project savedProject = projectRepository.save(project);
// Simulate exception
if (true) throw new RuntimeException("Simulate...");
return savedProject;
}
}
Controller:
@RequestMapping("/api/v1/projects")
@RestController
@AllArgsConstructor
public class ProjectController {
private final ProjectService projectService;
@PostMapping
public ResponseEntity<Project> create(@RequestBody Project entity) {
Project project = projectService.create(entity)
return ResponseEntity.status(HttpStatus.CREATED).body(project);
}
}
Javers does not support transactions for MongoDB because there are no transactions in MongoDB.