• New Feature
  • Status: Open
  • 2 Major
  • Resolution:
  • prodmgmt
  • Reporter: zaraki
  • April 19, 2011
  • 0
  • Watchers: 3
  • April 26, 2011


Idea is to configure cache via ehcache.xml initially without transactionalMode and introduce transactionalMode at Runtime.


Tim Eck 2011-04-19

I think you might be able to do this with something like this:

Configuration config = ConfigurationFactory.parseConfiguration(getClass() .getResourceAsStream(“/path/to/your/config/ehcache.xml”)); CacheConfiguration cacheConfig = config.getCacheConfigurations().get(“foo”);

// add the tx config as desired

// new up the CacheManager with the modified config object

h k 2011-04-19

Thanks Tim will check it out,

h k 2011-04-20

Hi Tim but wouldn’t that be a new cache altogether?

Tim Eck 2011-04-20

I’m maybe not following exactly what you want to do. There isn’t a way to alter an existing cache instance that has been initialized to turn on transactions. The code I’m suggesting is just a way to mutate some static config (from an ehcache.xml) at runtime before you load the caches. Perhaps that is not what you’re after?

For the sake of completeness here is a longer code sample. This code creates two cache managers using the same base config file but in one the transactional mode is altered. {quote} Configuration config1 = ConfigurationFactory.parseConfiguration(getClass().getResourceAsStream(“/ehcache.xml”)); CacheConfiguration cacheConfig = config1.getCacheConfigurations().get(“test”);

    // enable local TX

    CacheManager cacheManager1 = new CacheManager(config1);

    Configuration config2 = ConfigurationFactory.parseConfiguration(Foo.class.getResourceAsStream("/ehcache.xml"));
    CacheManager cacheManager2 = new CacheManager(config2);

    System.err.println(cacheManager2.getCache("test").getCacheConfiguration().getTransactionalMode()); \{quote\}

The ehcache.xml is just something like this: {quote}


h k 2011-04-20

Thanks Tim for extended response, yes “isn’t a way to alter an existing cache instance that has been initialized to turn on transactions” was the very functionality I was looking for, I did end up writing code similar to what you’ve posted, Just wanted to verify if there was any other way to alter cache Tx behaviour at runtime. Thanks

Tim Eck 2011-04-20

okee doke. Well I suppose this still stand than as a new feature request. I don’t know if enabling transactional support on a live cache would challenging or not.

It might be good to know your use case a little more here though. In particular why the need for transactional support cannot be determined before the cache is initialized. Or do you want to mix/match transactional updates/reads to the same underlying cache data or something?

h k 2011-04-20

Hi Tim, The Use case is the shared code being used for two different applications

with Local Transactions not essential in one of them thus boosting performance.

Tim Eck 2011-04-22

I think I understand the use case. I guess what I don’t understand is why the decision to use transactions cannot be made before the cache is initialized?