【问题标题】:Set Ehcache timeout when using JTA使用 JTA 时设置 Ehcache 超时
【发布时间】:2015-02-17 17:19:00
【问题描述】:

我已使用设置 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;
}

任何指针将不胜感激。

【问题讨论】:

    标签: java spring ehcache atomikos


    【解决方案1】:

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

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

    在xml中:

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

    或在代码中:

    new Configuration().defaultTransactionTimeoutInSeconds(120);
    

    【讨论】:

      【解决方案2】:

      您应该将超时设置为 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);
      

      【讨论】:

        猜你喜欢
        • 2013-08-28
        • 2019-07-25
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 2017-02-27
        • 2021-03-05
        • 2020-10-12
        • 1970-01-01
        相关资源
        最近更新 更多