lhfei / hibernate-memcached

Automatically exported from code.google.com/p/hibernate-memcached
0 stars 0 forks source link

for different hql query the log output duplicate code, please help me #18

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
1. core lib is hibernate-memcached-1.2.2.jar,memcached-2.4.2.jar and 
hibernate-core-3.3.2.GA.jar

2. use hibernate query and setCacheable(true)
code:

String hql = " select menu from User user, Role role, Auth auth, Resource 
resource, Menu menu where user.id=:userId and user.roleId=role.id and 
role.id=auth.roleId and auth.resourceId=resource.id and resource.rtype=:rtype 
and resource.objectId=menu.id and menu.parentId=:parentId and 
menu.moduleId=:moduleId ";
        Map<String, Integer> filterMap = new HashMap<String, Integer>(2);
        filterMap.put("userId", userId);
        filterMap.put("moduleId", moduleId);
        filterMap.put("rtype", ResourceConstants.TYPE_MENU);
        filterMap.put("parentId", parentId);
        return super.find(hql, filterMap, true);

super.find(hql, filterMap, true) code is:

public <X> List<X> find(String hql, Map<String, ?> values, boolean useCache) {
    Query query = getSession().createQuery(queryString);
    query.setCacheable(useCache);
     if (values != null) {
       query.setProperties(values);
     }
     return query.list();
}

3. turn log4j level debug , i see this:
One:
DEBUG: com.googlecode.hibernate.memcached.HashCodeKeyStrategy - Transformed key 
[sql: select menu4_.id as id5_, menu4_.create_time as create2_5_, menu4_.is_dir 
as is3_5_, menu4_.last_modify_time as last4_5_, menu4_.module_id as module5_5_, 
menu4_.name as name5_, menu4_.parent_id as parent7_5_, menu4_.sort_index as 
sort8_5_, menu4_.url as url5_ from user user0_, role role1_, auth auth2_, 
resource resource3_, menu menu4_ where user0_.id=? and user0_.role_id=role1_.id 
and role1_.id=auth2_.role_id and auth2_.resource_id=resource3_.id and 
resource3_.rtype=? and resource3_.object_id=menu4_.id and menu4_.parent_id=? 
and menu4_.module_id=?; parameters: ; named parameters: {parentId=0, 
moduleId=2, userId=1, rtype=2}] to hashCode [2082154712]
DEBUG: com.googlecode.hibernate.memcached.HashCodeKeyStrategy - Final cache 
key: [org.hibernate.cache.StandardQueryCache:0:2082154712]
DEBUG: com.googlecode.hibernate.memcached.MemcachedCache - 
Memcache.get(org.hibernate.cache.StandardQueryCache:0:2082154712)
DEBUG: com.googlecode.hibernate.memcached.spymemcached.SpyMemcache - 
MemcachedClient.get(org.hibernate.cache.StandardQueryCache:0:2082154712)

Two:
DEBUG: com.googlecode.hibernate.memcached.HashCodeKeyStrategy - Transformed key 
[sql: select menu4_.id as id5_, menu4_.create_time as create2_5_, menu4_.is_dir 
as is3_5_, menu4_.last_modify_time as last4_5_, menu4_.module_id as module5_5_, 
menu4_.name as name5_, menu4_.parent_id as parent7_5_, menu4_.sort_index as 
sort8_5_, menu4_.url as url5_ from user user0_, role role1_, auth auth2_, 
resource resource3_, menu menu4_ where user0_.id=? and user0_.role_id=role1_.id 
and role1_.id=auth2_.role_id and auth2_.resource_id=resource3_.id and 
resource3_.rtype=? and resource3_.object_id=menu4_.id and menu4_.parent_id=? 
and menu4_.module_id=?; parameters: ; named parameters: {parentId=1, 
moduleId=1, userId=1, rtype=2}] to hashCode [2082154712]
DEBUG: com.googlecode.hibernate.memcached.HashCodeKeyStrategy - Final cache 
key: [org.hibernate.cache.StandardQueryCache:0:2082154712]
DEBUG: com.googlecode.hibernate.memcached.MemcachedCache - 
Memcache.get(org.hibernate.cache.StandardQueryCache:0:2082154712)

see the hashcode with two different hql out put in log, bug get same hashcode 
why? please help me. 

this problem make my application read wrong data from hibernate cache

Original issue reported on code.google.com by Tuzl...@gmail.com on 27 Jun 2010 at 4:52

GoogleCodeExporter commented 8 years ago
I think you need to set the cacheRegion.
setCacheRegionName(blah)

Original comment by raykrue...@gmail.com on 27 Jun 2010 at 4:59

GoogleCodeExporter commented 8 years ago
but i see log at application start:

INFO : com.googlecode.hibernate.memcached.MemcachedCacheProvider - Building 
cache for region [com.zwc.datasystem.entity.sys.Menu]
DEBUG: com.googlecode.hibernate.memcached.MemcachedCacheProvider - Using 
KeyStrategy: [com.googlecode.hibernate.memcached.HashCodeKeyStrategy]
INFO : com.googlecode.hibernate.memcached.MemcachedCacheProvider - Building 
cache for region [com.zwc.datasystem.entity.sys.Auth]
DEBUG: com.googlecode.hibernate.memcached.MemcachedCacheProvider - Using 
KeyStrategy: [com.googlecode.hibernate.memcached.HashCodeKeyStrategy]
INFO : com.googlecode.hibernate.memcached.MemcachedCacheProvider - Building 
cache for region [com.zwc.datasystem.entity.manage.StatBet]
DEBUG: com.googlecode.hibernate.memcached.MemcachedCacheProvider - Using 
KeyStrategy: [com.googlecode.hibernate.memcached.HashCodeKeyStrategy]
INFO : com.googlecode.hibernate.memcached.MemcachedCacheProvider - Building 
cache for region [com.zwc.datasystem.entity.manage.StatDetailReport]
DEBUG: com.googlecode.hibernate.memcached.MemcachedCacheProvider - Using 
KeyStrategy: [com.googlecode.hibernate.memcached.HashCodeKeyStrategy]
INFO : com.googlecode.hibernate.memcached.MemcachedCacheProvider - Building 
cache for region [com.zwc.datasystem.entity.sys.User]
DEBUG: com.googlecode.hibernate.memcached.MemcachedCacheProvider - Using 
KeyStrategy: [com.googlecode.hibernate.memcached.HashCodeKeyStrategy]
INFO : com.googlecode.hibernate.memcached.MemcachedCacheProvider - Building 
cache for region [com.zwc.datasystem.entity.sys.Resource]
DEBUG: com.googlecode.hibernate.memcached.MemcachedCacheProvider - Using 
KeyStrategy: [com.googlecode.hibernate.memcached.HashCodeKeyStrategy]
INFO : com.googlecode.hibernate.memcached.MemcachedCacheProvider - Building 
cache for region [com.zwc.datasystem.entity.sys.Module]
DEBUG: com.googlecode.hibernate.memcached.MemcachedCacheProvider - Using 
KeyStrategy: [com.googlecode.hibernate.memcached.HashCodeKeyStrategy]
INFO : com.googlecode.hibernate.memcached.MemcachedCacheProvider - Building 
cache for region [com.zwc.datasystem.entity.sys.Role]

like auto add the cacheRegion? 

Original comment by Tuzl...@gmail.com on 27 Jun 2010 at 5:07

GoogleCodeExporter commented 8 years ago
Those are the region names for your entities. Your queries need a region name 
as well.

Original comment by raykrue...@gmail.com on 27 Jun 2010 at 5:09

GoogleCodeExporter commented 8 years ago
when query cacheRegion is empty ,why not auto add the cacheRegion for query?:)

Original comment by Tuzl...@gmail.com on 27 Jun 2010 at 5:13

GoogleCodeExporter commented 8 years ago
Thanks raykrueger, but when i use hibernate query , i try use 
query.setCacheRegion(entityClass.getName());

but still have this problem , duplicate hashcode

Original comment by Tuzl...@gmail.com on 27 Jun 2010 at 5:34

GoogleCodeExporter commented 8 years ago
I see the hibernate-memcached-1.2.2.jar code:

public class HashCodeKeyStrategy extends AbstractKeyStrategy
{
  protected String transformKeyObject(Object key)
  {
    int hashCode = key.hashCode();
    this.log.debug("Transformed key [{}] to hashCode [{}]", key, Integer.valueOf(hashCode));
    return String.valueOf(hashCode);
  }
}

and i see the debug log:

DEBUG: com.googlecode.hibernate.memcached.HashCodeKeyStrategy - Transformed key 
[sql: select menu4_.id as id5_, menu4_.create_time as create2_5_, menu4_.is_dir 
as is3_5_, menu4_.last_modify_time as last4_5_, menu4_.module_id as module5_5_, 
menu4_.name as name5_, menu4_.parent_id as parent7_5_, menu4_.sort_index as 
sort8_5_, menu4_.url as url5_ from user user0_, role role1_, auth auth2_, 
resource resource3_, menu menu4_ where user0_.id=? and user0_.role_id=role1_.id 
and role1_.id=auth2_.role_id and auth2_.resource_id=resource3_.id and 
resource3_.rtype=? and resource3_.object_id=menu4_.id and menu4_.parent_id=? 
and menu4_.module_id=?; parameters: ; named parameters: {parentId=0, 
moduleId=2, userId=1, rtype=2}] to hashCode [2082154712]

i try:
this.log.debug("Transformed key [{}] to hashCode [{}]", key, 
Integer.valueOf(hashCode));

1.I copy log key for first param:

sql: select menu4_.id as id5_, menu4_.create_time as create2_5_, menu4_.is_dir 
as is3_5_, menu4_.last_modify_time as last4_5_, menu4_.module_id as module5_5_, 
menu4_.name as name5_, menu4_.parent_id as parent7_5_, menu4_.sort_index as 
sort8_5_, menu4_.url as url5_ from user user0_, role role1_, auth auth2_, 
resource resource3_, menu menu4_ where user0_.id=? and user0_.role_id=role1_.id 
and role1_.id=auth2_.role_id and auth2_.resource_id=resource3_.id and 
resource3_.rtype=? and resource3_.object_id=menu4_.id and menu4_.parent_id=? 
and menu4_.module_id=?; parameters: ; named parameters: {parentId=0, 
moduleId=2, userId=1, rtype=2}

2. i try 

System.out.println(String.valueOf(key.hashCode))  this output is: -177187100

not equals log output : 2082154712

Original comment by Tuzl...@gmail.com on 27 Jun 2010 at 5:44

GoogleCodeExporter commented 8 years ago
what's the (protected String transformKeyObject(Object key))  key type ? 

Original comment by Tuzl...@gmail.com on 27 Jun 2010 at 5:46

GoogleCodeExporter commented 8 years ago
I have to sleep , soo tired for this probleam, if any one have suggestion mail 
me:

tuhaitao@foxmail.com

thanks.

Original comment by Tuzl...@gmail.com on 27 Jun 2010 at 5:50

GoogleCodeExporter commented 8 years ago
Every query needs to have it's own individual cache region name. I can't just 
make one up, and you shouldn't use the entity name. Each hql you write should 
have it's own region name. 

Original comment by raykrue...@gmail.com on 27 Jun 2010 at 6:46

GoogleCodeExporter commented 8 years ago
In that case...
Closing this as it's not a bug. Bring up the discussion on the list if you'd 
like to talk about it further.

Original comment by raykrue...@gmail.com on 27 Jun 2010 at 7:16