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

📄 hibernateutil.java

📁 Spring +Web 的完整 MyEclipse 项目源码,使用者可以作为入门材料可以在此基础上深入学习
💻 JAVA
字号:
/*
 * Created on 2005-1-9
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.bid.hibernate;

import net.sf.hibernate.cfg.*;
import net.sf.hibernate.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class HibernateUtil {
	private static final SessionFactory sessionFactory;

	static {
			try {
				// Create the SessionFactory
				sessionFactory = new Configuration().configure().buildSessionFactory();
			} catch (Throwable ex) {
				ex.printStackTrace();
				System.out.println("Initial SessionFactory creation failed.");
				throw new ExceptionInInitializerError(ex);
			}
		}
	
	public static final ThreadLocal tLocalsess = new ThreadLocal();

		public static final ThreadLocal tLocaltx = new ThreadLocal();

		/*
		 * getting the thread-safe session for using
		 */
		public static Session currentSession(){
			Session session = (Session) tLocalsess.get();

			//open a new one, if none can be found.
			try{
				if (session == null || !session.isOpen()){
					session = openSession();
					tLocalsess.set(session);
				}
			}catch (HibernateException e){
				//throw new HibernateException(e);
				e.printStackTrace();
			}
			return session;
		}

		/*
		 * closing the thread-safe session
		 */
		public static void closeSession(){

			Session session = (Session) tLocalsess.get();
			tLocalsess.set(null);
			try{
				if (session != null && session.isOpen()){
					session.close();
				}

			}catch (HibernateException e){
				//throw new InfrastructureException(e);
			}
		}

		/*
		 * begin the transaction
		 */
		public static void beginTransaction(){
			System.out.println("begin tx");
			Transaction tx = (Transaction) tLocaltx.get();
			try{
				if (tx == null){
					tx = currentSession().beginTransaction();
					tLocaltx.set(tx);
				}
			}catch (HibernateException e){
				//throw new InfrastructureException(e);
			}
		}

		/*
		 * close the transaction
		 */
		public static void commitTransaction(){
			Transaction tx = (Transaction) tLocaltx.get();
			try{
				if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
					tx.commit();
				tLocaltx.set(null);
			}catch (HibernateException e){
				//throw new InfrastructureException(e);
			}
		}

		/*
		 * for rollbacking
		 */
		public static void rollbackTransaction(){
			Transaction tx = (Transaction) tLocaltx.get();
			try{
				tLocaltx.set(null);
				if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){
					tx.rollback();
				}
			}catch (HibernateException e){
				//throw new InfrastructureException(e);
			}
		}

		private static Session openSession() throws HibernateException{
			return getSessionFactory().openSession();
		}

		private static SessionFactory getSessionFactory() throws HibernateException{
			return sessionFactory;//SingletonSessionFactory.getInstance();
		}
}

⌨️ 快捷键说明

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