program using hibernate does not terminate

前端 未结 13 873
孤街浪徒
孤街浪徒 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 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();
    				}
    			}

提交回复
热议问题