0 Replies Latest reply on Nov 24, 2008 3:54 PM by Marek Rolsky

    Jboss Cache rollback

    Marek Rolsky Newbie

      I am working on using two Jboss Caches for my Web application. The first one is used by Hibernate as second level cache. The other is multipurpose cache for other objects (bean PojoCache ) .
      I am having synchronization issues in case of exception. When I throw an exception the Hibernate transaction is being rollback, however the second cache contains invalid values that should be rollbacked.
      My question is how to configure my for cache for rollback ( PojoCache ).
      The application is bases on Spring 2.5 , Hibernate 3.3 , Jboss Cache 3.0 and Jboss AS 5.0.

      Spring config :

      <bean id="hibernateSessionFactory"
       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      
       <property name="dataSource" ref="dataSourceJndi" />
       <property name="hibernateProperties">
       <props>
       <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect
       </prop>
       <prop key="hibernate.hbm2ddl.auto">update</prop>
       <prop key="hibernate.show_sql">true</prop>
       <prop key="hibernate.format_sql">false</prop>
       <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory
       </prop>
      
       <prop key="hibernate.transaction.manager_lookup_class">
       org.hibernate.transaction.JBossTransactionManagerLookup</prop>
      
       <prop key="hibernate.cache.region.factory_class">
       org.hibernate.cache.jbc2.SharedJBossCacheRegionFactory</prop>
      
       <prop key="hibernate.cache.region.jbc2.cfg.shared">mvcc.xml</prop>
      
       <aop:aspectj-autoproxy />
      
       <bean id="transactionManager"
       class="org.springframework.transaction.jta.JtaTransactionManager">
       </bean>
      
       <tx:annotation-driven transaction-manager="transactionManager" />
      
      


      Obtaining Cache

       MBeanServer server = MBeanServerLocator.locateJBoss();
       ObjectName on = new ObjectName("jboss.cache:service=Cache2");
       CacheJmxWrapperMBean cacheWrapper = (CacheJmxWrapperMBean) MBeanServerInvocationHandler
       .newProxyInstance(server, on, CacheJmxWrapperMBean.class, false);
       cache = cacheWrapper.getCache();
      
      


      The database is rollbacked , but cache is updated with invalid values.
      My method that I would like to rollback changes to cache:

       @Transactional(propagation = Propagation.REQUIRES_NEW)
       @Override
       public void add(Product p) {
       getHibernateTemplate().save(p);
      
       PojoCache pc = new PojoCache();
       pc.setId(p.getId());
       pc.setCounter(p.getCounter());
      
       cache.put(pc);
      
       PojoCache p2 = cache.get(-1);
       if (p2 == null) {
      
       PojoCache pc2 = new PojoCache();
       pc2.setId(-1);
       pc2.setCounter(1);
       cache.put(pc2);
       } else {
       p2.setCounter(p2.getCounter() + 1);
       cache.put(p2);
       }
      
       Date date = new Date();
       if (date.getTime() % 10 == 0) {
       throw new RuntimeException();
       }
      


      Thanks,
      Marek