List of objects are not cached when Criteria/Query[HQL] class used.

193 views
Skip to first unread message

Developer Issues

unread,
Sep 12, 2011, 7:59:32 AM9/12/11
to hibernate-memcached
List of objects are not cached when Criteria/Query[HQL] class used in
my app.
I have a query which returns result of 13 objects[hibernate]/rows[DB]
from a table. Each object is cached individually but not the list.
As per hibernate spec the references to those 13 objects has to be
stored as a part of query cache which is not happening.

When fired the same query second time it is hitting the DB again.

e.g : Below debug log shows for 161(PK) like wise 12 more are there.

2011-09-12 16:54:52,511 INFO [STDOUT] Hibernate: select
stattype0_.STATTYPEID as STATTYPEID20_, stattype0_.DATARULE as
DATARULE20_, stattype0_.ORDERING as ORDERING20_, stattype0_.SERVICEID
as SERVICEID20_, stattype0_.DESCRIPTION as DESCRIPT5_20_,
stattype0_.STATTYPECODE as STATTYPE6_20_, stattype0_.CREATEDBY as
CREATEDBY20_, stattype0_.CREATEDDATE as CREATEDD8_20_,
stattype0_.MODIFIEDBY as MODIFIEDBY20_, stattype0_.MODIFIEDDATE as
MODIFIE10_20_, stattype0_.PERIODICITY as PERIODI11_20_,
stattype0_.custom_periodid as custom12_20_, stattype0_.STARTDATE as
STARTDATE20_, stattype0_.ENDDATE as ENDDATE20_,
stattype0_.stattypeunit as stattyp15_20_ from lobby.stattype
stattype0_ where stattype0_.SERVICEID=?
2011-09-12 16:54:52,511 DEBUG
[com.googlecode.hibernate.memcached.HashCodeKeyStrategy] Transformed
key [com.myapp.mayhem.plugin.lobby.StatType#161] to hashCode [161]
2011-09-12 16:54:52,511 DEBUG
[com.googlecode.hibernate.memcached.HashCodeKeyStrategy] Final cache
key: [com.myapp.mayhem.plugin.lobby.StatType:0:161]
2011-09-12 16:54:52,511 DEBUG
[com.googlecode.hibernate.memcached.MemcachedCache]
Memcache.get(com.myapp.mayhem.plugin.lobby.StatType:0:161)
2011-09-12 16:54:52,511 DEBUG
[com.googlecode.hibernate.memcached.spymemcached.SpyMemcache]
MemcachedClient.get(com.myapp.mayhem.plugin.lobby.StatType:0:161)
2011-09-12 16:54:52,526 DEBUG
[com.googlecode.hibernate.memcached.HashCodeKeyStrategy] Transformed
key [com.myapp.mayhem.plugin.lobby.StatType#161] to hashCode [161]
2011-09-12 16:54:52,526 DEBUG
[com.googlecode.hibernate.memcached.HashCodeKeyStrategy] Final cache
key: [com.myapp.mayhem.plugin.lobby.StatType:0:161]
2011-09-12 16:54:52,526 DEBUG
[com.googlecode.hibernate.memcached.MemcachedCache]
Memcache.set(com.myapp.mayhem.plugin.lobby.StatType:0:161)
2011-09-12 16:54:52,526 DEBUG
[com.googlecode.hibernate.memcached.spymemcached.SpyMemcache]
MemcachedClient.set(com.myapp.mayhem.plugin.lobby.StatType:0:161)

2011-09-12 16:55:04,120 INFO [STDOUT] Hibernate: select
stattype0_.STATTYPEID as STATTYPEID20_, stattype0_.DATARULE as
DATARULE20_, stattype0_.ORDERING as ORDERING20_, stattype0_.SERVICEID
as SERVICEID20_, stattype0_.DESCRIPTION as DESCRIPT5_20_,
stattype0_.STATTYPECODE as STATTYPE6_20_, stattype0_.CREATEDBY as
CREATEDBY20_, stattype0_.CREATEDDATE as CREATEDD8_20_,
stattype0_.MODIFIEDBY as MODIFIEDBY20_, stattype0_.MODIFIEDDATE as
MODIFIE10_20_, stattype0_.PERIODICITY as PERIODI11_20_,
stattype0_.custom_periodid as custom12_20_, stattype0_.STARTDATE as
STARTDATE20_, stattype0_.ENDDATE as ENDDATE20_,
stattype0_.stattypeunit as stattyp15_20_ from lobby.stattype
stattype0_ where stattype0_.SERVICEID=?
2011-09-12 16:55:04,120 DEBUG
[com.googlecode.hibernate.memcached.HashCodeKeyStrategy] Transformed
key [com.myapp.mayhem.plugin.lobby.StatType#161] to hashCode [161]
2011-09-12 16:55:04,120 DEBUG
[com.googlecode.hibernate.memcached.HashCodeKeyStrategy] Final cache
key: [com.myapp.mayhem.plugin.lobby.StatType:0:161]
2011-09-12 16:55:04,120 DEBUG
[com.googlecode.hibernate.memcached.MemcachedCache]
Memcache.get(com.myapp.mayhem.plugin.lobby.StatType:0:161)


Used versions
hibernate-memcached-1.2.2.jar,hibernate3.jar,memcached-2.3.1.jar

<cache usage="nonstrict-read-write" />

Query query = session.createQuery("from StatType as s where
s.serviceId=?");

query.setCacheable(true).setCacheRegion(StatType.class.getName()); //
tried with different region name e.g my.region.
query.setLong(0,new Long(serviceEngine.getId())); //gives service
id e.g :5
List list = query.list();

I have enabled query cache and second level cache in my app.

Thanks
Developer

RayKrueger

unread,
Sep 12, 2011, 10:39:42 AM9/12/11
to hibernate-memcached
> List of objects are not cached when Criteria/Query[HQL] class used in
> my app.
> I have a query which returns result of 13 objects[hibernate]/rows[DB]
> from a table. Each object is cached individually but not the list.
> As per hibernate spec the references to those 13 objects has to be
> stored as a part of query cache which is not happening.

Hibernate only caches IDs for the objects, it loads the objects after
their IDs are retrieved from the cache.

Developer Issues

unread,
Sep 13, 2011, 12:42:26 AM9/13/11
to hibernate-memcached
Thank you for the quick reply Ray.

My concern here is why there is a DB hit again[2011-09-12 16:55:04,120
INFO [STDOUT] - from above log] for every request, even if when query
cache is used.

As per this article "http://www.javalobby.org/java/forums/t48846.html"
when query cache is used it shoud work something like below. Please
correct me If I am wrong.

*----------------------------------------------------------------------------------------
*
| Query
Cache |
|----------------------------------------------------------------------------------------|
| ["from StatType as s where s.serviceId=?", [ 161 ] ] -> [ 161 ] ]
| .....like wise 12 more objects as per my requirement.
*----------------------------------------------------------------------------------------
*

When there is second level cache and query cache enabled in my app why
there is a DB hit.

Please also let me know, the current behavior of my app is the
functionality.So that I can confirm no more investigation will be
required from my end.

Note** : I am not expecting a list should be in cache, but how
hibernate/memcached handles when list of refernces has to be cached
and retrived without a DB hit/query being fired.

I hope you understand my concern :).

Thanks
Developer.

Ray Krueger

unread,
Sep 13, 2011, 8:54:59 AM9/13/11
to hibernate...@googlegroups.com
> When there is second level cache and query cache enabled in my app why
> there is a DB hit.

Are the models themselves in question marked as cacheable?

Developer Issues

unread,
Sep 13, 2011, 9:19:40 AM9/13/11
to hibernate...@googlegroups.com
 
Because of some confusion I made a mistake in below example. Please check the corrected one below.
 
serviceId[5] would be passed to query at runtime and expected results are : "Stattype model objects"  [i.e 13 objects/rows]
 
STATTYPEID is PK .e.g 161,162
 
*----------------------------------------------------------------------------------------
*
|                                    QueryCache                                         |
|----------------------------------------------------------------------------------------|
| ["from StatType as s where s.serviceId=?", [ 5 ] -> [ 161,162,163 .....N - in my case 13].
*----------------------------------------------------------------------------------------
 
Is this can be possible when query cache is enabled.
 
If I pass same serviceId muliple times and executed same code there shouldn't be DB hit (no selects should happen). If check above log it is happening.
 
Thanks for the reply. I have been trying to resolve this.
 
Please help me to resolve/understand this.
 
Thanks

Developer Issues

unread,
Sep 14, 2011, 8:21:29 AM9/14/11
to hibernate...@googlegroups.com
Hi Ray,
 
Tested using EHCache with attached POC. Both Second level cache and Query cache working seamless.
 
But the same is not working for Memcached.
 
To test, run test cases in CountryDAOTest class.
 
Details of POC:
1) SQL scripts are available for initial DB settings.
2) Testcases are available in CountryDAOTest class.
3) Configuration settings for EHCAche and Memcached are mentioned in hibernate.cfg.xml, can switch and test.
4) This is Maven project.

Test case contains the same piece of code executed for 5 times but only there is one DB hit.
 
It is just to confirm whether Memcached support this.
 
Thanks

 

Developer Issues

unread,
Sep 14, 2011, 8:21:59 AM9/14/11
to hibernate...@googlegroups.com
Please find the attached
 
Thanks
 
hibernateCaching.rar

Ray Krueger

unread,
Sep 14, 2011, 8:31:10 AM9/14/11
to hibernate...@googlegroups.com
I'll gladly accept a patch for whatever you think the issue might be. Fork the project on github, clone it, change it, push it, and send a pull request.

I don't actively work on the project much. It is open source and I encourage others to help out. He best way to help is to fix stuff :)
--
You received this message because you are subscribed to the Google Groups "hibernate-memcached" group.
To post to this group, send email to hibernate...@googlegroups.com.
To unsubscribe from this group, send email to hibernate-memca...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hibernate-memcached?hl=en.

Ray Krueger

unread,
Sep 14, 2011, 8:56:46 AM9/14/11
to hibernate...@googlegroups.com
Also, would you mind doing some testing with the current code in master?
The 1.3-SNAPSHOT code has some improvements around key strategy; mostly because HashCodeKeyStrategy is crappy.

I'm curious if cache misses are causing the behavior you're seeing.


On Wednesday, September 14, 2011 at 7:31 AM, Ray Krueger wrote:

> I'll gladly accept a patch for whatever you think the issue might be. Fork the project on github, clone it, change it, push it, and send a pull request.
>
> I don't actively work on the project much. It is open source and I encourage others to help out. He best way to help is to fix stuff :)
>

> On Sep 14, 2011, at 7:21, Developer Issues <devthough...@gmail.com (mailto:devthough...@gmail.com)> wrote:
>
> > Hi Ray,
> >
> > Tested using EHCache with attached POC. Both Second level cache and Query cache working seamless.
> >
> > But the same is not working for Memcached.
> >
> > To test, run test cases in CountryDAOTest class.
> >
> > Details of POC:
> > 1) SQL scripts are available for initial DB settings.
> > 2) Testcases are available in CountryDAOTest class.
> > 3) Configuration settings for EHCAche and Memcached are mentioned in hibernate.cfg.xml, can switch and test.
> > 4) This is Maven project.
> >
> > Test case contains the same piece of code executed for 5 times but only there is one DB hit.
> >
> > It is just to confirm whether Memcached support this.
> >
> > Thanks
> >
> > --
> > You received this message because you are subscribed to the Google Groups "hibernate-memcached" group.

> > To post to this group, send email to hibernate...@googlegroups.com (mailto:hibernate...@googlegroups.com).
> > To unsubscribe from this group, send email to hibernate-memca...@googlegroups.com (mailto:hibernate-memca...@googlegroups.com).

Developer Issues

unread,
Sep 15, 2011, 1:47:14 AM9/15/11
to hibernate...@googlegroups.com
Hey Ray,
 
Thanks for the reply.
I made a small mistake in my configuration settings. Now it is working fine. 
The attached POC uses "1.3-SNAPSHOT", even tested on that, working fine.
 
Thanks for your time.
 
 

Ray Krueger

unread,
Sep 15, 2011, 8:55:05 AM9/15/11
to hibernate...@googlegroups.com
Fantastic! What was the issue?

On Sep 15, 2011, at 0:47, Developer Issues <devthoughts.issues
--
You received this message because you are subscribed to the Google Groups "hibernate-memcached" group.
To post to this group, send email to hibernate...@googlegroups.com.
To unsubscribe from this group, send email to hibernate-memca...@googlegroups.com.

kyaw khaing

unread,
Oct 2, 2013, 6:02:22 AM10/2/13
to hibernate...@googlegroups.com
Hello,
       What is your mistake. I also face this problem. Query cache is not working.
Thanks.


On Thursday, September 15, 2011 8:55:05 PM UTC+8, RayKrueger wrote:
Fantastic! What was the issue?

On Sep 15, 2011, at 0:47, Developer Issues <devthoughts.issues
Hey Ray,
 
Thanks for the reply.
I made a small mistake in my configuration settings. Now it is working fine. 
The attached POC uses "1.3-SNAPSHOT", even tested on that, working fine.
 
Thanks for your time.
 
 

--
You received this message because you are subscribed to the Google Groups "hibernate-memcached" group.
To post to this group, send email to hibernate...@googlegroups.com.
To unsubscribe from this group, send email to hibernate-memcached+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages