Close SessionFactory in Hibernate 4.3

后端 未结 3 1555
灰色年华
灰色年华 2020-12-31 22:20

I\'m upgrading my Hibernate to the latest version. With my old HibernateUtil.java I had no problems but when upgrading it, the SessionFactory doesn\'t seem to c

3条回答
  •  心在旅途
    2020-12-31 23:04

    Release the serviceRegistry when the main application terminates.

    public class Service {
    private SessionFactory factory;
    private ServiceRegistry serviceRegistry;
    
    public void initialize() throws Exception{
    
        Configuration configuration = new Configuration();
        configuration.configure("com/jeecourse/config/hibernate.cfg.xml");
    
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                configuration.getProperties()).build();
    
        factory = configuration.buildSessionFactory(serviceRegistry);
    
    }
    
    public void close() throws Exception{
        if(serviceRegistry!= null) {
            StandardServiceRegistryBuilder.destroy(serviceRegistry);
        }
    }
    

提交回复
热议问题