Transition to Hibernate 4.3.4. The main method never ends. JVM does not end

I need to upgrade from Hibernate 4.2.3 to Hibernate 4.3.4 to test some JPA 2.1 Spec. I just changed this line of code

Previous line:

 final org.hibernate.service.ServiceRegistry serviceRegistry = new org.hibernate.service.ServiceRegistryBuilder().applySettings(hibConfiguration.getProperties()).buildServiceRegistry(); 

new Added line of code

 final org.hibernate.service.ServiceRegistry serviceRegistry = new org.hibernate.boot.registry.StandardServiceRegistryBuilder().applySettings(hibConfiguration.getProperties()).build(); 

The problem is, I have a main method for testing only the Criteria API , but the main method never ends with the latest Hibernate Version , I get the current streams, and with the latest version the streams follow below.

 ThreadList: [threadID:]2 [threadName:] Reference Handler [isDaemon:] true [isAlive:] true [threadID:]3 [threadName:] Finalizer [isDaemon:] true [isAlive:] true [threadID:]4 [threadName:] Signal Dispatcher [isDaemon:] true [isAlive:] true [threadID:]5 [threadName:] Attach Listener [isDaemon:] true [isAlive:] true [threadID:]10 [threadName:] Abandoned connection cleanup thread [isDaemon:] true [isAlive:] true [threadID:]11 [threadName:] pool-1-thread-1 [isDaemon:] false [isAlive:] true [threadID:]1 [threadName:] main [isDaemon:] false [isAlive:] true 

Compared to the previous version, this thread is new.

 [threadID:]11 [threadName:] pool-1-thread-1 [isDaemon:] false [isAlive:] true 

not demons by specification.

 No, it is not. The virtual machine terminates if the last non-daemon thread has finished. It doesn't have to be the main thread. 

I think this thread will not end in at least 5 minutes.

The problem only occurs when creating a HibernateSessionFactory.

 public static void main(String[] args) { HibernateHandler handler = new HibernateHandler(true);//creates the HibernateSessionFactory return;//JVM not finish at this point } 

I want my JVM to complete with the completion of the main method.

what am I doing wrong.

+6
source share
2 answers

It seems that the new ServiceRegister not closed , Destroyed , when the SessionFactory closed, you should destroy yourself when you call.

 getSessionFactory.close(). 

my new hibernate code

 final Configuration hibConfiguration = new Configuration().configure(yourCFGPath); final org.hibernate.service.ServiceRegistry serviceRegistry = new org.hibernate.boot.registry .StandardServiceRegistryBuilder(). applySettings(hibConfiguration.getProperties()).build(); hibConfiguration.setSessionFactoryObserver(new SessionFactoryObserver() { @Override public void sessionFactoryCreated(SessionFactory factory){} @Override public void sessionFactoryClosed(SessionFactory factory) { ((StandardServiceRegistryImpl)serviceRegistry).destroy(); }}); final org.hibernate.SessionFactory factory = hibConfiguration.buildSessionFactory(serviceRegistry); 

later you need to call

 session.getSessionFactory().close(); 

Hope that really helps someone.

+6
source

To find out what the stream does, you can take a dump of the stream, for example, by running your program from eclipse in debug mode and pausing the stream.

In your specific case, however, you should try calling sessionFactory.close() .

+2
source

All Articles