У меня есть сервис с методом findById, помеченным @Transactional(readOnly = true), и другим методом, который выполняет мягкое удаление.
Вместо повторения вызова можно ли вызвать репозиторий findById(id) внутри метода excluir?
IntelliJ обычно предупреждает, когда метод @Transactional вызывается из другого метода @Transactional. Однако в этом случае, поскольку findById помечен как readOnly = true, предупреждение не выдается.
Я хотел бы понять, безопасен ли этот подход и существует ли какая-либо рекомендуемая передовая практика в отношении границ транзакций в этом сценарии.
Код: Выделить всё
@Service
@RequiredArgsConstructor
public class ProdutoService {
private final ProductRepository productRepository;
@Transactional(readOnly = true)
public Product findById(UUID id){
return productRepository.findByIdAndActiveTrue(id)
.orElseThrow(() -> new NotFoundException("Could not found a product with the given ID"));
}
@Transactional
public void excluir(UUID id){
Product product = productRepository.findByIdAndActiveTrue(id)
.orElseThrow(() -> new NotFoundException("Could not found a product with the given ID"));
product.setActive(false);
productRepository.save(product);
}
}
Код: Выделить всё
@Service
@RequiredArgsConstructor
public class ProdutoService {
private final ProductRepository productRepository;
@Transactional(readOnly = true)
public Product findById(UUID id){
return productRepository.findByIdAndActiveTrue(id)
.orElseThrow(() -> new NotFoundException("Could not found a product with the given ID"));
}
@Transactional
public void excluir(UUID id){
Product product = findById(id)
product.setActive(false);
productRepository.save(product);
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... inside-ano
Мобильная версия