본문 바로가기
개발공부/시행착오

Spring - No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call 에러 해결

by dding-g 2020. 2. 28.

https://stackoverflow.com/questions/32269192/spring-no-entitymanager-with-actual-transaction-available-for-current-thread

 

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

I get this error when trying to invoke "persist" method to save entity model to database in my Spring MVC web application. Can't really find any post or page in internet that can relate to this

stackoverflow.com

 

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.

 

spring boot jpa 환경에서

 

db에 delete 명령을 보내 row를 삭제하려는 시도 중 발생하였다. 

 

@Transaction 어노테이션을 붙여주면 해결된다. 중복된 request를 방지하려는 목적 이라고 생각된다.