问题在休眠DAO(会话已经关闭!!) [英] problem in hibernate DAO (session already closed!!)

查看:162
本文介绍了问题在休眠DAO(会话已经关闭!!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了DAO,如下所示,

//伪代码

  public MyDAO {

private session session;
MYDAO {
this.setSession(HibernateUtil.getSessionFactory()。getCurrentSession());
}

public void save(MyObj obj){
// save obj in db
}
}


EG

 特征特征=新特征); 
feature.setFeatureName(testf333);

FeatureDAO featureDAO = new FeatureDAO();
Transaction tx = featureDAO.getSession()。beginTransaction();
featureDAO.makePersistent(feature);
tx.commit();
System.out.println(成功保存在数据库中+ feature.getId());


tx = featureDAO.getSession()。beginTransaction(); //在这里获取错误
Feature feature4 = new Feature();
feature4.setFeatureName(4444);
featureDAO.makePersistent(feature4);

tx.commit();
System.out.println(成功保存在数据库中+ feature.getId());

我认为这个问题可以通过检查session是否关闭来解决。但是我可以在哪里设置一个新的会话,因为我使用DAO的会话来启动一个transcation

解决方案

与其试图坚持 Session 在你的 MyDAO 中,你可能最好只保留 SessionFactory ,通过在 MyDAO 中定义您的 getSession 方法作为

  public Session getSession(){
return sessionFactory.getCurrentSession();

$ / code>

或者只保存与 MyDao中的会话处理相关的任何内容在所有和使用

  public Session getSession(){
return HibernateUtil.getSessionFactory ).getCurrentSession();

hibernate会话绑定到特定的线程,并在提交时关闭,但会话工厂的 getCurrentSession()方法根据需要获取新会话,以便您不必担心。


I have implemented DAO as follows,

//pseudoCode

public MyDAO{

  private Session session;
  MYDAO{
  this.setSession(HibernateUtil.getSessionFactory().getCurrentSession());
  }

  public void save(MyObj obj){
   //save obj in db
  }
}

Now i have used the dao to save a single object it works fine.Now if save two object inside seperate transcation i get a error saying "session is already closed"

EG

Feature feature = new Feature();
feature.setFeatureName("testf333");

FeatureDAO featureDAO = new FeatureDAO();
Transaction tx = featureDAO.getSession().beginTransaction();
featureDAO.makePersistent(feature);
tx.commit();
System.out.println("successfully save in db " + feature.getId());


tx = featureDAO.getSession().beginTransaction();  //getting error here
Feature feature4 = new Feature();
feature4.setFeatureName("4444");
featureDAO.makePersistent(feature4);

tx.commit();
System.out.println("successfully save in db " + feature.getId());

I think this problem can be solved by checking if session is closed .But where can i set a new session because i use the session from DAO to start a transcation

解决方案

Rather than trying to hold onto the Session in your MyDAO, you're probably best off holding onto just the SessionFactory, and getting a session from is when needed, by defining your getSession method in MyDAO as

public Session getSession() {
   return sessionFactory.getCurrentSession();
}

or just save nothing related to session handling in MyDao at all and use

public Session getSession() {
   return HibernateUtil.getSessionFactory().getCurrentSession();
}

A hibernate session is tied to a specific thread and closed on commit, but the session factory's getCurrentSession() method gets a new session as needed so that you don't have to worry about it.

这篇关于问题在休眠DAO(会话已经关闭!!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆