我正在运行基于 Infinispan 单元测试之一的简单测试用例。在我的测试中,我期望在我的群集中复制超时时收到CacheException
。
我使用悲观的事务锁定,并且由于某种原因,在这种情况下不会抛出异常。如果我评论悲观锁定,我会得到预期的异常。
@Test(groups = "functional", testName = "replication.ReplicationExceptionTest")
public class ReplicationExceptionTest extends MultipleCacheManagersTest {
protected void createCacheManagers() {
ConfigurationBuilder configuration = getDefaultClusteredCacheConfig(CacheMode.REPL_SYNC, true);
configuration.locking()
.lockAcquisitionTimeout(60000l)
.transaction().transactionManagerLookup(new DummyTransactionManagerLookup());
// uncomment this line and exception is not thrown for some reason
//.lockingMode(LockingMode.PESSIMISTIC);
createClusteredCaches(2, configuration);
waitForClusterToForm();
}
@Test(groups = "functional", expectedExceptions = { CacheException.class })
public void testSyncReplTimeout() {
AdvancedCache cache1 = cache(0).getAdvancedCache();
AdvancedCache cache2 = cache(1).getAdvancedCache();
cache2.addInterceptor(new CommandInterceptor() {
@Override
protected Object handleDefault(InvocationContext ctx, VisitableCommand cmd)
throws Throwable {
// Add a delay
Thread.sleep(100);
return super.handleDefault(ctx, cmd);
}
}, 0);
cache1.getCacheConfiguration().clustering().sync().replTimeout(10);
cache2.getCacheConfiguration().clustering().sync().replTimeout(10);
TestingUtil.blockUntilViewsReceived(10000, cache1, cache2);
cache1.put("k", "v");
}
}
有人可以帮助我理解为什么它会在启用悲观锁定的情况下隐藏异常以及如何解决这个问题吗?
更新:我使用的是Infinispan 5.3.0.Final。
答案 0 :(得分:2)
这是因为Infinispan默认将自己注册为事务管理器作为"同步"而不是完整的XA资源。不允许同步在其afterCompletion()
方法中抛出XAExceptions,并且事务管理器(包括DummyTransactionManagerLookup
)会吞下任何运行时异常。
在乐观模式下,在beforeCompletion()
期间复制键和值,允许抛出异常以取消事务。
在悲观模式下,您应该将Infinispan配置为注册为XA资源:
configuration.transaction().useSynchronization(false);
编辑:默认情况下,DummyTransactionManager
不支持XA交易,因此您还需要为XA配置它(甚至更好,使用Narayana):
DummyTransactionManager.getInstance().setUseXaXid(true);