hibernateconfig.java

来自「开源的OpenId的一个java实现」· Java 代码 · 共 80 行

JAVA
80
字号
/* * Copyright 2005,2006 WSO2, Inc. http://www.wso2.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.wso2.solutions.identity.persistence;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.wso2.solutions.identity.IdentityProviderException;/** * This loads the hibernate configuration and manages hibernate sessions. */public class HibernateConfig {    private static Log log = LogFactory.getLog(HibernateConfig.class);    private SessionFactory sessionFactory;    public final ThreadLocal SESSION = new ThreadLocal();    public HibernateConfig(String fileName) throws IdentityProviderException {        try {            sessionFactory = new Configuration().configure(fileName)                    .buildSessionFactory();        } catch (Throwable e) {            if (log.isDebugEnabled()) {                log.debug("Initial SessionFactory creation failed.", e);            }            throw new IdentityProviderException("sessionFactoryCreationFailed",                    e);        }    }    /**     * Accessor to obtain the session of the current thread.     *      * @return Returns an <code>org.hibernate.Session</code> instace which is     *         the hibernate session pertaining to this thread.     */    public Session getCurrentSession() {        Session session = (Session) SESSION.get();        if (session == null) {            session = sessionFactory.openSession();            SESSION.set(session);        }        return session;    }    /**     * Closes the hibernate session of the current thread.     */    public void closeSession() {        Session session = (Session) SESSION.get();        if (session != null) {            session.close();        }        SESSION.set(null);    }}

⌨️ 快捷键说明

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