[已解决]Hibernate程序未终止

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值