Java 使用JTA时设置Ehcache超时

Java 使用JTA时设置Ehcache超时,java,spring,ehcache,atomikos,Java,Spring,Ehcache,Atomikos,我已经使用设置cacheConfiguration.setTransactionalMode(“xa”)将Ehcache连接到我的JTA事务管理器(由Atomikos提供),我在应用程序启动大约15秒后收到以下错误: Caused by: net.sf.ehcache.transaction.TransactionTimeoutException: transaction [0] timed out at net.sf.ehcache.transaction.local.LocalTra

我已经使用设置
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中:


...
或在代码中:

newconfiguration().defaultTransactionTimeoutInSeconds(120);

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

此外,Spring的“org.springframework.cache.ehcache.EhCacheManagerFactoryBean”没有公开DefaultTransactionTimeouts的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环境中注入值。然后,在AfterPropertieSet()中,我编写了以下代码:

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