




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 注冊會計師模擬試題+參考答案解析
- 教育工作會議
- 中醫學課件:溫里藥性與應用
- 《POP廣告》課件演示
- 紙制品行業安全生產法規與標準實施考核試卷
- 玻璃保溫容器生產質量控制與改進措施實施策略考核試卷
- 私募股權投資盡職調查實務考核試卷
- 紙張表面處理技術考核試卷
- 禮儀用品企業市場營銷策略調整與優化考核試卷
- 2025年耐磨球段項目建議書
- 2025年電工操作資格證考試復習考試題庫(共583題)(含答案)
- 2025年攝影師職業技能鑒定試卷:攝影現場拍攝光線與色彩協調技巧試題
- 臨床面試專業真題及答案
- 2025年公共事務管理師考試試卷及答案
- 醫藥職業道德課程課件
- 2025-2030中國鈹行業市場發展趨勢與前景展望戰略研究報告
- 2025屆河北省“五個一”名校聯盟高三下學期4月聯考物理試題(含答案)
- logo保密合同協議
- 網格員考試題及答案重慶
- 網絡安全知識手冊
- 消費者心理與行為附微課第2版白玉苓課后參考答案
評論
0/150
提交評論