2014-04-23 169 views
0

我在使用Hibernate的Tomcat上運行JSF應用程序; 我有一些DAO方法對數據庫進行操作,像這樣:休眠會話關閉已關閉

*

public boolean removeJprogram(Jobprogram jp) { 
     Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
     org.hibernate.Transaction tx = session.beginTransaction(); 
     try { 
      session.delete(jp); 
      tx.commit(); 
      System.out.println("Record deleted"); 
      return true; 
     } catch (Exception e) { 
      System.out.println(e.getMessage()); 
      tx.rollback(); 
      return false; 
     }finally{ 
      session.close(); 
     } 
    } 

我休眠文件讀入;但我錯誤會議已經關閉; 如果我不把session.close,有時給我錯誤會議已經選擇或somtinh像這樣。

+0

您正在使用什麼版本的Hibernate? – jgitter

+0

[Duplicate](http://stackoverflow.com/questions/6634344/problem-in-hibernate-dao-session-already-closed)? – hd1

+0

該版本是3.6.10 – antoiovi

回答

2

檢查hibernate配置文件中的自動提交,它可能被設置爲true。

+0

我試過了,但還是不行;我沒有關閉會話,但我有時候有錯誤:非法嘗試將代理與兩個打開的會話相關聯 – antoiovi

+0

用openSesion()替換(getCurrentSession()() –

+0

YES ..它可以工作,Tankyou ...我希望瞭解原因。它將auto.commit設置爲true或false; – antoiovi

0

在同一個班,我這個方法是正確的工作:

public List<Jobprogram> programsForUser(Date startDate, Date endDate, Users user) { 
    List programs = null; 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    org.hibernate.Transaction tx = session.beginTransaction(); 
    try { 
     Criteria criteria = session.createCriteria(Jobprogram.class); 
     if (startDate != null) { 
      criteria.add(Restrictions.ge("day", startDate)); 
     } 
     if (endDate != null) { 
      criteria.add(Restrictions.le("day", endDate)); 
     } 
     criteria.add(Restrictions.eq("users", user)); 
     criteria.addOrder(Order.asc("day")); 
     return criteria.list(); 
    } catch (Exception e) { 
     System.out.println(e.getMessage()); 
     tx.rollback(); 
     return null; 
    }finally{ 
     session.close(); 
    } 
} 

所以我試圖刪除從上面的代碼中tx.commit(),現在它工作正常;但我介意如果thi是正確的方式做這項工作.....我試圖讀取關於工作單位的文章,以解決問題,但它看起來像一切模糊..

+0

在這段代碼中,你不是在交易?是否因爲你沒有試圖堅持任何東西給db? –

+0

不,即時刪除記錄... – antoiovi

+0

我在說你使用Criteria API的代碼。 –

0

簡短介紹:
由Bassel使用openSession回答時,代碼在我提交轉錄時起作用;當我不要犯transiction會話定期封閉,普拉莫德說,原因是這麼想的..堅持(貼befor此代碼).. indipendently如果我使用的openSession或的getCurrentSession

Session session = HibernateUtil.getSessionFactory().openSession(); 
    org.hibernate.Transaction tx = session.beginTransaction(); 
    try { 
     session.save(jobprogram); 
     session.flush(); 
     session.clear(); 
     tx.commit(); 
     System.out.println("Record succesfully inserted...."); 
     return true; 
    } catch (Exception e) { 
     return false; 
    }finally{ 
     session.close(); 
    } 

,並盡我所能constate在其他的代碼,即doesen't堅持..:

session=HibernateUtil.getSessionFactory().getCurrentSession(); 
    try{ 
     org.hibernate.Transaction tx=session.beginTransaction(); 
     Query q = session.createQuery (
     "from Users where user_name='"+username+"' and user_pass='"+password+"'"); 
    user = (Users) q.list().get(0); 
     return user; 
    }catch(Exception e){ 
     return null; 
    }finally{ 
    session.close(); 
} 

下一步是understend的getCurrentSession和的openSession的區別...