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

📄 sessionfactory.java

📁 A Java web application, based on Struts and Hibernate, that serves as an online running log. Users m
💻 JAVA
字号:
/* @LICENSE_COPYRIGHT@ */
package net.sf.irunninglog.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

import net.sf.irunninglog.util.FatalRuntimeException;

/**
 * Factory class used for creating Hibernate sessions.  This class encapsulates
 * all of the logic needed to configure Hibernate and instantiate new session
 * instances using the static <code>openSession</code> method.
 *
 * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a>
 * @version $Revision: 1.2 $ $Date: 2005/06/25 15:18:20 $
 * @since iRunningLog 1.0
 */
public final class SessionFactory {

    /** <code>Log</code> instance for this class. */
    private static final Log LOG = LogFactory.getLog(SessionFactory.class);

    /** Factory for creating sessions. */
    private static final org.hibernate.SessionFactory FACTORY;

    static {
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Configuring Hibernate");
            }

            FACTORY = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            LOG.fatal("Error encountered while configuring Hibernate", ex);
            throw new FatalRuntimeException(ex);
        }
    }

    /** Utility class - does not expose a constructor. */
    private SessionFactory() { }

    /**
     * Create and open a new Hibernate session.  This will use the session
     * factory to provide a new session for use in Hibernate operations.
     *
     * @return The new Hibernate session
     */
    protected static Session openSession() {
        Session session = FACTORY.openSession();

        if (LOG.isDebugEnabled()) {
            LOG.debug("openSession: Created a new session " + session);
        }

        return session;
    }

}

⌨️ 快捷键说明

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