posts - 0,  comments - 5,  trackbacks - 0

          ?

          ?1 import ?org.apache.commons.logging.Log;
          ?2 import ?org.apache.commons.logging.LogFactory;
          ?3 import ?org.hibernate.FlushMode;
          ?4 import ?org.hibernate.HibernateException;
          ?5 import ?org.hibernate.Session;
          ?6 import ?org.hibernate.SessionFactory;
          ?7 import ?org.hibernate.Transaction;
          ?8 import ?org.hibernate.cfg.Configuration;
          ?9
          10
          11
          12 public ? class ?SessionManager? {
          13 ???? private ? static ?Log?log? = ?LogFactory.getLog(SessionManager. class );
          14 ???? private ? static ? final ?SessionFactory?sessionFactory;
          15 ???? /** 靜態塊:創建session?factory */
          16 ???? static ? {
          17 ???????? try ? {
          18 ????????????System.out.println( " 數據映射初始化begin " );
          19 ????????????Configuration?configure? = ? new ?Configuration().configure();
          20 ????????????sessionFactory? = configure.buildSessionFactory();
          21 ???????????? /* URL?configFileURL?=
          22 ????????????????????SessionManager.class.getResource("/hibernate.cfg.xml");
          23 ????????????Configuration?configure?=?(new?Configuration()).configure(configFileURL); */

          24 ????????????System.out.println( " 數據映射初始化end " );
          25 ????????}
          ? catch ?(Throwable?ex)? {
          26 ????????????log.error( " Initial?SessionFactory?creation?failed. " ,?ex);
          27 ???????????? throw ? new ?ExceptionInInitializerError(ex);
          28 ????????}

          29 ????}

          30 ???? /** 獲取當前線程的session對象 */
          31 ???? public ? static ?Session?currentSession()? throws ?HibernateException? {
          32 ????????Session?s? = ?sessionFactory.openSession();
          33 ????????s.setFlushMode(FlushMode.NEVER);
          34 ???????? return ?s;
          35 ????}

          36 ???? public ? static ?Session?currentSessionFlush()? throws ?HibernateException? {
          37 ????????Session?s? = ?sessionFactory.openSession();
          38 ????????s.setFlushMode(FlushMode.AUTO);
          39 ???????? return ?s;
          40 ????}

          41 ???? /** 關閉當前線程所在的session對象 */
          42 ???? public ? static ? void ?closeSession(Session?s)? throws ?HibernateException? {
          43 ???????? if ?(s? != ? null ) {
          44 ????????????s.close();
          45 ????????}

          46 ????}

          47 ???? public ? static ?SessionFactory?getSessionFactory()? {
          48 ???????? if (sessionFactory != null ) {
          49 ???????????? return ?sessionFactory;
          50 ????????}

          51 ???????? else {
          52 ????????????System.out.println( " 無效的factiory " );
          53 ???????????? return ? null ;
          54 ????????}

          55 ????}

          56 }

          57
          posted on 2007-09-03 09:23 crazy 閱讀(394) 評論(0)  編輯  收藏 所屬分類: hibernate
          主站蜘蛛池模板: 水城县| 泉州市| 舒城县| 兰溪市| 伊宁县| 民丰县| 泰顺县| 五家渠市| 蒙山县| 平定县| 封丘县| 榆社县| 韩城市| 苏州市| 武穴市| 墨脱县| 佛坪县| 泰州市| 新龙县| 安化县| 乃东县| 桂林市| 五莲县| 南川市| 获嘉县| 台州市| 通州区| 祁连县| 大埔区| 蒲江县| 开鲁县| 明星| 桃江县| 房产| 始兴县| 静宁县| 即墨市| 安西县| 陵川县| 安义县| 岳阳县|