3

Actually I'm looking for the best practice for using session and DAO for hibernate 4.2.x

The first approach:

public class AccountDAOImpl implements AccountDAO {

    Session session=HibernateSessionFactoryUtil.getSession();
    Transaction transaction =null;
    @Override
    public void saveAccnt(Account account) {
        // TODO Auto-generated method stub
        try
        {
            transaction=session.beginTransaction();
            session.save(account);

            transaction.commit();

        }
        catch(RuntimeException re)
        {
            if(transaction!=null)
                transaction.rollback();
            throw re;
        }
        finally
        {
            session.close();
        }

    }
}

or we do the second approach:

public class AccountDAOImpl implements AccountDAO {

    Session session=HibernateSessionFactoryUtil.getSession();
    Transaction transaction =null;
    @Override
    public void saveAccnt(Account account) {
        // TODO Auto-generated method stub
        try
        {
            transaction=session.beginTransaction();
            session.save(account);

            transaction.commit();

        }
        catch(RuntimeException re)
        {
            if(transaction!=null)
                transaction.rollback();
            throw re;
        }


    }
}

The difference actually in close session which every time need to perform DAO need to close session when the work completed .

Updated :

public class HibernateSessionFactoryUtil {

private static SessionFactory sessionFactory=null;
public static Session getSession() {

    if(sessionFactory==null)
    {
        createSessionFactory();
    }


    return sessionFactory.openSession();
}


public static SessionFactory getSessionFactory() {
    if(sessionFactory==null)
    {
        createSessionFactory();
    }
    return sessionFactory;
}


private static void createSessionFactory() {
    Configuration configuration = new Configuration();
    configuration.configure("/hibernate.cfg.xml");

    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
     sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}

}

Saif
  • 6,804
  • 8
  • 40
  • 61
user2227726
  • 66
  • 1
  • 5

1 Answers1

2

You shouldn't close the session in your method: you didn't open it there.

Do either your second approach, or this:

public class AccountDAOImpl implements AccountDAO {

    @Override
    public void saveAccnt(Account account) {
        Transaction transaction =null;
        Session session=HibernateSessionFactoryUtil.getSession();
        try
        {
            transaction=session.beginTransaction();
            session.save(account);

            transaction.commit();

        }
        catch(RuntimeException re)
        {
            if(transaction!=null)
                transaction.rollback();
            throw re;
        }
        finally
        {
            session.close();
        }

    }
}
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97