JPA TopLink - 프로세스 시작 부분에서 활성 트랜잭션을 확인할 필요가 있습니까?
-
13-11-2019 - |
문제
프로세스 시작시 트랜잭션이 현재 활성화되었는지 여부를 확인하는 인터넷에서 몇 가지 샘플을 보았습니다.
내 코드는 공장에서 EntityManager를 가져 오는 코드입니다.
나는 이유를 도 불러 일으키는지, 거래가 시작되기 전에 거래가 활성화되었는지 여부를 확인 해야하는 이유는 무엇입니까?
는 동일한 EntityManager 인스턴스를 사용하는 것과 동일한 프로세스가 사용될 수 있기 때문입니까?(EntityManagerFactory는 싱글 톤이지만 EntityManager가 아닙니다)
. @Path("update")
@PUT
@Consumes("application/json")
public Response machineUpdate(String content) {
JSONObject jObj = null;
EntityManager em = null;
EntityTransaction txn = null;
try {
JSONObject jObj = new JSONObject(content);
em = EmProvider.getInstance().getEntityManagerFactory().createEntityManager();
//what's this line doing here???
if(em.getTransaction().isActive()) {
return HttpStatusHandler.sendConflict();
}
txn = em.getTransaction();
txn.begin();
//more process ......
}
catch(.....
해결책
JPA 트랜잭션 API를 사용하는 코드가 지정된 트랜잭션 검사의 이유가 없으므로 EntityManager가 방금 생성 된 트랜잭션이 활성화 될 수있는 방법이 없습니다.
JTA Managed EntityManager를 사용하는 경우 JTA 트랜잭션이 이미 활성화 될 수 있습니다.그러나 JTA의 경우 JPA 트랜잭션을 사용하여 트랜잭션을 시작할 수 없으므로 JTA로 트랜잭션 시작 또는 JPA에서 JOINTRANSACTION ()을 사용합니다.