Hibernate Mapping 加載慢,時常連接超時問題

遺留工程開發新功能 增加了幾張表 用eclipse hibernate 插件自動生成映射文件(*.hbm.xml)和Entity, 然後開發dao, 單元測試的時候發現時間有點久, 偶爾還有這樣的異常

Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:555)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:613)
... 49 more
Caused by: org.dom4j.DocumentException: Connection timed out Nested exception: Connection timed out
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:546)
... 50 more

剛開始誤以爲數據庫連接超時,但偶然中發現新生成的映射文件的頭與之前的映射文件的頭不一致
新映射文件頭是這樣的

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

已有映射文件頭

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

發現解析新映射文件需要好幾秒

Feb 27, 2016 1:58:04 PM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : config/bar/Award.hbm.xml
Feb 27, 2016 1:58:09 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: com.foo.activities.bar.entity.Award -> award

而將http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd改成http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd後, 立即解析完成

Feb 27, 2016 2:00:00 PM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : config/bar/Award.hbm.xml
Feb 27, 2016 2:00:00 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: com.foo.activities.bar.entity.Award -> award

爲什麼會有這樣大的差距呢,而在瀏覽器中實際訪問上述兩個url, 卻得到相反的認識, 訪問http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd很快, 而訪問http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd壓根訪問不了。

嘗試跟蹤了半天源碼也沒找到具體哪裏不同。可有什麼工具可以便捷的定位到底是哪一處代碼執行較久嗎? 現在使用JDK自帶的jvisualvm中的Sample, 定位的還是很粗粒度, 如

基本上就是異常信息中的類信息。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章