Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一致性和容器管理事务_Java_Jboss_Transactions_Ejb_Oracle Coherence - Fatal编程技术网

Java 一致性和容器管理事务

Java 一致性和容器管理事务,java,jboss,transactions,ejb,oracle-coherence,Java,Jboss,Transactions,Ejb,Oracle Coherence,我正在实现同时写入数据库和Oracle Coherence 3.7.1,并希望使整个操作具有事务性 我想对我的方法提出批评 目前,我创建了如下façade类: public class Facade { @EJB private JdbcDao jdbcDao; @EJB private CoherenceDao coherenceDao; @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

我正在实现同时写入数据库和Oracle Coherence 3.7.1,并希望使整个操作具有事务性

我想对我的方法提出批评

目前,我创建了如下façade类:

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);
   }
}
公共类Facade{
@EJB
私有JdbcDao JdbcDao;
@EJB
私人相干(o)相干(o);;
@TransactionAttribute(TransactionAttributeType.REQUIRES\u NEW)
私有void更新方法(列表){
jdbcDao.update(列表);
一致性更新(列表);
}
}
我想JDBCDAO不需要对事务做任何特定的事情,如果发生了某种情况,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
   }
}
公共类JdbcDao{
私有无效更新(列表){
//我想对于交易我没有什么具体的事情要做。
//如果我不捕捉任何异常,它就可以正常工作
}
}
这是有趣的部分。如何实现一致性支持事务? 我想我应该在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();
      }

   }
}
公共类一致性DAO{
私有无效更新(列表){
//我应该如何使其具有事务性?
//我想它应该以某种方式抛出RuntimeException?
TransactionMap mapTx=CacheFactory.getLocalTransaction(缓存);
mapTx.setTransactionIsolation(TransactionMap.TRANSACTION\u REPEATABLE\u GET);
setConcurrency(TransactionMap.CONCUR_悲观);
//将缓存收集到集合中
Collection txnCollection=Collections.singleton(mapTx);
试一试{
mapTx.begin();
//在这里输入mapTx
CacheFactory.commitTransactionCollection(txnCollection,1);
}捕获(可丢弃的t){
CacheFactory.rollbackTransactionCollection(txnCollection);
抛出新的RuntimeException();
}
}
}

这种方法会像预期的那样有效吗

我知道你一年前问过这个问题,一年后我现在的回答对你来说可能没有那么有价值,但我还是试一试

只要在调用
coherendao.update(list)的方法之后没有RuneTimeException,您尝试做的事情就会起作用您可能会假设在该行之后没有任何代码行,但这不是全部

例如:数据库中可能有一些可延迟的约束。当容器试图提交在
updateMethod(List)
方法退出时以及在方法调用
coherenceDao.update(List)
之后的事务时,将应用这些约束。另一种情况类似于在执行
coherenceDao.update(list)
之后但仍在事务提交之前到数据库的连接超时。 在这两种情况下,CoherenceDAO类的更新方法都安全可靠地执行,并且coherence事务不再回滚,这将使缓存处于不一致的状态,因为这些DB或Hibernate异常将导致RuneTimeException,这将导致容器管理的事务回滚