-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate & Jboss cache
PostPosted: Wed Sep 22, 2010 5:25 am 
Newbie

Joined: Wed Sep 22, 2010 3:38 am
Posts: 4
Hi all!
I've spent a week trying different solutions for my requirement (hibernate with jboss as 2nd level cache under tomcat 6). At first, it was a problem to configure transaction manager. I've rejected JOTM and use at present Atomikos as it seems to be working fine and sufficiently documented as well.
My configuration:

Spring context:
Code:
   <bean id="placeholderConfig"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
         <list>
            <value>classpath:config.properties</value>
         </list>
      </property>
   </bean>

   <aop:aspectj-autoproxy />
   <tx:annotation-driven transaction-manager="transactionManager" />
   <context:component-scan base-package="com.test" />

   <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
      init-method="init" destroy-method="close">
      <property name="forceShutdown" value="false" />
   </bean>

   <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
      <property name="transactionTimeout" value="300" />
   </bean>

   <bean id="transactionManager"
      class="org.springframework.transaction.jta.JtaTransactionManager">
      <property name="transactionManager" ref="atomikosTransactionManager" />
      <property name="userTransaction" ref="atomikosUserTransaction" />
   </bean>

   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>

   <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
      init-method="init" destroy-method="close">
      <property name="uniqueResourceName">
         <value>XADBMS</value>
      </property>
      <property name="xaDataSourceClassName">
         <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
      </property>
      <property name="xaProperties">
         <props>
            <prop key="user">${ds_username}</prop>
            <prop key="password">${ds_password}</prop>
            <prop key="URL">${ds_url}</prop>
         </props>
      </property>
      <property name="poolSize" value="5" />
   </bean>

   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="packagesToScan">
         <list>
            <value>com.test</value>
         </list>
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">${hibernate_dialect}</prop>
            <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.jbc.MultiplexedJBossCacheRegionFactory</prop>
            <prop key="hibernate.cache.region_prefix">americasjobexchange.cache</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.use_structured_entries">true</prop>
            <prop key="hibernate.cache.jbc.query.localonly">true</prop>
            <prop key="hibernate.cache.jbc.cfg.entity">mvcc-entity</prop>
            <prop key="hibernate.cache.jbc.cfg.timestamps">timestamps-cache</prop>
            <prop key="hibernate.cache.jbc.cfg.query">local-query</prop>
            <prop key="hibernate.cache.jbc.cfg.multiplexer.stacks">jgroups-stacks.xml</prop>
            <prop key="hibernate.cache.jbc.configs">jbc-configs.xml</prop>
            
         </props>
      </property>
   </bean>



Entity:
Code:
package com.test.hibernate;

@Entity
@Table(name="table_name")
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class TestEntity {
   
   private static final long serialVersionUID = 2459140185756215638L;
   private Long id;
   private Integer daily;
   private Integer total;

   public TestEntity() {
      // Default constructor
   }
   
   @Id
   @Column(name = "id", nullable = false, length = 20)
   public Long getId() {
      return this.id;

   }

   public void setId(final Long id) {
      this.id = id;
   }

   @Column(name = "daily", nullable = false)
   public Integer getDaily() {
      return this.daily;

   }

   public void setDaily(final Integer daily) {
      this.daily = daily;
   }

   @Column(name = "total", nullable = false)
   public Integer getTotal() {
      return this.total;

   }

   public void setTotal(final Integer total) {
      this.total = total;
   }

}



Dao:
Code:
@Repository
@Transactional(readOnly = true)
public class TestDao {
   
   @Autowired
   private HibernateTemplate hibernateTemplate;
   
   public List get(){
      Session sess = hibernateTemplate.getSessionFactory().getCurrentSession();
      return sess.createCriteria(TestEntity.class).list();
   }
}


Junit:
Quote:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/context.xml"})
@TransactionConfiguration(transactionManager="transactionManager")
@Transactional

public class TestHb {

@Autowired
private TestDao dao;

@Test
public void testHb(){
System.out.println(dao.get());
System.out.println(dao.get());
System.out.println(dao.get());
}
}

On classpath there are config.properties with db stuff, original jbc-configs.xml, jgroups-stacks.xml, modified transactions.properties, log4j.xml. Nothing special...

My question to hibernate gurus is: What I miss?
I see in logs that every seems to load properly, jgroups cluster starts okay, entity gets cached in jboss cache (used jconsole mbeans to check this), one hibernate session, BUT database is hit 3 times (used mysql query log to check this)?
Do I have to write code to query cache in dao and annotations are not enough?
I use spring 3.0.3.RELEASE & hibernate+jbosscache 3.5.2-Final versions from maven.


Top
 Profile  
 
 Post subject: Re: Hibernate & Jboss cache
PostPosted: Wed Sep 22, 2010 6:36 am 
Newbie

Joined: Wed Sep 22, 2010 3:38 am
Posts: 4
I just turned on statistics. My log contains:
Quote:
INFO - Began transaction (1): transaction manager [org.springframework.transaction.jta.JtaTransactionManager@186167d]; rollback [true]
DEBUG - checking cached query results in region: org.hibernate.cache.StandardQueryCache
DEBUG - query results were not found in cache
Hibernate:
select
testentity0_.id as id0_,
testentity0_.daily as daily0_,
testentity0_.total as total0_
from
table_name testentity0_
DEBUG - caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=12851515476
SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=1,elementCountInMemory=1,elementCountOnDisk=-1,sizeInMemory=-1]
DEBUG - checking cached query results in region: org.hibernate.cache.StandardQueryCache
DEBUG - query results were not found in cache
Hibernate:
select
testentity0_.id as id0_,
testentity0_.daily as daily0_,
testentity0_.total as total0_
from
table_name testentity0_
DEBUG - caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=12851515477
SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=1,elementCountInMemory=1,elementCountOnDisk=-1,sizeInMemory=-1]
DEBUG - checking cached query results in region: org.hibernate.cache.StandardQueryCache
DEBUG - query results were not found in cache
Hibernate:
select
testentity0_.id as id0_,
testentity0_.daily as daily0_,
testentity0_.total as total0_
from
table_name testentity0_
DEBUG - caching query results in region: org.hibernate.cache.StandardQueryCache; timestamp=12851515477
SecondLevelCacheStatistics[hitCount=0,missCount=0,putCount=1,elementCountInMemory=1,elementCountOnDisk=-1,sizeInMemory=-1]
INFO - Rolled back transaction after test execution for test context

I just don't get it why hitCount=0 & missCount=0? :(


Top
 Profile  
 
 Post subject: Re: Hibernate & Jboss cache
PostPosted: Wed Sep 22, 2010 8:23 am 
Newbie

Joined: Wed Sep 22, 2010 3:38 am
Posts: 4
The issue solves by invoking setCachable(true) to criteria.
This is NOT really obvious: I marked entity as cachable, so why do I have to write code to set criteria cachable?


Top
 Profile  
 
 Post subject: Re: Hibernate & Jboss cache
PostPosted: Thu Sep 23, 2010 10:39 am 
Newbie

Joined: Wed Sep 22, 2010 3:38 am
Posts: 4
Another question from my side is how do I properly evict cache regions from hibernate?

I used:
Code:
SessionFactory hsf;
...
Cache cache = hsf.getCache();
cache.evictEntityRegion(regionName);
cache.evictQueryRegion(regionName);


With different configs (mvcc-entity, mvcc-shared) I see the same result:
1.hibernate 2nd lvl cache stats say all evicted
2.jboss cache mbean stats say none is evicted.
Eviction happens after timeToLiveSeconds from eviction policy config.

Is this some kind of replication/synchronization issue?

Very appreciate for any kind of help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.