简体   繁体   中英

`SessionFactory` and `Session` objects can't work with “try-with-resource”?

When using hibernate, I would like to see if SessionFactory and Session objects can work with "try-with-resource", so that I can't ignore invoking their close() methods:

try (SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession()){

    session.beginTransaction();

    Customer customer = new Customer();
    customer.setFirstName("James");
    customer.setLastName("Bond");
    customer.setSsn(999998);
    customer.setAddressLine1("1111 S St");
    customer.setCity("London");
    customer.setState("LDN");
    customer.setCountry("UK");

    session.save(customer);

    session.getTransaction().commit();

    // session.close();                                                                                                                                                        
    // sessionFactory.close();                                         

} catch (Exception e) {
    e.printStackTrace();
}

I get the errors however:

CustomerTest.java:12: error: incompatible types: try-with-resources not applicable to variable type
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
                       ^
    (SessionFactory cannot be converted to AutoCloseable)
CustomerTest.java:18: error: incompatible types: try-with-resources not applicable to variable type
        Session session = sessionFactory.openSession()){
                ^
    (Session cannot be converted to AutoCloseable)
2 errors

Does it mean that SessionFactory and Session objects can't work with "try-with-resource", because the two classes don't implement AutoCloseable interface?

Thanks.

Does it mean that SessionFactory and Session objects can't work with "try-with-resource", because the two classes don't implement AutoCloseable interface?

Yes, that's exactly what it means.

If you take a look at newer versions of Hibernate, however, you'll find that both SessionFactory and Session do implement the AutoCloseable interface there.

I think the change was made in Hibernate 5, so upgrading your version of Hibernate could be a potential solution.

This has been fixed in hibernate version 5. If you can upgrade to version 5, please use this. Supported Jira ticket

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

For project which cannot upgrade, for that we can implement our own CloseableSession interface.

public class CloseableSession implements AutoCloseable {

    private final Session session;

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

    public Session getSession() {
        return session;
    }

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

Usage

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM