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

📄 hibernate_util.java

📁 采用最新Struts2+Hibernate架构开发
💻 JAVA
字号:
package www.hibernate.util.method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Hibernate_util {
	// hibernate configure file
	private static final String configFile = "hibernate.cfg.xml";

	// to load hibernate.properties
	private static  Configuration conf = new Configuration();

	// session container
	private static final ThreadLocal<Session> threadSession = new ThreadLocal<Session>();

	// transaction container
	private static final ThreadLocal<Transaction> threadTran = new ThreadLocal<Transaction>();

	// hibernate sessionFactory
	private static SessionFactory sessionFactory = null;

	// hibernate session
	private static Session session = null;

	// log log.properties
	private static final Log log = LogFactory.getLog(Hibernate_util.class);

	public Hibernate_util() {

	}

	static {
		// to load *.hbm.xml and hibernate.cfg.xml
		conf.configure(configFile);
		// create hibernate sessionFactory
		sessionFactory = conf.buildSessionFactory();
	}

	/**
	 * create session
	 * 
	 * @return session
	 */
	public  static synchronized Session openSession() {
		try {
			session = threadSession.get();
			if (session == null|| !session.isOpen()){
				session = sessionFactory.openSession();
			    threadSession.set(session);
			}
			log.info("create Session succeed");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("create Session lost");
		}
		return session;
	}

	/**
	 * begin transaction
	 * 
	 */
	public  static synchronized void beginTransaction() {
		try {
			session = openSession();
			Transaction tran = session.beginTransaction();
			threadTran.set(tran);
			log.info("create Transaction succeed");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("create Transaction lost");
		}
	}

	/**
	 * commit transaction
	 * 
	 */
	public static void commitTransaction() {
		try {
			Transaction tran = threadTran.get();
			if (tran != null && !tran.wasCommitted() && !tran.wasRolledBack())
				tran.commit();
			threadTran.set(null);
			log.info("commit Transaction succeed");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("commit Transaction lost");
		}
	}

	/**
	 * rollback transaction
	 * 
	 */
	public static void rollbackTransaction() {
		try {
			Transaction tran = threadTran.get();
			if (tran != null && !tran.wasCommitted() && !tran.wasRolledBack())
				tran.rollback();
			threadTran.set(null);
			log.info("rollback Transaction succeed");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("rollback Transaction lost");
		}
	}

	/**
	 * colose sesion resource
	 * 
	 */
	public static void closeSession() {
		try {
			session = threadSession.get();
			if (session != null || session.isOpen())
				session.close();
			threadSession.set(null);
			log.info("close Session succeed");
		} catch (Exception e) {
			log.error("close Session lost");
		}
	}
}

⌨️ 快捷键说明

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