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

    Jboss cache TransactionCompletedEvent in Jboss AS 5.1

      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:

      @CacheListener
      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;
                  }
                  cache.start();
                  cache.addCacheListener(this);

                  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 {
                  transMgr.begin();
                  loadConfig();  // load data to the cache through bunch of puts
                  transMgr.commit();
              } catch (Exception e) {
                  try { transMgr.rollback(); } catch(Throwable t) {}
                  logger.error("Unable to load cluster configuration; " + e);
                  return false;
              }


              return true;
          }
         
          @TransactionCompleted
          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.");
                  return;
              }
              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.