⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hibernateutil.java

📁 这是本人曾经在公司里用的,内部开发框架,基于struts+hibernate今天分享给大家
💻 JAVA
字号:
/**
 * 
 */
package cn.bway.common.dao;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

import org.apache.log4j.Logger;

import cn.bway.common.BwayHibernateException;


/**
 * @author Kson
 *
 */
public class HibernateUtil {
	  private static SessionFactory sessionFactory = null;

	  public static final ThreadLocal session = new ThreadLocal();

	  private static Logger logger = Logger.getLogger(HibernateUtil.class);

	  public static final ThreadLocal transactionThread = new ThreadLocal();

	  public static Session getAloneSessionBySessionFactory() throws
	  BwayHibernateException, HibernateException {
	    if (sessionFactory == null)
	    {
	      logger.error("sessionFactory is null! Initial is error");
	      return null;
	    }
	    return sessionFactory.openSession();
	  }

	  public static Session currentSession() throws BwayHibernateException, HibernateException {
	    if (sessionFactory == null) {

	      if (getSessionFactory() == false) {
	        throw new BwayHibernateException(
	            "Exception geting SessionFactory from JNDI ");
	      }
	    }
	    Session s = (Session) session.get();
	    // Open a new Session, if this Thread has none yet
	    if (s == null) {
	      s = sessionFactory.openSession();
	      session.set(s);
	    }
	    return s;
	  }

	  public static Session currentSession(boolean isCommit) throws
	  BwayHibernateException, HibernateException {
	    if (sessionFactory == null) {

	      if (getSessionFactory() == false) {
	        throw new BwayHibernateException(
	            "Exception geting SessionFactory from JNDI ");
	      }
	    }
	    Session s = (Session) session.get();
	    if (s == null) {
	      s = sessionFactory.openSession();
	      session.set(s);
	    }
	    if (isCommit) {
	      currentTransaction();
	    }
	    return s;
	  }

	  public static void commitTransaction() throws BwayHibernateException, HibernateException {
	    Transaction tx = (Transaction) transactionThread.get();
	    //System.out.println("2=  " + ((Session) session.get()).connection() );
	    transactionThread.set(null);
	    if (tx != null) {
	      System.out.println("+++++++++++++++++++++++++++++=  " + ( (Session) session.get()).isOpen());
	      System.out.println("=================" + tx + "------------" +
	                         ( (Session) session.get()).isOpen());
	      tx.commit();
	    }
	  }

	  public static void rollbackTransaction() throws BwayHibernateException, HibernateException {
	    Transaction tx = (Transaction) transactionThread.get();
	    transactionThread.set(null);
	    if (tx != null) {
	      tx.rollback();
	    }
	  }

	  public static Transaction currentTransaction() throws BwayHibernateException, HibernateException {
	    Transaction tx = (Transaction) transactionThread.get();
	    if (tx == null) {
	      tx = currentSession().beginTransaction();
	      transactionThread.set(tx);
	    }
	    return tx;
	  }

	  public static void closeSession() {
	    try {
	      Session s = (Session) session.get();
	      session.set(null);
	      if (s != null) {
	        s.close();
	      }
	    }
	    catch (Exception e) {
	      e.printStackTrace();
	    }
	  }

	  private static boolean getSystemSessionFactory() {
	    try {

	      Context inttex = new InitialContext();
	      sessionFactory = (SessionFactory) inttex
	          .lookup("HibernateSessionFactory");
	    }
	    catch (NamingException e) {
	      return false;
	    }
	    return true;
	  }
	  
	  private static boolean getSessionFactory() throws BwayHibernateException, HibernateException {
	    sessionFactory = new Configuration().configure().buildSessionFactory();
	    return true;
	  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -