How to use try-catch and resource argument to close a connection

I am developing a dynamic Struts2-Hibernate web application that makes a database connection. As with establishing a connection, most of the time I forgot to close the connection. This way I can achieve this with try-catch-resource . I also went through javaDoc , but that caused my confusion. This is what I got from JavaDoc, please correct me if I am wrong.

A class that has connection code must implement AutoCloseable . This gives one method public void close() throws Exception

. Now my question is what code to write in the close method and how to use the Connection class that implements AutoCloseable.

MyConnectionClass

public class StuHibernateUtils implements AutoCloseable {

    static SessionFactory sessionfactory;

    static
    {
        Configuration cfg=new Configuration().configure("/student.cfg.xml");
        ServiceRegistry registry=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
        sessionfactory=cfg.buildSessionFactory(registry);
    }

    public static SessionFactory getSessionFactory()
    {
        return sessionfactory;      
    }

    public static Session getSession()
    {
        return sessionfactory.openSession();

    }

    @Override
    public void close() throws Exception {
        // TODO Auto-generated method stub

    }
}

      

How to use a StuHibernateUtils instance to automatically close a connection.

+2


source to share


2 answers


I think you should be looking for the AutoCloseable session and not your helper class?

Including the fact that Hibernate doesn't seem to support AutoCloseable, but I'm assuming you have your code in the lines:

Session session = null;

try {
    session = sessionFactory.openSession();
    ...
} finally {
   if (session!=null) session.close()
}

      

Do you want to do the following to avoid closing the session?

try (Session session = sessionFactory.openSession()) {
...
}

      

In my project, I created a CloseableSession that implements AutoCloseable and provides access to the main session. Unfortunately, since AutoClosable and Session have close () methods, I could not implement both and use the normal delegation pattern.



public class CloseableSession implements AutoCloseable {

    private final Session session;

    public CloseableSession(Session session) {
        this.session = session;
    }

    public Session delegate() {
        return session;
    }

    @Override
    public void close() {
        session.close();
    }
}

      

This means that you can do the following and the session will automatically close.

try (CloseableSession session = new CloseableSession(
                sessionFactory.openSession())) {
...
}

      

Although this means that whenever you want to use a session, you now need to call session.delegate (). foo ().

As an aside, using static methods to enforce a session might seem like a temporary splash screen, but static methods tend to cause problems in the line for unit testing, etc., and make it difficult to share the current implementation with another implementation. I would recommend going to SessionFactory or your StuHibernateUtils class wherever required.

+10


source


This is fixed in hibernate version 5. If you can upgrade to version 5 use that. Jira's Supported Ticket



https://hibernate.atlassian.net/browse/HHH-8898

0


source







All Articles