使用JTA时设置Ehcache超时



我已经使用设置cacheConfiguration.setTransactionalMode("xa");将Ehcache挂接到我的JTA事务管理器(由Atomikos提供)中,并且在我的应用程序启动大约15秒后收到以下错误:

Caused by: net.sf.ehcache.transaction.TransactionTimeoutException: transaction [0] timed out
    at net.sf.ehcache.transaction.local.LocalTransactionStore.assertNotTimedOut(LocalTransactionStore.java:108) ~[ehcache-2.9.0.jar:2.9.0]
    at net.sf.ehcache.transaction.local.LocalTransactionStore.remove(LocalTransactionStore.java:391) ~[ehcache-2.9.0.jar:2.9.0]
    at net.sf.ehcache.transaction.local.JtaLocalTransactionStore.remove(JtaLocalTransactionStore.java:375) ~[ehcache-2.9.0.jar:2.9.0]
    at net.sf.ehcache.store.AbstractCopyingCacheStore.remove(AbstractCopyingCacheStore.java:110) ~[ehcache-2.9.0.jar:2.9.0]
    at net.sf.ehcache.store.TxCopyingCacheStore.remove(TxCopyingCacheStore.java:33) ~[ehcache-2.9.0.jar:2.9.0]
    at net.sf.ehcache.Cache.removeInternal(Cache.java:2401) ~[ehcache-2.9.0.jar:2.9.0]
    at net.sf.ehcache.Cache.remove(Cache.java:2306) ~[ehcache-2.9.0.jar:2.9.0]
    at net.sf.ehcache.Cache.remove(Cache.java:2224) ~[ehcache-2.9.0.jar:2.9.0]

当我的应用程序第一次启动时,它会在一个事务中执行一些初始设置,大约需要60秒才能完成。因此,我需要将15秒的超时时间增加到一个更大的值,但无法找到控制的地方。从Ehcache文档来看,这似乎应该由JTA控制,但我已经为UserTransaction和TransactionManager:设置了默认超时

@Bean
public UserTransaction userTransaction() throws SystemException {
    UserTransactionImp uti = new UserTransactionImp();
    uti.setTransactionTimeout(120000);
    return uti;
}
@Bean(initMethod = "init", destroyMethod = "close")
public TransactionManager transactionManager() throws SystemException {
    UserTransactionManager utm = new UserTransactionManager();
    utm.setForceShutdown(false);
    utm.setTransactionTimeout(120000);
    return utm;
}

任何建议都将不胜感激。

从代码来看,我认为Ehcache需要配置自己的事务超时。

您可以在CacheManager级别配置中执行此操作。

在xml:中

<ehcache ... defaultTransactionTimeoutInSeconds="120" ...>
  ...
</ehcache>

或代码:

new Configuration().defaultTransactionTimeoutInSeconds(120);

您应该将超时设置为Spring Environment属性,以便在其他地方引用它。

此外,Spring的"org.springframework.cache.ehcache.EhCacheManagerFactoryBean"没有公开defaultTransactionTimeoutSeconds的setter,以便能够在Spring创建CacheManager之前修改"配置"。

为了解决您的问题,我创建了一个自定义的EhCacheManagerFactoryBean,而不是使用Spring。我添加了我需要的新属性getter和setter。

// new attribute
private int defaultTransactionTimeoutSeconds;
public void setDefaultTransactionTimeoutSeconds(int value) {
   defaultTransactionTimeoutSeconds = value;
}
public int getDefaultTransactionTimeoutSeconds() {
   return defaultTransactionTimeoutSeconds;
}

从Spring环境中注入值。然后,在afterPropertiesSet()中,我编码为:

Configuration configuration = (this.configLocation != null ?
            EhCacheManagerUtils.parseConfiguration(this.configLocation) : ConfigurationFactory.parseConfiguration());
        configuration.setDefaultTransactionTimeoutInSeconds(this.defaultTransactionTimeoutInSeconds);

最新更新