轉:getHibernateTemplate()更換getSession() hibernate3 默認支持延遲加載lazy SQL語句

轉至:http://quyulu.blog.163.com/blog/static/3191360520101114115837861/

 

在項目中遇到數據庫連接開了沒有關閉導致在客戶那邊部署後網站運行了沒多久就死了。連接不上數據庫了。。

這是一篇搜索到的文章。。手動關閉session、僅供參考

 

一.getHibernateTemplate()更換getSession(),手動關閉session

 

問題:操作CLOB字段時出錯

javax.servlet.ServletException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started

 

//***********新增********************// public boolean newDataObj(Object obj) { boolean ret=true; Session session=this.getHibernateTemplate().getSessionFactory().openSession(); try{ Transaction tx=session.beginTransaction(); session.save(obj); tx.commit(); } catch(Exception e){ e.printStackTrace(); logger.error(e); ret=false; } finally { if (session!= null && session.isOpen()) { session.close(); session = null; } } return ret; } //***********更新********************// public boolean updateDataObj(Object obj) { boolean ret=true; Session session=this.getHibernateTemplate().getSessionFactory().openSession(); try{ Transaction tx=session.beginTransaction(); session.update(obj); tx.commit(); } catch(Exception e) { e.printStackTrace(); logger.error(e); ret=false; } finally { if (session!= null && session.isOpen()) { session.close(); session = null; } } return ret; } //***********刪除********************// public boolean deleteDateObj(Object obj) { boolean ret=true; Session session=this.getHibernateTemplate().getSessionFactory().openSession(); try { Transaction tx=session.beginTransaction(); session.delete(obj); tx.commit(); } catch (Exception e) { e.printStackTrace(); logger.error(e); ret=false; } finally { if (session != null && session.isOpen()) { session.close(); session = null; } } return ret; } //***********刪除********************// public List<TDepartment> queryDep(String defaultDisplay) { List depList = null; Session session=this.getHibernateTemplate().getSessionFactory().openSession(); String hql = "from TDepartment tDepartment where tDepartment.isused='1' "; if(defaultDisplay!=null && !defaultDisplay.equals("")) hql += " and tDepartment.defaultDisplay='"+defaultDisplay+"' "; hql += " order by tDepartment.departmentId asc "; try { Query query = session.createQuery(hql); depList = query.list(); session.close(); } catch (HibernateException e) { e.printStackTrace(); } finally { if (session!= null && session.isOpen()) { session.close(); session = null; } } return depList; }

 

 

 

 

 

 

:

1.使用getSession()方法你只要繼承sessionFactory,而使用getHibernateTemplate()方法必須繼承

HibernateDaoSupport當然包括sessionFactory,這點區別都不是特別重要的,下面這些區別就很重要了

 


     2.getSession()

 

方法是沒有經過spring包裝的,spring會把最原始的session給你,在使用完之後必須自己調用相應close方法,

而且也不會對聲明式事務進行相應的管理,一旦沒有及時關閉連接,就會導致數據庫連接池的連接數溢出,

 

 

getHibernateTemplate()

方法是經過spring封裝的,例如添加相應的聲明式事務管理,由spring管理相應的連接。


   
在實際的使用過程中發現的確getHibernateTemplate()getSession()方法要好很多,但是有些方法在

getHibernateTemplate()並沒有提供,這時我們用HibernateCallback 回調的方法管理數據庫.

 

getSession() 獲得的是原始的sessionFactory,每次你必須自己維護session如結束後你必須關閉session。如果是hibernate中進行數據庫操作,你獲得是原始的hibernate styleexcepttion

 

hibernate templatespring包裝過的,它會幫你管理session,並且它會將hibernate exceptions轉換成其他的分類後的錯誤。這點getSession是肯定不行了。例如你用orclemysql返回的錯誤在getSession中就是不一樣的,而在hibernate template中就是一樣的。

 

實際使用中發現,對於基本的操作Hibernate template處理的的確比getSession要好,但到了複雜查詢的時候如分頁時需要調用getHibernateTemplate().execute(HibernateCallBack).要產生很多innerClass,調試非常不便。而getSession就相當簡單多了。

 

=======================================================

 

二. 延遲加載

問題:無法加載關聯項

錯誤頁面提示

could not initialize proxy - no Session

控制檯

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

 

 

        <many-to-one

        name="fkTDepartment"

        column="FK_T_DEPARTMENT"

        class="com.commonfile.hibernate.TDepartment"

        lazy="false"

 

:

hibernate中:hibernate3 默認支持延遲加載(lazy="proxy"我們可以把proxy看作是true),hibernate2 默認立即加載 (lazy="false")。

hibernate3中,所有的實體設置文件(user.hbm.xml)中的lazy屬性都被默認設成了true,就是當這個類沒有被調用時,延時加載,導致了以上情況的發生,在配置文件中將lzay屬性設爲false

 

 

===============================================================================

 

.SQL語句 查詢出錯

select new TUser(tUser.id,tUser.userName,tUser.department,tUser.fkTStation,tUser.unitFlag) from TUser  tUser where tUser.id != 'f' and tUser.isused=1 and tUser.id not in (00309,00324,00338,00313,00311,00337) order by tUser.department.codepath, tUser.fkTStation.sequence,tUser.unitFlag, nlssort(tUser.userName,'NLS_SORT=SCHINESE_PINYIN_M')

 

: hibernate3不支持沒有單引號”的條件查詢

 

 

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