3 Replies Latest reply on Feb 11, 2010 10:15 AM by Frank Cheng

    Jboss cache TransactionCompletedEvent in Jboss AS 5.1

    Frank Cheng Newbie
      I am trying to add transaction support to Jboss Cache. The TransactionComplted listener got hit when the transaction was commited or whenever a read happened to the cache. The cache root in TransactionCompletedEvent is always null.

      Here is the code I am using:

      public class JbossCacheListener {
          private static Log logger = LogFactory.getLog(JbossCacheListener.class);
          public static String DEFAULT_CONFIC_CACHE_NAME = "config-default-name";
          private InitialContext context;
          private Cache cache;
          private TransactionManager transMgr;
          public boolean init(){
              try {
                  this.context = new InitialContext();
                  CacheManager cacheManager = (CacheManager)context.lookup("java:CacheManager");
                  cache = cacheManager.getCache(DEFAULT_CONFIC_CACHE_NAME, true);
                  if (cache == null) {
                      logger.error("Unable to find cache " + DEFAULT_CONFIC_CACHE_NAME + ", jboss cache isn't configured properly.");
                      return false;

                  GenericTransactionManagerLookup tranLookup = new GenericTransactionManagerLookup();
                  transMgr = tranLookup.getTransactionManager();
              } catch (NamingException e1) {
                  logger.error("unable to find initial context by naming service; " + e1);
                  return false;
              } catch (Exception e) {
                  logger.error("unable to find initial context by naming service; " + e);
                  return false;

              try {
                  loadConfig();  // load data to the cache through bunch of puts
              } catch (Exception e) {
                  try { transMgr.rollback(); } catch(Throwable t) {}
                  logger.error("Unable to load cluster configuration; " + e);
                  return false;

              return true;
          public void perform(TransactionCompletedEvent te)
              logger.info("An trans complete event " + te.getType() + " isOriginLocal " + te.isOriginLocal() + " isSuccessful " + te.isSuccessful() + " has occured");
              if (te.getCache().getRoot() == null) {
                  logger.info("cache root is null from TransactionCompletedEvent after complete.");
              logger.info("transEvent: " + te.getTransaction().toString());
          private void loadConfig() {
              Node rootNode = cache.getRoot();
              Node personRecordsNode = rootNode.addChild(Fqn.fromString("/org/mycompany/personRecords"));    
              personRecordsNode.put("name", "Peter");
              personRecordsNode.put("address", "1 main st");

      And here is some of the configuration properties.

               <property name="transactionManagerLookupClass">org.jboss.cache.transaction.GenericTransactionManagerLookup</property>
               <property name="nodeLockingScheme">OPTIMISTIC</property>
               <property name="useLockStriping">false</property>
               <property name="cacheMode">REPL_ASYNC</property>

      Perform function gots called and te.getCache().getRoot() is null. And also, perform gots call whenerver a read happened on any of the nodes in the cache and te.getCache().getRoot() is null.

      Any idea on this? Thanks in advance.