본문 바로가기
Spring/Spring Data JPA

No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call

by 아이티.파머 2022. 5. 24.
반응형

EntityManager 를 이용하여 mearge 문을 사용하거나 insert를 사용할때... 다음과 같은 오류가 났다.

 No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call

transaction 관련된 설정이 없다고해서 확인해보니 
역시나...   @Transactional 을 설정하지 않았다.

@Transactional
public <T extends AbstractAdEntity> void update(T abstractAdEntity) {

    try {
        entityManager.merge(abstractAdEntity);
        entityManager.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        entityManager.close();
    }

}

 

The @PersistenceContext annotation has an optional attribute type, which defaults to PersistenceContextType.TRANSACTION. This default is what you need to receive a shared EntityManager proxy. The alternative, PersistenceContextType.EXTENDED, is a completely different affair: This results in a so-called extended EntityManager, which is not thread-safe and hence must not be used in a concurrently accessed component such as a Spring-managed singleton bean. Extended EntityManagers are only supposed to be used in stateful components that, for example, reside in a session, with the lifecycle of the EntityManager not tied to a current transaction but rather being completely up to the application.

EntityManager 의 트랜젝션을 어플리케이션에서 위임해서 사용한다.   어플리케이션의 로직에 @Transaction 을 사용 할것.

 

https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#transaction

 

Data Access

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies (such as JDBC, Hibernate, or JPA) in a consistent way. This lets you switch between the aforementioned persistence technologies fairly easily, a

docs.spring.io

 

 

 

 

반응형