簡(jiǎn)單的說(shuō),我用hibernate語(yǔ)言進(jìn)行查詢(xún),初始化了一個(gè)session,函數(shù)需要return一個(gè)Iterator類(lèi)型,但是我要關(guān)閉此session,我應(yīng)該把這個(gè)close session語(yǔ)句放在哪里?
詳細(xì)點(diǎn)說(shuō),我的與數(shù)據(jù)庫(kù)連接進(jìn)行查詢(xún)的函數(shù)如下:
public class OperatorBean extends AbsQueryMap
{
public OperatorBean()throws HibernateException
{
this.initSession();
}
//查詢(xún)所有操作員的基本信息
public Iterator getAllOperatorBase()throws HibernateException
{
String queryString = "select operatorInfos from OperatorInfo as operatorInfos";
initSession();
Query query = this.session.createQuery(queryString);
Iterator it= query.iterate();
return it;
}
}
其中,AbsQueryBase的作用是初始化session,具體代碼如下:
public class AbsQueryMap {
/**
* 打開(kāi)當(dāng)前的數(shù)據(jù)庫(kù)連接
* @return
* @throws HibernateException
*/
public void initSession() throws HibernateException {
this.session = DBUtil.currentSession();
}
/**
* 關(guān)閉當(dāng)前的數(shù)據(jù)庫(kù)連接
* @throws HibernateException
*/
public void closeSession() throws HibernateException {
DBUtil.closeSession();
}
}
我在Action中調(diào)用OperatorBean,如下
OperatorBean business=new OperatorBean();
Iterator it=business.getAllOperatorBase();
難道我要在Action中關(guān)閉session嗎?
急救啊,小米!