0

I have two services as given below, and both has public methods with @Transactional annotation which is invoked via a self call.

@Service
public class ShopManager {
    @Autowired
    ShopRepository shopRepository;

    public void createShop(List<Shop> shops){
        // validation logic

        // other business logic

        createAllShops(shops);
    }

    @Transactional
    public void createAllShops(List<Shop> shops){
        shopRepository.saveAll(shops);
    }
}

And here is the second service.

@Service
public class ItemManager {
    @Autowired
    ItemRepository itemRepository;

    public void createItem(List<Item> items){
        // validation logic

        // other business logic

        createAllItems(items);
    }

    @Transactional
    public void createAllItems(List<Item> items){
        itemRepository.saveAll(items);
    }
}

As per the documentation Spring would ignore @Transactional annotation on self calls (implicit or explicit) since it uses proxies. However, in one service, the transaction management is working as expected and in the second service @Transactional is ignored.

Any idea why?

vinodpthmn
  • 1,062
  • 14
  • 28
  • 2
    Are you sure the transaction is started by the method with the `@Transactional` and not before, in the case it works? – Nikos Paraskevopoulos Aug 21 '23 at 07:48
  • 1
    duplicate of https://stackoverflow.com/questions/3423972/spring-transaction-method-call-by-the-method-within-the-same-class-does-not-wo – mlecz Aug 21 '23 at 07:50

0 Answers0