program using hibernate does not terminate

前端 未结 13 839
孤街浪徒
孤街浪徒 2020-12-25 10:14

I created a program using Hibernate.

The program reaches the main function end, nevertheless the program is running.

I wonder if it happens when Sessio

相关标签:
13条回答
  • 2020-12-25 10:58

    i had the same problem, the solution is so simple , you have to add this property to configuration's file

        <property name="hibernate.c3p0.timeout">0</property>
    
    0 讨论(0)
  • 2020-12-25 10:59

    Same problem in 4.3.4.Final.

    Now after adding following code, the problem is gone.

    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);
        }
    }
    

    .....

    0 讨论(0)
  • 2020-12-25 11:01

    I faced the same issue, the reason is we need to close the resources properly. try closing the resources in finally block

    try{
    			Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
    			sessionFactory = cfg.buildSessionFactory();;
    			session = sessionFactory.openSession();
    			transaction = session.beginTransaction();
    			session.save(person);
    			transaction.commit();
    			}catch(Exception e){
    				System.out.println("Exception occured. "+e.getMessage());
    			}finally{
    				if(session.isOpen()){
    					System.out.println("Closing session");
    					session.close();
    				}
    				if(!sessionFactory.isClosed()){
    					System.out.println("Closing SessionFactory");
    					sessionFactory.close();
    				}
    			}

    0 讨论(0)
  • 2020-12-25 11:04

    maybe, I solved this problem.

    I saw the thread dump after Util.getSessionFactory().close() called, a thread named "pool-2-thread-1" state was TIMED_WAITING (parking).

    The following snippets dump

    Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.0-b69 mixed mode):
    
    "DestroyJavaVM" #16 prio=5 os_prio=0 tid=0x00000000020b9000 nid=0x3684 waiting on condition [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
    "pool-2-thread-1" #15 prio=5 os_prio=0 tid=0x000000001bc27000 nid=0x3f0 waiting on condition [0x000000001ce6f000]
       java.lang.Thread.State: TIMED_WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x0000000080be30a0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
        at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
        at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
        at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
        at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:744)
    
    "derby.rawStoreDaemon" #14 daemon prio=5 os_prio=0 tid=0x000000001b059000 nid=0xa3c in Object.wait() [0x000000001ba1f000]
       java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000805f6190> (a org.apache.derby.impl.services.daemon.BasicDaemon)
        at org.apache.derby.impl.services.daemon.BasicDaemon.rest(Unknown Source)
        - locked <0x00000000805f6190> (a org.apache.derby.impl.services.daemon.BasicDaemon)
        at org.apache.derby.impl.services.daemon.BasicDaemon.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:744)
    
    "Timer-0" #13 daemon prio=5 os_prio=0 tid=0x000000001b08e800 nid=0x2160 in Object.wait() [0x000000001b6af000]
       java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x0000000080608118> (a java.util.TaskQueue)
        at java.lang.Object.wait(Object.java:502)
        at java.util.TimerThread.mainLoop(Timer.java:526)
        - locked <0x0000000080608118> (a java.util.TaskQueue)
        at java.util.TimerThread.run(Timer.java:505)
    

    I thought the cause is thread named "pool-2-thread-1" that created by buildSessionFactory method.

    As a result of comparing the two buildSessionFactory method, I noticed that ServiceRegistry resources has not released.

    Program successfully terminated by releasing it.

    The following code, I adding.

    Util.java

    configuration.setSessionFactoryObserver(
            new SessionFactoryObserver() {
                @Override
                public void sessionFactoryCreated(SessionFactory factory) {}
                @Override
                public void sessionFactoryClosed(SessionFactory factory) {
                    ((StandardServiceRegistryImpl) serviceRegistry).destroy();
                }
            }
    );
    

    thanks.

    0 讨论(0)
  • 2020-12-25 11:05

    It seems that Hibernate 4.3.1 introduced a bug. I create the connection in my application with:

    EntityManagerFactory connection = Persistence.createEntityManagerFactory(...)
    

    but even if the createEntityManagerFactory method fails with an exception, the service registry remains open. However, as you could see from the above code, I cannot terminate my application because as the method didn't succeed the variable connection wasn't assigned (it is null), so I cannot call connection.close() that would destroy the service registry. It seems that this is really a bug, because how will I be able to release resources without resorting to a hack, like using specific Hibernate APIs from a JPA application?

    0 讨论(0)
  • 2020-12-25 11:06

    I met this problem also today, and I found that the solution like:

    sessionFactory.close();
    

    will work if you have

    <property name="connection.pool_size">1</property>
    
    0 讨论(0)
提交回复
热议问题