antoiovi
antoiovi

Reputation: 33

Hibernate session close already closed

I'm running a JSF application on Tomcat using Hibernate; I have some dao methods to perform operations on the database, like this :

*

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();
        }
    }

as I read in hibernate documentation; but i'have the error Session was already closed; if i dont't put the session.close, sometimes give me the error session already opned or somtinh like this.

Upvotes: 1

Views: 2982

Answers (3)

antoiovi
antoiovi

Reputation: 33

Shortly briefing :
As answered by Bassel using openSession the code works when i commit transictions; when i don't commit transiction the session is regularly closed as pramod said, cause it dosen't persist..(code posted befor this)..indipendently if i use openSession or 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();
    }

and as i could constate in an other piece of code , that doesen't persist..:

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();
 }

Next step is to understend the difference between getCurrentSession and openSession...

Upvotes: 0

antoiovi
antoiovi

Reputation: 33

In the same class i've this method that properly works :

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();
    }
}

So i've tried to remove the tx.commit() from the code above, and now it works fine; but i mind if thi is the right way to do the job.....I tried to read the article about unit of work, to undestand the issue, but it look like everything nebulous..

Upvotes: 0

Bassel Kh
Bassel Kh

Reputation: 1977

Check the auto commit in hibernate configuration file, it might be set as true.

Upvotes: 2

Related Questions