使用ThreadLocal实现事务的管理

在HibernateSessionFactory类中,我们可以仿照Session的管理方式来实现对Transaction事务的管理!

 

package com.hibernate;

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

 

public class HibernateSessionFactory {

 private static String CONFIG_FILE_LOCATION = "/com/hibernate/hibernate.cfg.xml";

 

 /**
  *
  * 程序在运行过程中会启动多个线程, threadLocal可以在单个线程内实现线程内部共享数据---使用单例模式
  *
  */
 private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

 /**
  * 使用ThreadLocal实现事务的管理
  *
  */
 private static final ThreadLocal<Transaction> txThreadLocal = new ThreadLocal<Transaction>();

//配置文件节点<hibernate-configuration>对应读取的对象

 private static Configuration configuration = new Configuration();

//配置文件<session-factory>节点对应读取的对象
 private static org.hibernate.SessionFactory sessionFactory;

//配置文件hibernate.cfg.xml配置文件所在的路径
 private static String configFile = CONFIG_FILE_LOCATION;

 

//静态块,会早于构造函数执行

 static {
  try {
   configuration.configure(configFile);
   sessionFactory = configuration.buildSessionFactory();
  } catch (Exception e) {
   System.err.println("%%%% Error Creating SessionFactory %%%%");
   e.printStackTrace();
  }
 }

 

 

 private HibernateSessionFactory() {
 }

 /**
  * Returns the ThreadLocal Session instance. Lazy initialize the
  * <code>SessionFactory</code> if needed.
  *
  * @return Session
  * @throws HibernateException
  */


 public static Session getSession() throws HibernateException {
  Session session = (Session) threadLocal.get();

  if (session == null || !session.isOpen()) {
   if (sessionFactory == null) {
    rebuildSessionFactory();
   }
   session = (sessionFactory != null) ? sessionFactory.openSession()
     : null;
   threadLocal.set(session);
  }

  return session;
 }

 public static void beginTransaction() throws HibernateException {
  //取得已经绑定的事务
  Transaction tx = (Transaction) txThreadLocal.get();
  
  //如果事务为空,或已经提交或已经回滚,则开启一个新的事务
  if (tx == null || tx.wasCommitted() || tx.wasRolledBack()) {
   tx = getSession().beginTransaction();
   //把事务绑定的本地线程中,供提交会回滚使用
   txThreadLocal.set(tx);
  }

 }

 

 public static void commitTransaction() throws HibernateException {
  //取得已经绑定的事务
  Transaction tx = (Transaction) txThreadLocal.get();
  //如果事务不等于空,并没有提交或回滚。则提交事务
  if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
   tx.commit();
  }
  //清空当前线程事务,确保下个操作取得新的事务
  txThreadLocal.set(null);

 }

 public static void rollbackTransaction() throws HibernateException {
  Transaction tx = (Transaction) txThreadLocal.get();

  if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
   tx.rollback();
  }

 


  txThreadLocal.set(null);

 }

 

 

 /**
  * Rebuild hibernate session factory
  *
  */
 public static void rebuildSessionFactory() {
  try {
   configuration.configure(configFile);
   sessionFactory = configuration.buildSessionFactory();
  } catch (Exception e) {
   System.err.println("%%%% Error Creating SessionFactory %%%%");
   e.printStackTrace();
  }
 }

 /**
  * 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();
  }
 }

 /**
  * return session factory
  *
  */
 public static org.hibernate.SessionFactory getSessionFactory() {
  return sessionFactory;
 }

 /**
  * return session factory
  *
  * session factory will be rebuilded in the next call
  */
 public static void setConfigFile(String configFile) {
  HibernateSessionFactory.configFile = configFile;
  sessionFactory = null;
 }

 /**
  * return hibernate configuration
  *
  */
 public static Configuration getConfiguration() {
  return configuration;
 }

}

你可能感兴趣的:(xml,Hibernate,配置管理)