javaEE hibernate中的Session关闭问题.

在pl/sql中查hibernate的session连接数

select count(*) from v$session

select count(*) from V$SESSION_WAIT

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

带openSession()必须手动关闭session,没带的hibernate可以自动关闭.

1.  ====

代码
String hql = " Select u From UserBean u left join fetch u.roles r left join fetch r.permits where u.userid = :userid";
        Session ss 
= getHibernateTemplate().getSessionFactory().openSession();
        UserBean us 
=(UserBean)ss.createQuery(hql).setParameter("userid",user.getUserid()).uniqueResult();
        
try{
            
return us ;
        }
        
finally{
            ss.close();
        }

2. ====

代码
public List queryForPage(final String hql, final int offset,
            
final int length) {
        List list 
= getHibernateTemplate().executeFind(new HibernateCallback() {
            
public Object doInHibernate(Session session)
                    
throws HibernateException, SQLException {
                Query query 
= session.createQuery(hql);
                query.setFirstResult(offset);
                query.setMaxResults(length);
                List list 
= query.list();
//                releaseSession(session);
                try{
                    
return list;
                }
                
finally{
                    session.close();
                }
            }
        });
        
return list;
    }

 

3.  ===hibernate会自动关闭的如:

PlacardBean pt=(PlacardBean)this.getHibernateTemplate().get(PlacardBean.class, typeId);

------------

String hql="FROM OqcReturnBean as O WHERE O.mblnr='"+value+"'";
  List list = getHibernateTemplate().find(hql);

-------------

 

posted @ 2010-03-06 08:52  wj-conquer  阅读(372)  评论(0编辑  收藏  举报