Today I am going to tell you other alternative you can do in order to avoid LazyInitializationException: Session has been closed. If you are using SPRING with HIBERNATE that you can take the advantages of IOC and AOP. These are some functions that you must have to know in order to understand the working of this concept.

session.connect() : It is a function that will be called on a Session object it is responsible for establishing the connection with the database.

session.disconnect() : It will release the connection resources. It doesn't mean you are closing the connection.

session.flush() : this function is used to serialize the data to the database. If you have something in the session that is not submitted to DB you can submit them using session.flush().

So here is the utility class that you can take :

HibernateUtil.java 

public class HibernateUtil {
    @Autowired
    private static SessionFactory sessionFactory;
    private static final ThreadLocal threadsession=new ThreadLocal();
    private static final ThreadLocal threadtransaction=new ThreadLocal();
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public void connect() {
        try {
            Session session=threadsession.get();
            if(session==null) {
                getSession();
                session=threadsession.get();
            }
            session.reconnect();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    public void diconnect() {
        Session session=threadsession.get();
        session.disconnect();
    }
    public Session getSession()    {
        Session session=threadsession.get();
        try {
            if(session==null) {
                session=sessionFactory.openSession();
                threadsession.set(session);
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return session;
    }
    public void closeSession() {
        try {
            Session session=threadsession.get();
            threadsession.set(null);
            if(session!=null && session.isOpen())
                session.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    public void beginTransaction() {
        Transaction tx=threadtransaction.get();
        try {
            if(tx==null) {
                tx=getSession().beginTransaction();
                threadtransaction.set(tx);
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    public void commitTransaction() {
        Transaction tx=threadtransaction.get();
        try {
            if(tx!=null && !tx.wasCommitted() && !tx.wasRolledBack())
                tx.commit();
            threadtransaction.set(null);
        }
        catch(Exception e) {
            rollbackTransaction();
            e.printStackTrace();
        }
    }
    public void rollbackTransaction() {
        Transaction tx=threadtransaction.get();
        try {
            threadtransaction.set(null);
            if(tx!=null && !tx.wasCommitted() && !tx.wasRolledBack()) {
                tx.rollback();
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            closeSession();
        }
    }
}

You can easily understand the HibernateUtil class we have use the Thread Local design pattern that you can see her.
I am taking one interceptor from where we can call the functions from out utility class and you can call it by using AOP.
here is the code that you can apply on your service layer using AOP.

ConnectionService.java

public class ConnectionService {

 @Autowired
 HibernateUtil2 hutil;
 public void startConnection() {
  hutil.connect();
  hutil.beginTransaction();
  System.out.println("before");
 }
 public void closeConnection() {
  System.out.println("after");
  hutil.commitTransaction();
  hutil.diconnect();
  hutil.getSession().flush();
 }
}


Now you have to configure this in spring configuration file and you have done.
LazyInitializationException: Session has been closed

1 comment:

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic