简体   繁体   English

一致性和容器管理的交易

[英]Coherence and container managed transactions

I'm implementing simultaneous write into database and Oracle Coherence 3.7.1 and want to make whole operation transactional. 我正在实现同时写入数据库和Oracle Coherence 3.7.1的功能,并且希望使整个操作具有事务性。

I would like to have a critique on my approach. 我想对我的方法提出批评。

Currently, I've created façade class like this: 目前,我已经创建了如下外观类:

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);
   }
}

I guess JDBC DAO would not need to do anything specific about transactions, it something happens Hibernate would throw some kind of RuntimeException. 我猜想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
   }
}

Here is interesting part. 这是有趣的部分。 How do I make Coherence support transactions? 如何进行Coherence支持交易? I guess I should open coherence transaction inside update() method and on any exceptions inside it I should throw RuntimeException myself? 我想我应该在update()方法中打开一致性事务,并且在其中发生任何异常时都应该自己抛出RuntimeException?

I currently thinking of something like this: 我目前正在考虑这样的事情:

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();
      }

   }
}

Would this approach work as expected? 这种方法会按预期工作吗?

I know that you asked this question a year ago and my answer now might not be as much as value for you after a year but I still give it a try. 我知道您一年前曾问过这个问题,现在我的回答可能不及一年后给您带来的价值,但我仍然尝试一下。

What you are trying to do works as long as there is no RuneTimeException after the method call of coherenceDao.update(list); 只要在coherenceDao.update(list);方法调用之后没有RuneTimeException,您尝试做的事情就可以起作用coherenceDao.update(list); You might be assuming that you don't have any line of codes after that line but that's not the whole story. 您可能会假设在该行之后没有任何代码行,但这不是全部。

As an example: You might have some deferrable constraints in your Database. 例如:您的数据库中可能有一些延迟的约束。 Those constraints will be applied when the container is trying to commit the transaction which is on method exit of updateMethod(List<DomainObject> list) and after your method call to coherenceDao.update(list) . 当容器尝试提交在updateMethod(List<DomainObject> list)方法出口处的事务时,以及在对coherenceDao.update(list)方法调用之后,将应用这些约束。 Another cases would be like a connection timeout to database after that coherenceDao.update(list) is executed but still before the transaction commit. 另一种情况是执行coherenceDao.update(list)之后但仍在事务提交之前与数据库的连接超时。 In both cases your update method of CoherenceDAO class is executed safe and sound and your coherence transaction is not rollbacked anymore which will put your cache in an inconsistent state because you will get a RuneTimeException because of those DB or Hibernate Exceptions and that will cause your container managed transaction to be rollbacked! 在这两种情况下,您的CoherenceDAO类的更新方法均安全可靠地执行,并且您的一致性事务不再回滚,这会使您的缓存处于不一致状态,因为由于这些DB或Hibernate Exceptions您将获得RuneTimeException并导致容器托管事务将被回滚!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM