EHCache 本地事务管理器在删除时超时



我正在做一个合并ehCache的项目。目前,我正在编写JUnit测试来测试各种行为。添加事务管理器时,我开始在某些调用上看到超时问题。

目前正在使用 eHcache 2.10.5,但也尝试了 2.8.4 下面是我的 xml

<cache name="ehCache_1"
maxElementsInMemory="20"
eternal="false"
overflowToDisk="false"
timeToLiveSeconds="50"
timeToIdleSeconds="10"
transactionalMode="local">

下面是我的JUnit

@Test
public void testEHCacheIssue(){
try{
Ehcache ehCache_1 = ehCacheManager.getCache("ehCache_1");
transactionManager = ehCacheManager.getTransactionController();
transactionManager.setDefaultTransactionTimeout(15);
transactionManager.begin();
ehCache_1.put(new Element("1", new TestElementPerson(1, "firstName1", "lastName1","email1@test.com",1234)));
transactionManager.commit();
transactionManager.begin();
ehCache_1.put(new Element("2", new TestElementPerson(2, "firstName3", "lastName3","email3@test.com",1234)));
transactionManager.commit();
transactionManager.begin();
ehCache_1.get("1");
transactionManager.commit();
transactionManager.begin(); 
ehCache_1.remove("1");//Timeout happens here
transactionManager.commit();
}catch(Exception ex){
LOGGER.error("There was an exception", ex);
Assert.fail();
}
}

这会在删除时抛出以下错误:

有一个例外

net.sf.ehcache.transaction.TransactionTimeoutException: transaction [3] 超时时间 net.sf.ehcache.transaction.local.LocalTransactionStore.assertNotTimedOut(LocalTransactionStore.java:108( 在 net.sf.ehcache.transaction.local.LocalTransactionStore.remove(LocalTransactionStore.java:391( 在 net.sf.ehcache.store.AbstractCopyingCacheStore.remove(AbstractCopyingCacheStore.java:110( 在 net.sf.ehcache.store.TxCopyingCacheStore.remove(TxCopyingCacheStore.java:33( at net.sf.ehcache.Cache.removeInternal(Cache.java:2426( at net.sf.ehcache.Cache.remove(Cache.java:2331( at net.sf.ehcache.Cache.remove(Cache.java:2249( at net.sf.ehcache.Cache.remove(Cache.java:2227(

我注意到,如果我做任何涉及放置或删除已经存在的密钥的事情,我会遇到这个问题。我知道事务管理器在开始和提交之间对密钥进行锁定,并且事务管理器的线程有时会在并发环境中超时,但我不确定如果事务管理器在任何严重的时间段内锁定对象的密钥,我该如何使用事务管理器。

超时问题与没有按照文档中的要求为我们正在缓存的对象定义hashCode/toString有关。

https://www.ehcache.org/documentation/2.8/apis/transactions.html#requirements

添加hashCode/toString为自己解决了这个问题。

最新更新