GitHub
Search
Issue
- Update되야 할 상품이 새로운 ID값을 부여받고 새로운 Entity로 저장되어 중복 상품이 나온 상황
Environment
- Multi-Thread 환경
- Thread 당 하나의 트랜잭션
- Each thread selects data based on BroadcastDT and ShoppingChannel from DB
EntityManager is Not Thread-Safe
기본적으로 EntityManager는 Thread-safe하지 않다. 즉, 스레드간에 EntityManager를 공유하면 안된다고한다.
Hibernate 문서에서는 EntityManager의 동시성 문제에 대해 기술하고 있는데 요약하면 아래와 같다.
(원문 : https://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/transactions.html#transactions-basics-issues)
•
EntityManager는 스레드에 안전하지 않기 때문에 멀티 스레드 환경 예를 들면 HTTP requests, session beans, Swing works 같은 곳에서 공유하여 사용하면 경쟁 상태(Race condition) 가 발생할 수 있다.
•
EntityManager에서의 발생할 수 있는 예외(Exception)은 데이터 베이스 트랜젝션(database transaction)이 롤백(rollback) 해야 한다는 의미이고 만약 EntityManager를 애플리케이션 단위라면 애플리케이션이 정지해야 한다.
•
영속성 컨텍스트(persistence context)는 객체(여기서는 객체는 Entity)의 상태를 캐시에 저장하고 있기 때문에 OutOfMemoryException가 발생 할때 까지 끝없이 커질 수 있다.
Thread-safe하게 EntityManager를 사용하려면?
JPA in Multi-thread env
진행 중