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
          主站蜘蛛池模板: 通山县| 樟树市| 浦江县| 山丹县| 岑巩县| 霍州市| 乐都县| 永定县| 康马县| 吉林省| 淳安县| 息烽县| 江达县| 体育| 松桃| 肇州县| 赤峰市| 河间市| 大石桥市| 凉城县| 福贡县| 离岛区| 永胜县| 犍为县| 闸北区| 望都县| 贵定县| 丹阳市| 青神县| 土默特右旗| 荥阳市| 故城县| 天门市| 扶风县| 柳河县| 宝山区| 三原县| 岳普湖县| 江陵县| 高台县| 乌苏市|