HibernateSession

本文介绍了一个用于管理Hibernate会话和事务的实用类。该类提供了一系列的方法来创建、获取、提交或回滚会话及事务,同时实现了线程局部变量以确保每个线程拥有独立的会话实例。
public class HibernateSession {

	private static final ThreadLocal sessionThread = new ThreadLocal();

	private static final ThreadLocal transactionThread = new ThreadLocal();

	private static SessionFactory sf = null;

	public static void setSessionFactory(SessionFactory _sf) {
		sf = _sf;
	}

	public static boolean isSessionCreated() {
		return (sessionThread.get() != null);
	}

	public static boolean isTransactionCreated() {
		return (transactionThread.get() != null);
	}
	/**
	 * Current Session
	 *
	 * @return Session
	 * @throws HibernateException
	 */
	public static Session currentSession() throws HibernateException {
		Session s = (Session) sessionThread.get();
		if (s == null) {
			if (sf == null)
				sf = new Configuration().configure().buildSessionFactory();
			s = sf.openSession();
			sessionThread.set(s);
		}
		return s;
		//		return sf.getCurrentSession();
	}

	/**
	 * Get a new Session
	 *
	 * @return Session
	 * @throws HibernateException
	 */
	public static Session newSession() throws HibernateException {
		if (sf == null)
			sf = new Configuration().configure().buildSessionFactory();
		return sf.openSession();
	}

	/**
	 * Start or Add current Session's Transaction
	 *
	 * @return Transaction
	 * @throws HibernateException
	 */
	public static Transaction currentTransaction() throws HibernateException {
		Transaction tx = (Transaction) transactionThread.get();
		if (tx == null) {
			tx = currentSession().beginTransaction();
			transactionThread.set(tx);
		}
		return tx;
	}

	/**
	 * Commit
	 *
	 * @throws HibernateException
	 */
	public static void commitTransaction() throws HibernateException {
		Transaction tx = (Transaction) transactionThread.get();
		transactionThread.set(null);
		if (tx != null)
			tx.commit();
	}

	/**
	 * Rollback
	 *
	 * @throws HibernateException
	 */
	public static void rollbackTransaction() throws HibernateException {
		Transaction tx = (Transaction) transactionThread.get();
		transactionThread.set(null);
		if (tx != null)
			tx.rollback();
	}

	/**
	 * Close Session
	 *
	 * @throws HibernateException
	 */
	public static void closeSession() throws HibernateException {
		Session s = (Session) sessionThread.get();
		sessionThread.set(null);
		if (s != null && s.isOpen()) {
			try {
				s.close();
			}
			catch (Exception e) {
				
			}
		}
		//		if (sf != null) {
		//			sf.getCurrentSession().close();
		//		}
	}

	public static void main(String[] args) {
		try {
			String conf = "hibernateTest.cfg.xml";
			if (args.length != 0)
				conf = args[0];
			Configuration cfg = new Configuration().configure("/" + conf);
			SchemaExport se = new SchemaExport(cfg);
			se.setOutputFile("create_table.sql");
			se.create(true, true);
		}
		catch (HibernateException e) {
			System.out.println(e.getMessage());
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值