第三十一天 慵懒的投射在JDBC上的暖阳 —Hibernate的使用(四)

             6月19日,小雨。“黄梅时节家家雨,青草池塘处处蛙。有约不来过夜半,闲敲棋子落灯花。”

        面向对象无限包容的个性,给对SQL和数据库一窍不通的澳大利亚人Gavin King创造了极大的想象空间。那些原本尴尬的不利因素---OO对象模型和关系型数据库之间的设计理念上的差异即-“O/R阻抗失衡(O/R Impedance Mismatch)”等。在澳大利亚人的转化手段中,都被自觉或不自觉地消除了。Hibernate的出现,让面向对象的对象模型和关系型数据库的数据结构之间的相互转换达到了一种高峰。好的coder对Hibernate的Session的理解往往会让人大吃一惊。或许可以说,澳大利亚人用Session表达了自己柔软变通的适应性。所有的这些,让每一个Session在open和close之间的短暂生命得到了升华。

            Session接口负责执行被持久化对象的CRUD操作(CRUD的任务是完成与数据库的交流,包含了很多常见的SQL语句。)。但需要注意的是Session对象是非线程安全的。

        1、自己动手,封装HibernateUtil类

     运用中为避免资源消耗,一般都会手动封装一个HibernateUtil类(未使用Spring管理的前提下)。该类的作用使Hibernate加载配置文件config, 创建sessionFactory等只运行一次。

package edu.eurasia.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * Configures and provides access to Hibernate sessions, tied to the current
 * thread of execution.
 */
public class HibernateUtil {

	/**
	 * Location of hibernate.cfg.xml file. NOTICE: Location should be on the
	 * classpath as Hibernate uses #resourceAsStream style lookup for its
	 * configuration file. That is place the config file in a Java package - the
	 * default location is the default Java package.<br>
	 * <br>
	 * Examples: <br>
	 * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
	 */
	private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

	/** Holds a single instance of Session */
	private static final ThreadLocal threadLocal = new ThreadLocal();

	/** The single instance of hibernate configuration */
	private static final Configuration cfg = new Configuration();

	/** The single instance of hibernate SessionFactory */
	private static org.hibernate.SessionFactory sessionFactory;

	/**
	 * Returns the ThreadLocal Session instance. Lazy initialize the
	 * <code>SessionFactory</code> if needed.
	 * 
	 * @return Session
	 * @throws HibernateException
	 */
	public static Session currentSession() throws HibernateException {
		Session session = (Session) threadLocal.get();

		if (session == null || !session.isOpen()) {
			if (sessionFactory == null) {
				try {
					cfg.configure(CONFIG_FILE_LOCATION);
					sessionFactory = cfg.buildSessionFactory();
				} catch (Exception e) {
					System.err.println("%%%% Error Creating SessionFactory %%%%");
					e.printStackTrace();
				}
			}
			session = (sessionFactory != null) ? sessionFactory.openSession()
					: null;
			threadLocal.set(session);
		}

		return session;
	}

	/**
	 * Close the single hibernate session instance.
	 * 
	 * @throws HibernateException
	 */
	public static void closeSession() throws HibernateException {
		Session session = (Session) threadLocal.get();
		threadLocal.set(null);

		if (session != null) {
			session.close();
		}
	}

	/**
	 * Default constructor.
	 */
	private HibernateUtil() {
	}
}

         2、 Hibernate使用事务     

       Hibernate对JDBC进行了轻量级的封装,它本身在设计时并不具备事务处理功能。Hibernate将底层的JDBCTransaction或JTATransaction进行了封装,再在外面套上Transaction和Session的外壳,其实是通过委托底层的JDBC或JTA来实现事务的处理功能的。      

package edu.eurasia.hibernate;

import java.util.Iterator;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class SimpleTransaction {
	public static void main(String[] args) {
		SimpleTransaction simpleTran = new SimpleTransaction();
		simpleTran.tran();
	}

	// 演示事务使用的方法
	public void tran() {
		// 得到当前事务
		Session session = HibernateUtil.currentSession();
		// 声明事务
		Transaction tx = null;
		try {
			// HQL查询语句
			String hql = "from UserInfo";
			// 事务的开始
			tx = session.beginTransaction();
			// 事务的中间操作
			Iterator it = session.createQuery(hql).list().iterator();
			while (it.hasNext()) {
				UserInfo userinfo = (UserInfo) it.next();
				System.out.println(userinfo.getUsername()+"  "+userinfo.getPassword());
			}
			// 提交事务
			tx.commit();
		} catch (Exception ex) {
			if (tx != null) {
				try {
					// 回滚事务
					tx.rollback();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} finally {
			// 关闭session
			session.close();
		}
	}
}

           3、MySQL数据库UserInfo类,映射文件UserInfo.hbm.xml和hibernate.cfg.xml配置文件见:

第十九天 慵懒的投射在JDBC上的暖阳 —Hibernate的使用(一) 

       4、还需要添加一个slf4j-nop-1.6.2.jar文件。工程结构如下:

     际运用中,经常需要将当前线程和session绑定.一般的用法为使用ThreadLocal: 在HibernateUtil类中封装hibernate的管理.通过openSession取得session,并将其放入ThreadLocal变量中. 这样业务逻辑中仅需通过工具类取得当前线程对应的session.使用完毕后,调用工具类closeSession方法将session关闭,当前线程的ThreadLocal变量置为NULL. 保证线程归还线程池复用后,ThreadLocal为空,以免出现导致其它线程访问到本线程变量。

       后,Hibernate的SessionFactory提供获取session的新方法getCurrentSession (获得与当前线程绑定的session). 内部通过代理封装,此方式得到的session不仅和当前线程绑定,也无需手动开关. 默认在事务提交之后,session自动关闭。
           了,引入Spring之后.sessionfactory的创建等都交给spring管理.Spring也提供了HibernateTemplate, HibernateDaoSupport这样的封装方法。用户可以不再考虑session的管理,事务的开启关闭.只需配置事务即可。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值