2012-01-25 22 views
3

私は同じトランザクションエンティティで2つの異なるDB呼び出しをしようとしています。私はbegin()commit()の間で両方の質問をすることができることを知っていますが、私は教育目的のためだけにこれを試しています。JPA:複数のトランザクション

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)は、あなたが最初に(コミット間のEntityManagerからトランザクションをreobtainどうなり工場

関連する問題