2010-07-19 2 views
2

몇 가지 데이터 저장소 작업을 실행하고 싶지만 각 작업에 대해 지속성 관리자에 대한 새로운 참조를 얻으려면 확실하지 않습니다 (비용이 많이 듭니까?). 예 :다중 데이터 저장소 작업 - 얼마나 많은 지속성 관리자 참조가 필요합니까?

public void submitUserRating(String username, String productId, int val) { 

    PersistenceManagerFactory pmf = PMF.get(); 
    PersistenceManager pm = pmf.getPersistenceManager(); 

    Transaction tx = pm.currentTransaction(); 
    try { 

     // Operation 1, don't care if this fails. 
     try { 
      Rating rating = new Rating(username, val); 
      pm.makePersistent(rating); 
     } catch (Exception ex) { 
      // no big deal, carry on. 
     } 

     // Operation 2... 
     // Operation 3... 
     // Operation N... 
     // all on same pm, ok? 

     // Do transaction, ok to do on same pm reference still? 
     tx.begin(); 
     try { 
      Product product = pm.getObjectById(productId); 
      product.setNumViews(product.getNumViews() + 1); 
      pm.makePersistent(product); 
     } catch (Exception ex) {} 
     } 
     tx.commit(); 
    } 
    finally { 
     if (tx.isActive()) { 
      tx.rollback(); 
     } 
     pm.close(); 
    } 
} 

위의 예는 그 예입니다. 난 그 일을 할 수 있다고 생각하지만, 우리는 두 개의 트랜잭션? 위에서

public void myexample() { 


    PersistenceManagerFactory pmf = PMF.get(); 
    PersistenceManager pm = pmf.getPersistenceManager(); 

    Transaction tx = pm.currentTransaction(); 
    try { 
     // Transaction operation 1 
     tx.begin(); 
     pm.makePersistent(new Blah()); 
     tx.commit(); 

     // Transaction operation 2 
     tx.begin(); 
     pm.makePersistent(new Foo()); 
     tx.commit(); 
    } 
    finally { 
     if (tx.isActive()) { 
      tx.rollback(); 
     } 
     pm.close(); 
    } 
} 

, 작업을 수행하려는 경우 어떤 일 & 2는 독립적 - OP1이 실패 할 경우 즉, 내가 '상관 없어 계속해서 op2를 수행하고 싶습니다.

감사합니다.

+1

+1 PM의 의도 된 수명주기는 항상 나를위한 JDO의 더욱 혼란스러운 측면 중 하나였습니다. :) –

답변

1

하나의 PersistenceManager 만 있으면됩니다.

관련 문제