簡體   English   中英

一致性和容器管理的交易

[英]Coherence and container managed transactions

我正在實現同時寫入數據庫和Oracle Coherence 3.7.1的功能,並且希望使整個操作具有事務性。

我想對我的方法提出批評。

目前,我已經創建了如下外觀類:

public class Facade {
   @EJB
   private JdbcDao jdbcDao;
   @EJB
   private CoherenceDao coherenceDao;

   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
   private void updateMethod(List<DomainObject> list) {
      jdbcDao.update(list);
      coherenceDao.update(list);
   }
}

我猜想JDBC DAO不需要對事務做任何特定的事情,因為Hibernate會拋出某種RuntimeException異常。

public class JdbcDao {
   private void update(List<DomainObject> list) {
       // I presume there is nothing specific I have to do about transactions.
       // if I don't catch any exceptions it would work just fine
   }
}

這是有趣的部分。 如何進行Coherence支持交易? 我想我應該在update()方法中打開一致性事務,並且在其中發生任何異常時都應該自己拋出RuntimeException?

我目前正在考慮這樣的事情:

public class CoherenceDao {
   private void update(List<DomainObject> list) {
      // how should I make it transactional?
      // I guess it should somehow throw RuntimeException?

      TransactionMap mapTx = CacheFactory.getLocalTransaction(cache);
      mapTx.setTransactionIsolation(TransactionMap.TRANSACTION_REPEATABLE_GET);
      mapTx.setConcurrency(TransactionMap.CONCUR_PESSIMISTIC);

      // gather the cache(s) into a Collection
      Collection txnCollection = Collections.singleton(mapTx);

      try {
         mapTx.begin();

         // put into mapTx here

         CacheFactory.commitTransactionCollection(txnCollection, 1);
      } catch (Throwable t) {
         CacheFactory.rollbackTransactionCollection(txnCollection);
         throw new RuntimeException();
      }

   }
}

這種方法會按預期工作嗎?

我知道您一年前曾問過這個問題,現在我的回答可能不及一年后給您帶來的價值,但我仍然嘗試一下。

只要在coherenceDao.update(list);方法調用之后沒有RuneTimeException,您嘗試做的事情就可以起作用coherenceDao.update(list); 您可能會假設在該行之后沒有任何代碼行,但這不是全部。

例如:您的數據庫中可能有一些延遲的約束。 當容器嘗試提交在updateMethod(List<DomainObject> list)方法出口處的事務時,以及在對coherenceDao.update(list)方法調用之后,將應用這些約束。 另一種情況是執行coherenceDao.update(list)之后但仍在事務提交之前與數據庫的連接超時。 在這兩種情況下,您的CoherenceDAO類的更新方法均安全可靠地執行,並且您的一致性事務不再回滾,這會使您的緩存處於不一致狀態,因為由於這些DB或Hibernate Exceptions您將獲得RuneTimeException並導致容器托管事務將被回滾!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM