0

我已经使用设置将 Ehcache 连接到我的 JTA 事务管理器(由 Atomikos 提供)中cacheConfiguration.setTransactionalMode("xa");,并且在我的应用程序启动大约 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;
}

任何指针将不胜感激。

4

2 回答 2

0

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

此外,Spring 的 'org.springframework.cache.ehcache.EhCacheManagerFactoryBean' 没有公开 defaultTransactionTimeoutSeconds 的设置器,以便能够在 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);
于 2015-03-27T15:06:18.480 回答
0

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

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

在 xml 中:

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

或在代码中:

new Configuration().defaultTransactionTimeoutInSeconds(120);
于 2015-02-18T15:21:20.153 回答