ssh框架关于hibernate中session关闭的问题
package dao.impl;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.opensymphony.xwork2.ActionContext;
import dao.ManagerDAO;
import entity.Dh0103Staff;
public class ManagerDAOImpl extends HibernateDaoSupport implements ManagerDAO{
public List<Dh0103Staff> findAll(int pagesize,int pagecur,String StaffNumber){
if(StaffNumber==null){
String hql = "from Dh0103Staff";
Query query = getSession().createQuery(hql);
query.setFirstResult((pagecur-1)*pagesize);
query.setMaxResults(pagesize);
List<Dh0103Staff> list = query.list();
getSession().close();
return list;
}
else{
String hql = "from Dh0103Staff d where d.staffNumber like ?";
Query query = getSession().createQuery(hql);
query.setString(0, "%"+StaffNumber+"%");
query.setFirstResult((pagecur-1)*pagesize);
query.setMaxResults(pagesize);
List<Dh0103Staff> list = query.list();
getSession().close();
return list;
}
}
public int count(String staffNumber){
if(staffNumber==null){
String hql = "select count(d.staffId) from Dh0103Staff d where d.staffStae=?";
Query query = getSession().createQuery(hql);
query.setInteger(0,0);
long l=(Long) query.uniqueResult();
int count = (int)l;
ActionContext.getContext().getSession().put("count", count);
getSession().close();
return count;
}else{
String hql = "select count(d.staffId) from Dh0103Staff d where d.staffNumber like ? and d.staffStae=?";
Query query = getSession().createQuery(hql);
query.setString(0,"%"+staffNumber+"%");
query.setInteger(1,0);
long l=(Long) query.uniqueResult();
int count = (int)l;
ActionContext.getContext().getSession().put("count", count);
getSession().close();
return count;
}
}
public int account(){
String hql="select d.staffNumber from Dh0103Staff d";
Query query = getSession().createQuery(hql);
Integer account = 0;
Integer temp= 0;
List<String> list = query.list();
Iterator<String> it = list.iterator();
while(it.hasNext()){
account = Integer.parseInt(it.next());
if(account>temp){
temp = account;
}
}
Integer account2 = temp+1;
getSession().close();
return account2;
}
public boolean save(Dh0103Staff dh0103Staff){
boolean bool = false;
try {
Session session = super.getSessionFactory().openSession();
Transaction tran = session.beginTransaction();
session.save(dh0103Staff);
tran.commit();
bool = true;
session.close();
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
public Dh0103Staff findById(int staffId){
Dh0103Staff instance = (Dh0103Staff) getHibernateTemplate().get(
"entity.Dh0103Staff", staffId);
return instance;
}
public Dh0103Staff merge(Dh0103Staff detachedInstance){
Dh0103Staff result = (Dh0103Staff) getHibernateTemplate().merge(detachedInstance);
return result;
}
}
如上代码,我是在action类调用上面的方法的,然后返回到jsp页面显示数据库中的表。在一开始加载页面的时候,是没问题的,findAll()方法也查询出了数据库中的表,jsp页面显示无误。但当我刷新jsp页面的时候这张表就加载不出来了,问了别人,别人说是session没关闭,但是我有getSession.close()这条语句。为什么没关闭?,怎么关闭?求哪位好心的大神指点一番!
SSH 框架 Hibernate session
[解决办法]
使用spring管理,直接使用
getHibernateTemplate()就可以了