Recently I was writing a small hibernate program and noticed that the program was not terminating even though main method is executed successfully. I was using Hibernate latest version 4.3.5.Final.
最近,我正在编写一个小的Hibernate程序,并注意到即使成功执行了main方法,该程序也没有终止。 我正在使用Hibernate最新版本4.3.5.Final 。
Hibernate程序未终止程序 (Hibernate Program Not Terminating Program)
My sample class code is shown below.
我的示例类代码如下所示。
package com.journaldev.hibernate.main;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.journaldev.hibernate.model.Employee1;
import com.journaldev.hibernate.util.HibernateUtil;
public class HibernateMain {
public static void main(String[] args) {
Employee1 emp = new Employee1();
emp.setName("Lisa");
emp.setRole("Manager");
emp.setInsertTime(new Date());
//Get Session
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
//start transaction
session.beginTransaction();
//Save the Model object
session.save(emp);
//Commit transaction
session.getTransaction().commit();
System.out.println("Employee ID="+emp.getId());
}
}
The problem with above code is that Hibernate doesn’t release the resources and it’s our responsibility to release the resources once we are done with it.
上面代码的问题在于,Hibernate不会释放资源,一旦完成,这是我们的责任。
As you can notice that in above program, ServiceManager
instance is created but not closed.
如您所见,在以上程序中, ServiceManager
实例已创建但未关闭。
So I changed the program to below code and the program started terminating fine after the main method is executed.
因此,我将程序更改为以下代码,并且在执行main方法后程序开始终止。
package com.journaldev.hibernate.main;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.journaldev.hibernate.model.Employee1;
import com.journaldev.hibernate.util.HibernateUtil;
public class HibernateMain {
public static void main(String[] args) {
Employee1 emp = new Employee1();
emp.setName("Lisa");
emp.setRole("Manager");
emp.setInsertTime(new Date());
SessionFactory sessionFactory = null;
Session session = null;
Transaction transaction = null;
try{
//Get Session
sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.getCurrentSession();
//start transaction
transaction = session.beginTransaction();
//Save the Model object
session.save(emp);
//Commit transaction
transaction.commit();
System.out.println("Employee ID="+emp.getId());
}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();
}
}
}
}
Notice that I am closing resources in finally block, so that even if application throws any exception all the resources are released.
请注意,我正在finally块中关闭资源,因此,即使应用程序抛出任何异常,所有资源也会被释放。
翻译自: https://www.journaldev.com/2894/solved-hibernate-program-not-terminating