database.java

来自「开源项目CRM之OpenCustomer」· Java 代码 · 共 133 行

JAVA
133
字号
/*******************************************************************************
 * ***** BEGIN LICENSE BLOCK Version: MPL 1.1
 * 
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (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.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * 
 * The Original Code is the OpenCustomer CRM.
 * 
 * The Initial Developer of the Original Code is Thomas Bader (Bader & Jene
 * Software-Ingenieurb黵o). Portions created by the Initial Developer are
 * Copyright (C) 2005 the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s): Thomas Bader <thomas.bader@bader-jene.de>
 * 
 * ***** END LICENSE BLOCK *****
 */

package org.opencustomer.db;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class Database
{

    /**
     * Holds the current hibernate session, if one has been created.
     */
    protected static ThreadLocal<Session> hibernateHolder = new ThreadLocal<Session>();

    protected static SessionFactory factory;

    /**
     * This method should only be called when this class is used directly - that
     * is, when using this class outside of the servlet container.
     * 
     * @throws HibernateException
     */
    public static void doInit() throws HibernateException
    {
        if (factory == null)
            factory = new AnnotationConfiguration().configure().buildSessionFactory();
    }

    /**
     * ONLY ever call this method from within the context of a servlet request
     * (specifically, one that has been associated with this filter). If you
     * want a Hibernate session at some other time, call getSessionFactory( )
     * and open/close the session yourself.
     * 
     * @return an appropriate Session object
     */
    public static Session getSession() throws HibernateException
    {
        Session sess = hibernateHolder.get();

        if (sess == null)
        {
            sess = factory.openSession();
            hibernateHolder.set(sess);
        }
        return sess;
    }

    /**
     * Close the session for this thread
     * 
     * @throws HibernateException
     */
    public static void closeSession() throws HibernateException
    {
        Session sess = hibernateHolder.get();

        if (sess != null)
        {
            sess.close();
            hibernateHolder.set(null);
        }
    }

    /**
     * @return the hibernate session factory
     */
    public static SessionFactory getSessionFactory()
    {
        return factory;
    }

    /**
     * Close the SessionFactory. Only call when closing the application
     * 
     */
    public static void closeFactory()
    {
        if (factory != null)
        {
            closeSession();
            factory.close();
        }
    }

    /**
     * This is a simple method to reduce the amount of code that needs to be
     * written every time hibernate is used.
     * 
     * @param The Transaction to rollback
     */
    public static void rollback(Transaction tx)
    {
        if (tx != null)
        {
            try
            {
                tx.rollback();
            }
            catch (HibernateException ex)
            {
                // Probably don't need to do anything - this is likely being
                // called because of another exception, and we don't want to
                // mask it with yet another exception.
            }
        }
    }
}

⌨️ 快捷键说明

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