hiberante 学习体会

下面是小弟学习Hibernate 的一些体会:

 

package com.wosdman.test;

 

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.

     * Location should be on the classpath as Hibernate uses 

     * #resourceAsStream style lookup for its configuration file.

     * The default classpath location of the hibernate config file is

     * in the default package. Use #setConfigFile() to update

     * the location of the configuration file for the current session.  

      */

     private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

       private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

    private  static Configuration configuration = new Configuration();

    private static org.hibernate.SessionFactory sessionFactory;

    private static String configFile = CONFIG_FILE_LOCATION;

 

       static {

           try {

                     configuration.configure(configFile);

                     sessionFactory = configuration.buildSessionFactory();

              } catch (Exception e) {

                     System.err

                                   .println("%%%% Error Creating SessionFactory %%%%");

                     e.printStackTrace();

              }

    }

    private HibernateSessionFactory() {

    }

      

       /**

     * Returns the ThreadLocal Session instance.  Lazy initialize

     * the <code>SessionFactory</code> if needed.

     *

     *  @return Session

     *  @throws HibernateException

     */

public static Session getSession() throws HibernateException {

 

首选对 ThreadLocal 作一个简单的说明 : 它的出现是为更好的服务的多线程 , 使基多线程的应用程序更加完美 . 如果有这样一种需要 : 每当我们开起一个新的线程 , 都需要访问一个变量 , 但这个变量可能会在其它的线程中被改变 , 这时 ThreadLocal 最有效来解这个问题 .

其实 ThreadLocal 并不是一个 Thread 而是一个 Thread local var , 我觉得应该访问的是本地的私有的静态的 (private static) 变量 , 但下面的这个应用不是的 ,session 并不是 private static

        Session session = (Session) threadLocal.get();

              当每一次调用时 ,session null

              if (session == null || !session.isOpen()) {

                     if (sessionFactory == null) {  // 似乎没有必要 , 因为在前面的 static 代码块中就对它 sessionfactory 初始化了

                            rebuildSessionFactory(); // 多重判断后来构建一个 sessionfactory, 其实是再次调用 configruation buildsessionfactory() method 完成的

                     }

                     session = (sessionFactory != null) ? sessionFactory.openSession()

                                   : null;    // 经过上面的判断以后 , 其实我觉得 sessionfactory 不可能为 null, 为了安全起见 , 与程序的逻辑性,这样写真有必要的 ,

来构建一个 session 对象

                     threadLocal.set(session);    // 把这个 session 对象装入 threadlocal 中,其实它是放入一个 Map 中,你可以查看 ThreadLocal 原代码。

              }

 

        return session;

    }

 

       /**

      *  Rebuild hibernate session factory

     *

     */

       public static void rebuildSessionFactory() {

              try {

                     configuration.configure(configFile);

                     sessionFactory = configuration.buildSessionFactory();

              } catch (Exception e) {

                     System.err

                                   .println("%%%% Error Creating SessionFactory %%%%");

                     e.printStackTrace();

              }

       }

 

       /**

     *  Close the single hibernate session instance.

     *

     *  @throws HibernateException

     */

    public static void closeSession() throws HibernateException {

        Session session = (Session) threadLocal.get();    // 因为 session 只是方法局部变量,在 threadlocal 中有 session ,所以只能通过 threadlocal.get() 方法来获得

        threadLocal.set(null);

 

        if (session != null) {

            session.close();

        }

    }

 

       /**

     *  return session factory

     *

     */

       public static org.hibernate.SessionFactory getSessionFactory() {

              return sessionFactory;

       }

 

       /**

     *  return session factory

     *

     *    session factory will be rebuilded in the next call

     */

       public static void setConfigFile(String configFile) {

              HibernateSessionFactory.configFile = configFile;

              sessionFactory = null;

       }

 

       /**

     *  return hibernate configuration

     *

     */

       public static Configuration getConfiguration() {

              return configuration;

       }

 

}

 

你可能感兴趣的:(thread,多线程,Hibernate,xml,Access)