java Infinispan缓存在悲观模式下不会引发异常
我正在运行基于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。决赛
# 1 楼答案
这是因为Infinispan在事务管理器中默认注册为“同步”,而不是完整的XA资源。不允许同步在其
afterCompletion()
方法中抛出XAException,事务管理器(包括DummyTransactionManagerLookup
)会吞下任何运行时异常在乐观模式下,在
beforeCompletion()
期间复制键和值,允许抛出异常以取消事务在悲观模式下,您应该将Infinispan配置为注册为XA资源:
编辑:默认情况下,
DummyTransactionManager
不支持XA事务,因此您还需要为XA配置它(或者更好,使用Narayana):