[英]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.