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

📄 hibernateutil.java

📁 学校管理系统 校园管理系统 struts+hibernate
💻 JAVA
字号:
package com.stuman.dao.hibernate;

/*
 * HibernateUtil 用来处理全局的SessionFactory实例 
 */
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static final SessionFactory sessionFactory;

	static {
		try {
			// 创建 SessionFactory
			sessionFactory = new Configuration().configure()
			.buildSessionFactory();
      System.out.println("create SessionFactory ");
      } 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();

	/*
	 * 获得线性安全的session
	 */
	public static Session currentSession() {
		Session session = (Session) tLocalsess.get();

		// 打开一个 如果当前不可用
		try {
			if (session == null || !session.isOpen()) {
				session = openSession();
				tLocalsess.set(session);
                System.out.println("openSession");
			}
		} catch (HibernateException e) {
			// 抛出 HibernateException(e);
			e.printStackTrace();
		}
		return session;
	}

	/*
	 * 关闭线性安全 session
	 */
	public static void closeSession() {
        
		Session session = (Session) tLocalsess.get();
		tLocalsess.set(null);
		try {
			if (session != null && session.isOpen()) {
				session.close();
				System.out.println("close Session");
			}

		} catch (HibernateException e) {
                //抛出 HibernateException异常
		}
	}

	/*
	 * 开启事务
s	 */
	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) {
			// 抛出 HibernateException异常
		}
	}

	/*
	 * 关闭事物
	 */
	public static void commitTransaction() {
        System.out.println("start commit tx");
		Transaction tx = (Transaction) tLocaltx.get();
		try {
			if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
				tx.commit();
			tLocaltx.set(null);
			System.out.println("commit tx");
		} catch (HibernateException e) {
//			 抛出 HibernateException异常
		}
	}

	/*
	 * 事物回滚
	 */
	public static void rollbackTransaction() {
		Transaction tx = (Transaction) tLocaltx.get();
        System.out.println("rollback ");
		try {
			tLocaltx.set(null);
			if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
				tx.rollback();
			}
		} catch (HibernateException e) {
//			 抛出 HibernateException异常
		}
	}

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

	private static SessionFactory getSessionFactory() throws HibernateException {
		return sessionFactory;
	}
}

⌨️ 快捷键说明

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