What is the best practice for hibernate session and DAO

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

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 is actually a closed session that the DAO has to do every time, should close the session when the work is done.

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

      

}

+3


source to share


2 answers


You don't have to close the session in your method: you didn't open it.

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

    }
}

      

+1


source


Finally, you must also close the sessionfactory "sessionFactory.close ()" instance. If you don't face the problem below.



Fundamentals - Troubleshooting Hibernate / JDBC Connection pooling issues .

-2


source







All Articles