Error

[JPA] No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call;

paran21 2022. 5. 13. 11:36

특정 조건에서는 delete 후에 save가 되는 로직이 있었는데,

다음의 에러가 발생했다.

 

Resolved [org.springframework.dao.InvalidDataAccessApiUsageException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call

 

삭제하는 로직이 기존에 JPA에서 제공하는 메서드가 아니라 변수명을 활용해서 새로 만든 메서드였는데(deleteByRegDate), 여기에 @Transactional을 붙이니 문제가 해결되었다.

@Transactional
void deleteByRegDate(LocalDate regDate);

 

기본적으로 JPA가 제공하는 기본 메서드에는 @Transactional 어노테이션이 붙어있는데,

@Transactional
@Override
public <S extends T> List<S> saveAll(Iterable<S> entities) {

   Assert.notNull(entities, "Entities must not be null!");

   List<S> result = new ArrayList<>();

   for (S entity : entities) {
      result.add(save(entity));
   }

   return result;
}

이 경우에는 새로 만든 메서드라 @Transactional 어노테이션이 없는 상태에서는 에러가 발생한 거였다.

 

왜 메서드마다 @Transactional이 붙어야 하는가, 에 대해서는 JPA 공부하면서 찾아보기!

'Error' 카테고리의 다른 글

#Springboot - MySQL : Field 'id' doesn't have a default value  (0) 2022.04.19