4 Replies Latest reply: May 15, 2012 6:30 AM by Nicolas Filotto RSS

What is the best way to inject the TransactionManager into a cache instance?

Nicolas Filotto Novice

Hi all,

 

I'm aware that by configuration we can indirectly provide the TransactionManager thanks to transactionManagerLookupClass however sometimes it is just simpler to inject it directly especially in case we would like to manage the transaction manager outside ISPN. In JBC, we could do that thanks to the following code:

 

cache.getConfiguration().getRuntimeConfig().setTransactionManager(transactionManager);

 

I could not find any equivalent of this in ISPN, so could you please tell me if the following code is the expected/recommanded way to do the same thing:

 


ComponentRegistry cr = cache.getAdvancedCache().getComponentRegistry();


cr.registerComponent(transactionManager, TransactionManager.class);


cr.rewire();


Thank you in advance for your help,

BR,

Nicolas

  • 1. Re: What is the best way to inject the TransactionManager into a cache instance?
    Galder Zamarreño Master

    In the new config in 5.1.0.CR1, you can do:

     

         ConfigurationBuilder cb = new ConfigurationBuilder();

            cb.transactionManagerLookup(new DummyTransactionManagerLookup());

     

    So, you can pass a transaction manager lookup instance that accesses your transaction manager. There's no need to construct it via reflection, you can just pass your initialized instance.

     

    The code you suggest is a hack

  • 2. Re: What is the best way to inject the TransactionManager into a cache instance?
    Nicolas Filotto Novice

    Thx Galder for your answer however it doesn't totally fulfill my needs as the transaction manager lookup is not a dynamic configuration parameter and since I don't see any way to inject it before creating the cache instance (I mean  manager.getCache(String cacheName, boolean start) doesn't exist), I'm stuck. Maybe there is somthing that I missunderstand as it looks like the new configuration class is immutable so you obviously don't expect that we modify it, if so how can we load first the configuration from the XML files and apply some changes programmatically? I must admit that the new configuration class doesn't seem to be a good news for me since I have many warnings in my code base that I cannot fix with the new configuration class as I rely on dynamic parameters a lot which doesn't seem to be allowed anymore.

  • 3. Re: What is the best way to inject the TransactionManager into a cache instance?
    Galder Zamarreño Master

    Nicolas, you can manually parse the configuration from XML (via Parser.parseFile() method), then apply changes to it (by making modifications to the configuration builder in it), and then create the cache manager + caches.

     

    Once the cache has started, some parameters can be modified but does not include the transaction manager.

  • 4. Re: What is the best way to inject the TransactionManager into a cache instance?
    Nicolas Filotto Novice

    Hi Galder,

     

    It is actually what I already do, indeed I first parse it as next:

     

    Parser parser = new Parser(Thread.currentThread().getContextClassLoader());

    // Loads the configuration from the input stream

    ConfigurationBuilderHolder holder = parser.parse(configStream);

     

     

    Then I inject the TM as below:

     

    ConfigurationBuilder confBuilder = holder.getDefaultConfigurationBuilder();
    if (tm != null)
    {
       TransactionManagerLookup tml = new TransactionManagerLookup()
       {
          public TransactionManager getTransactionManager() throws Exception
          {
             return tm;
          }
       };
       confBuilder.transaction().transactionManagerLookup(tml);
    }