Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrade To Hibernate 4.3.4 main method never terminates JVM not finish

i have to upgrade from Hibernate 4.2.3 to Hibernate 4.3.4 to test some JPA 2.1 Spec. i have only change this line of code

Previous line:

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

new Added code line

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

the problem i have a main method just for test some Criteria API but the main method never finish with the latest Hibernate Version i have retrieve the running threads and and with the latest version the threads are as follow.

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

comparing with the previous version this thread is the new one.

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

is a not a daemon according to the spec.

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 finish at least in 5 minutes.

the problem arises only creating the 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 terminates with the main method finish..

what i am doing wrong..

like image 544
chiperortiz Avatar asked Mar 10 '14 14:03

chiperortiz


2 Answers

Seems that new ServiceRegister is not closed,Destroyed when SessionFactory is closed you have to destroy by 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 have to call

session.getSessionFactory().close();

i hope really helps somebody.

like image 79
chiperortiz Avatar answered Nov 07 '22 18:11

chiperortiz


To find out what the thread is doing, you can take a thread dump, for instance by running your program from eclipse in debug mode, and suspending the thread.

In your specific case though, you should try invoking sessionFactory.close().

like image 35
meriton Avatar answered Nov 07 '22 19:11

meriton