I am writing Spring Boot application using Spring Data repositories. I have method that resets database and fills it with sample data. It works, however Spring uses hundreds of transactions to do this. Is there any way to limit number of transactions created by repositories to 1 or to not use them at all?
I would like to reuse same transaction within fillApples
and fillBananas
methods. I've tried using different combinations of @Transactional(propagation = Propagation.SUPPORTS)
but it does not change anything.
interface AppleRepository extends CrudRepository<Apple, Long>
interface BananaRepository extends JpaRepository<Banana, Long>
@Service
public class FruitService{
@Autowired
private final AppleRepository appleRepository;
@Autowired
private final BananaRepository bananaRepository;
public void reset(){
clearDb();
fillApples();
fillBananas();
//more fill methods
}
private void clearDb(){
appleRepository.deleteAll();
bananaRepository.deleteAll();
}
private void fillApples(){
for(int i = 0; i < n; i++){
Apple apple = new Apple(...);
appleRepository.save(apple);
}
}
private void fillBananas(){
for(int i = 0; i < n; i++){
Banana banana = new Banana(...);
bananaRepository.save(banana);
}
}
}
@RestController
public class FruitController{
@Autowired
private FruitService fruitService;
@RequestMapping(...)
public void reset(){
fruitService.reset();
}
}