-->
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.  [ 5 posts ] 
Author Message
 Post subject: Query cache is not correctly work
PostPosted: Tue Jul 14, 2009 9:35 am 
Newbie

Joined: Fri Jan 09, 2009 4:31 am
Posts: 9
User user = new User();
user.setAge(25);
user.setName("Mark");

session.save(user);

session.createQuery("FROM User u").setCacheable(true).list();
session.createQuery("FROM User u").setCacheable(true).list();

assertEquals(1, getSessionFactory().getStatistics().getQueryCacheHitCount());

-------------------------------------------------------------------------------------------
java.lang.AssertionError: expected:<1> but was:<0>

I think that hibernate flush user object before every query(why???) and query cache invalidate.
If i not insert user and execute only 2 queries - all ok (cache works).

Any ideas?

----------------------------

User mapping:

@Entity
@Table(name = "user")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User {

@Id
@GeneratedValue
private int id;

@Column(length = 32, nullable = false)
private String name;

@Column(nullable = false)
private int age;

...getters/setters

----------------------------

ehcache.xml:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<defaultCache maxElementsInMemory="0" eternal="true" overflowToDisk="false" />
<cache name="com.teztour.domain.User" maxElementsInMemory="10000" eternal="true" overflowToDisk="false" />
<cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="10000" eternal="true" overflowToDisk="false" />
<cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="10000" eternal="true" overflowToDisk="false" />
</ehcache>


Top
 Profile  
 
 Post subject: Re: Query cache is not correctly work
PostPosted: Tue Jul 14, 2009 10:35 am 
Beginner
Beginner

Joined: Wed Jun 17, 2009 9:03 pm
Posts: 31
Location: mumbai
If flushmode is auto (which is default) hibernate can synchronize persistance context objects with database before query execution. Try to use flushmode commit or manual.


Top
 Profile  
 
 Post subject: Re: Query cache is not correctly work
PostPosted: Tue Jul 14, 2009 2:04 pm 
Newbie

Joined: Fri Jan 09, 2009 4:31 am
Posts: 9
Yes, it's true, but what about dirty checking?


Top
 Profile  
 
 Post subject: Re: Query cache is not correctly work
PostPosted: Wed Jul 15, 2009 3:54 am 
Newbie

Joined: Sun Aug 24, 2008 3:21 pm
Posts: 8
Did you implement equals and hashcode correct? Please note the proxyproblem inside the equals() and please use only bean.getAttribute() instad of bean.attribute.


Top
 Profile  
 
 Post subject: Re: Query cache is not correctly work
PostPosted: Wed Jul 15, 2009 5:47 am 
Newbie

Joined: Fri Jan 09, 2009 4:31 am
Posts: 9
I don't need to implement equals and hashCode.

I call session.save(user) and hibernate MUST remember ID of user object.

And if user object don't change, persistent context don't flush!

F.e.:

User user = session.get(User.class, 3);

session.createQuery("FROM User u").setCacheable(true).list();
session.createQuery("FROM User u").setCacheable(true).list();

assertEquals(1, getSessionFactory().getStatistics().getQueryCacheHitCount());

All ok! But if user object is new - cache don't work. (


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.