- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
HibernateSessionFactory.java
资源名称:(J2EE)oa.rar [点击查看]
上传用户:lm2018
上传日期:2015-12-12
资源大小:30449k
文件大小:3k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.oa.module.system;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.cfg.Configuration;
- /**
- * Configures and provides access to Hibernate sessions, tied to the
- * current thread of execution. Follows the Thread Local Session
- * pattern, see {@link http://hibernate.org/42.html}.
- */
- public class HibernateSessionFactory {
- /**
- * Location of hibernate.cfg.xml file.
- * NOTICE: Location should be on the classpath as Hibernate uses
- * #resourceAsStream style lookup for its configuration file. That
- * is place the config file in a Java package - the default location
- * is the default Java package.<br><br>
- * Examples: <br>
- * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
- * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
- */
- private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
- /** Holds a single instance of Session */
- private static final ThreadLocal threadLocal = new ThreadLocal();
- /** The single instance of hibernate configuration */
- private static final Configuration cfg = new Configuration();
- /** The single instance of hibernate SessionFactory */
- private static org.hibernate.SessionFactory sessionFactory;
- /**
- * Returns the ThreadLocal Session instance. Lazy initialize
- * the <code>SessionFactory</code> if needed.
- *
- * @return Session
- * @throws HibernateException
- */
- public static Session currentSession() throws HibernateException {
- Session session = (Session) threadLocal.get();
- if (session == null || !session.isOpen()) {
- if (sessionFactory == null) {
- try {
- cfg.configure(CONFIG_FILE_LOCATION);
- sessionFactory = cfg.buildSessionFactory();
- } catch (Exception e) {
- System.err
- .println("%%%% Error Creating SessionFactory %%%%");
- e.printStackTrace();
- }
- }
- session = (sessionFactory != null) ? sessionFactory.openSession()
- : null;
- threadLocal.set(session);
- }
- return session;
- }
- /**
- * Close the single hibernate session instance.
- *
- * @throws HibernateException
- */
- public static void closeSession() throws HibernateException {
- Session session = (Session) threadLocal.get();
- threadLocal.set(null);
- if (session != null) {
- session.close();
- }
- }
- /**
- * Default constructor.
- */
- private HibernateSessionFactory() {
- }
- }