解决Hibernate SQL Query Cache的一个可靠性问题_第1页
解决Hibernate SQL Query Cache的一个可靠性问题_第2页
解决Hibernate SQL Query Cache的一个可靠性问题_第3页
解决Hibernate SQL Query Cache的一个可靠性问题_第4页
解决Hibernate SQL Query Cache的一个可靠性问题_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、解决Hibernate SQL Query Cache的一个可靠性问题(附源码) 上篇 帖子 Hibernate查询缓存的一个可靠性问题 Java代码 1. private final Set querySpaces = new HashSet();    private final Set querySpaces = new HashSet();     通过源码分析,发现 querySpaces  主要用来进行Cache更新检查,querySpaces 

2、; 存放的是基本VOClass 对应的 tableName, 例如: SYS_PERM,SYS_USER . Hibernate 在执行查询时,会检查这个集合中的所有 tableName, 如果该任意一个 tableName 对应 VOClass 二级缓存 有增,删,改的更新操作,即 UpdateTimestampsCache 不是最新的 ,那么该 Query 的 cache 就失效,就会重新去数据库中查询 ID 集合。     SQLCustomQuery 在构造函数中即进行 sql 的解析和querySpaces的判断,其中中有这样一段代码: Java代码 1

3、.   public SQLCustomQuery(.) throws HibernateException    2.     SQLQueryReturnProcessor processor = new SQLQueryReturnProcessor(queryReturns, scalarQueryReturns, factory);   3.    proc

4、cess();   4.      .   5.     SQLLoadable entityPersisters = (SQLLoadable) processor.getPersisters().toArray( new SQLLoadable0 );   6.      .   7.

5、   for (int i = 0; i < entityPersisters.length; i+)    8.       SQLLoadable persister = entityPersistersi;   9.    /alias2Persister.put( aliasesi, 

6、persister );   10.   /TODO:Does not consider any other tables referenced in the query       11. ArrayHelper.addAll( querySpaces, persister.getQuerySpaces() );   12.  

7、0;  .   13.       14. .   public SQLCustomQuery(.) throws HibernateException SQLQueryReturnProcessor processor = new SQLQueryReturnProcessor(queryReturns, scalarQueryReturns, factory); cess(); . SQLLoadable entityPersisters = (SQLLo

8、adable) processor.getPersisters().toArray( new SQLLoadable0 ); . for (int i = 0; i < entityPersisters.length; i+) SQLLoadable persister = entityPersistersi; /alias2Persister.put( aliasesi, persister ); /TODO:Does not consider any other tables referenced in the query ArrayHelper.addAll( querySpace

9、s, persister.getQuerySpaces() ); . .       /TODO: Does not consider any other tables referenced in the query     ArrayHelper.addAll( querySpaces, persister.getQuerySpaces() );     其中红色部分是将 persister 的querySpaces 赋给 sqlQuery 的 querySpaces, 但是 per

10、sister 代表返回结果集类型对应的表,测试用例中是权限表 SYS_PERM; 因此漏了关联表 (Reference Table),这就是问题所在。 我们看到作者 author Gavin King, Max Andersen 也打了 TODO 注释: /TODO: Does not consider any other tables referenced in the queryJava代码 1. /*  2.  * analyze reference table of a specified sql

11、 query  3.  * author Raymond He, Guangzhou China  4.  * Oct 8, 2008  5.  *  6.  */  7. public class SQLQueryReferencesAnalyzer       8.   private stat

12、ic Pattern p = Ppile( "w3," );      9.   private static Map sqlquery2ReferenceTableMapping  = new HashMap();   10.   private static Map tableName2EntityNam

13、eMapping = new HashMap(100);   11.         12.   private static final Log log = LogFactory.getLog( SQLQueryReferencesAnalyzer.class );   13.        &#

14、160;14.   public List analyzeSQL(SessionFactory sessionFactory,String sql)             15.     if(sqlquery2ReferenceTableMapping.containsKey(sql)    16.    

15、0;            17.     List refTables  = (List)sqlquery2ReferenceTableMapping.get(sql);   18.     if(log.isDebugEnabled()   19.      log.debu

16、g("Got ref tables:" + refTables + "rn of SQL: " + sql);   20.      return refTables;   21.    else    22.       

17、60;     23.      if(tableName2EntityNameMapping.size() = 0)   /init it once   24.     initTableName2EntityNameMapping(sessionFactory);    25.      

18、60;   26.                 27.     List refTables = new ArrayList(3);   28.     Matcher m = p.matcher(sql);   29.  &

19、#160;             30.     while(m.find()                    31.      String word  =&#

20、160;m.group();   32.      word = word.toUpperCase();   33.     /check if the word is a table name in sessionFactory   34.         

21、 /cache table for every sessionFactory independently, for multi sessionFactory env.   35.      String key = "SF" + sessionFactory.hashCode() + "_" + w

22、ord;    36.      if(tableName2EntityNameMapping.containsKey( key )    37.        if(log.isDebugEnabled() log.debug("word is table: "+ word);   38.  &#

23、160;     refTables.add( word);   39.              40.         41.                42.  

24、0;   if(log.isDebugEnabled()   43.      log.debug("To cache sqlquery2ReferenceTableMapping, ref tables:" + refTables + "rn of SQL: " + sql);   44.  &#

25、160;    /cache it   45.       sqlquery2ReferenceTableMapping.put(sql, refTables);   46.      return refTables;   47.       48.    /* * analy

26、ze reference table of a specified sql query * author Raymond He, Guangzhou China * Oct 8, 2008 * */public class SQLQueryReferencesAnalyzer private static Pattern p = Ppile( "w3," ); private static Map sqlquery2ReferenceTableMapping = new HashMap(); private static Map tableName2EntityNameMa

27、pping = new HashMap(100); private static final Log log = LogFactory.getLog( SQLQueryReferencesAnalyzer.class ); public List analyzeSQL(SessionFactory sessionFactory,String sql) if(sqlquery2ReferenceTableMapping.containsKey(sql) List refTables = (List)sqlquery2ReferenceTableMapping.get(sql);if(log.is

28、DebugEnabled() log.debug("Got ref tables:" + refTables + "rn of SQL: " + sql); return refTables; else if(tableName2EntityNameMapping.size() = 0) /init it onceinitTableName2EntityNameMapping(sessionFactory); List refTables = new ArrayList(3); Matcher m = p.matcher(sql); while(m.fi

29、nd() String word = m.group(); word = word.toUpperCase();/check if the word is a table name in sessionFactory /cache table for every sessionFactory independently, for multi sessionFactory env. String key = "SF" + sessionFactory.hashCode() + "_" + word; if(tableName2EntityNameMappi

30、ng.containsKey( key ) if(log.isDebugEnabled() log.debug("word is table: "+ word); refTables.add( word); if(log.isDebugEnabled() log.debug("To cache sqlquery2ReferenceTableMapping, ref tables:" + refTables + "rn of SQL: " + sql); /cache it sqlquery2ReferenceTableMapping.

31、put(sql, refTables); return refTables;    2) 调用reference table分析类,将关联表加入 querySpaces     在 SQLCustomQuery 中 完成 Gavin King  的 TODO 任务。     Java代码 1. /2008-10-6,Raymond He fix bug here,old text: Does not consider 

32、;any other tables referenced in the query   2.    /*start: analyze ref tables and add it to querySpaces */  3.    SQLQueryReferencesAnalyzer referencesAnalyzer =

33、0;new SQLQueryReferencesAnalyzer();   4.    List refTables = referencesAnalyzer.analyzeSQL(factory, sql);   5.    for (int k = 0; k < refTables.size(); k+)    6.  

34、;   querySpaces.add(refTables.get(k);   7.       8.    /*end */  /2008-10-6,Raymond He fix bug here,old text: Does not consider any other tables referenced in the query /*start: analyze ref tables and add it to querySpac

35、es */ SQLQueryReferencesAnalyzer referencesAnalyzer = new SQLQueryReferencesAnalyzer(); List refTables = referencesAnalyzer.analyzeSQL(factory, sql); for (int k = 0; k < refTables.size(); k+) querySpaces.add(refTables.get(k); /*end */3. 验证     还是之前那个测试用例,观察日志: Execute No. 0 * 2008-

36、10-08 17:32:50,140 DEBUG(AbstractBatcher.java,346) - select  this.PERMCODE as  PERM1_15_0_,  this.MODULECODE as  MODULE2_15_0_,  this.PERMTYPECODE as  PERM3_15_0_,  this.PERMNAME as  PERM4_15_0_,  this.PERMDESC as  PERM5_15_0_,  this.PORTNO as&#

37、160; PORT6_15_0_ from (select  t.perm_code as permCode,        t.module_code as moduleCode,        t.perm_name as permName,        t.perm_desc as permDesc,        t.port_no as

38、 portNo,        t.perm_type_code as permTypeCode   from sys_perm t join sys_role_perm o     on t.perm_code = o.perm_code     where o.role_code = ? ) this  (No.0)result size:1 Execute No. 1 * (No.1)result size:1 2008-10-08 17:32:50

39、,187 DEBUG(AbstractBatcher.java,346) - delete from SYS_ROLE_PERM where PERM_CODE=? and ROLE_CODE=? Execute No. 2 * 2008-10-08 17:32:50,187 DEBUG(AbstractBatcher.java,346) - select  this.PERMCODE as  PERM1_15_0_,  this.MODULECODE as  MODULE2_15_0_,  this.PERMTYPECODE as  PERM

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论