升级到Hibernate 4.3.4主要方法永远不会终止JVM不完成 [英] Upgrade To Hibernate 4.3.4 main method never terminates JVM not finish

查看:72
本文介绍了升级到Hibernate 4.3.4主要方法永远不会终止JVM不完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从 Hibernate 4.2.3 升级到 Hibernate 4.3.4 来测试一些 JPA 2.1 Spec。上一行:
$ b $ b

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

新增加的代码行

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

这个问题我有一个主要方法来测试一些 Criteria API 但是主要方法永远不会用最新的 Hibernate Version 来完成我已经检索正在运行的线程,最新版本的线程如下。

  ThreadList:
[threadID:] 2 [threadName:]引用Handler [isDaemon:] true [isAlive:] true
[threadID:] 3 [threadName:] Finalizer [isDaemon:] true [isAlive:] true
[threadName:] Signal Dispatcher [isDaemon:] true [isAlive:] true
[threadID:] 5 [threadName:]附加监听器[isDaemon:] true [isAlive:] true
[threadID:] 10 [threadName:清理线程[isDaemon:] true [isAlive:] true
[threadID:] 11 [threadName:] pool-1-thread-1 [isDaemon:] false [isAlive:] true
[threadID:] 1 [threadName:] main [isDaemon:] false [isAlive:] true

这个thr ead是新的。

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

根据规范不是守护进程。

 不,它不是。如果最后一个非守护线程完成,虚拟机将终止。它不一定是主线程。 

我认为这个线程至少会在5分钟内完成。



问题出现在创建HibernateSessionFactory中。

  public static void main (String [] args)
{
HibernateHandler handler = new HibernateHandler(true); //创建HibernateSessionFactory
return; // JVM不会在此完成
}

我希望我的JVM以主方法结束。



我做错了什么。

解决方案

看起来新的 ServiceRegister 关闭销毁 SessionFactory 是当你打电话时,你必须自己销毁。

  getSessionFactory.close()。 

我的新Hibernate代码

  final配置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工厂)
{
((StandardServiceRegistryImpl)serviceRegistry).destroy();
}});
final org.hibernate.SessionFactory factory = hibConfiguration.buildSessionFactory(serviceRegistry);

稍后您必须致电

  session.getSessionFactory()close()方法。 

我希望真的能帮助别人。


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..

解决方案

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.

这篇关于升级到Hibernate 4.3.4主要方法永远不会终止JVM不完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆