2012-01-25 3 views
3

동일한 거래 엔티티로 2 개의 다른 DB 호출을하려고합니다. begin()commit() 사이에서 두 가지 질문을 할 수 있다는 것을 알고 있지만 교육적인 목적으로 만 시도하고 있습니다.JPA : multiple transactions

EntityTransaction transaction = em.getTransaction(); 
EventService eventService = new EventService(); 
transaction.begin(); 
Event currentEvent = eventService.read(eventId); 
transaction.commit(); 

if (currentEvent != null){ 
    CommentService commentService = new CommentService(); 
    transaction.begin(); 
    commentList = commentService.getList(1, id, 50); 
    transaction.commit(); 
} 

이 코드 조각을 던졌습니다 :

예외 설명 : 트랜잭션은 현재 활성화되어 내가 이미 열려 거래에 begin()을 시도하고있어 것을 알고 정상

.

두 번째 transaction.begin()을 제외하고 DB로 작업해야 할 때마다 commit()을 사용하는 것이 맞습니까?

LE : 내가는 EclipseLink 및 RESOURCE_LOCAL

+0

입니다()? – MaDa

+0

같은 일이 발생합니다. 첫 번째 .begin() 이후 .isActive()는 EntityManager를 닫을 때까지 항상 true를 반환합니다. – Ionut

+0

em.getClass() 및 transaction.getClass()를 인쇄하여 어떤 구현이 사용되고 있는지 확인할 수 있습니까? EclipseLink의 트랜잭션 구현은 트랜잭션을 commit()의 finally 블록에서 비활성으로 표시하므로 래퍼가 직접 호출을 전달하지 않을 수 있습니다. 사용중인 EclipseLink 버전은 무엇입니까? – Chris

답변

2

transacton-typeRESOURCE_LOCAL으로 설정되어 있기 때문에 이러한 현상이 발생합니다. 이 경우 EntityManagerEntityTransaction을 처리해야하는 일부 SingleTon 클래스를 만들어야합니다.

0

홀수를 사용하고 있습니다. 이것은 효과가있다. 어떤 JPA 공급자를 사용하고 있습니까? 아마도 무슨 일이 벌어지고 있는지 로깅을 가능하게 할 수 있습니다.

+0

EclipseLink이고 트랜잭션 유형은 RESOURCE_LOCAL입니다. – Ionut

-1
private static EntityManager picassoEm = null; 

public static synchronized boolean insertToDB(EntityObject eobj) { 
    try { 
     if (picassoEm == null) { 
      picassoEm = JPAUnit.getEntityManagerFactoryPicasso().createEntityManager(); 

     } 
     EntityTransaction eT = picassoEm.getTransaction(); 
     eT.begin(); 
     picassoEm.persist(eobj); 
     eT.commit();    
     return true; 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
     return false; 
    } 

} 

JPAUnit 자신이이 거래를 reobtain 경우 먼저 (커밋 사이의 EntityManager에서 발생)와 두 번째 시작 무엇 공장