<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 167,  comments - 30,  trackbacks - 0

    package com.cns.certservice.dao.impl;

    import org.apache.log4j.Logger;
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;

    import com.cns.certservice.exception.DAOException;


    public class HibernateTemplate {

        private HibernateTemplate() {

        }

        /**
         * static final session factory
         */
        private static SessionFactory sessionFactory = null;

        /**
         * local thread variable used for storing share session instance
         */
        private static final ThreadLocal localSession = new ThreadLocal();

        /**
         * log4j logger
         */
        private static final Logger logger = Logger
                .getLogger(HibernateTemplate.class);
        /**
         * use JTA transaction
         */
        /**
         * 該工具唯一實例。
         */
        private static HibernateTemplate instance = null;
        private static Transaction tx = null;
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
        private static final Configuration cfg = new Configuration();
        /** Holds a single instance of Session */
        private static final ThreadLocal threadLocal = new ThreadLocal();

        /**
         * 獲取持久工具的唯一實例,以后不是使用單實例模式,而不是采用對象池支持。
         * @return PersistentTool
         * @throws BaseException
         */
        public synchronized static HibernateTemplate getInstance() {
            if (instance == null) {
                instance = new HibernateTemplate();
                instance.initHibernate();
            }
            return instance;
        }

        /**
         * 實現Hibernate的初始化配置環境。
         */
        public void initHibernate() {
            try {
                //此處從系統路徑中獲取配置文件
                cfg.configure(CONFIG_FILE_LOCATION);
            } catch (HibernateException ex) {
                ex.printStackTrace();
            }
            try {
                // 裝載配置,構造SessionFactory對象
                sessionFactory = cfg.buildSessionFactory();
            } catch (HibernateException e) {
                e.printStackTrace();
            }
        }

        /**
         * Get the share session
         * @
         * @return Session share session
         */
        public  Session getSession() {
            logger.debug("Now enter into getSession method of DaoUtil");
            //obtain share session
            Session session = (Session) localSession.get();
            try {
                if (session == null||!session.isOpen()) {
                    //get session by session factory
                    session = sessionFactory.openSession();
                    localSession.set(session);
                }
            } catch (HibernateException ex) {
                ex.printStackTrace();

            }
            return session;
        }

        /**
         * Close share session
         * @
         */
        public  void close() {
            logger.debug("Now enter into closeSessionl");
            //obtain share session
            Session session = (Session) localSession.get();
            localSession.set(null);
            if (session != null) {
                try {
                    session.flush();
                    session.close();
                } catch (HibernateException ex) {
                    ex.printStackTrace();

                }
            }
        }

        /**
         * Begin JTA transaction
         * @
         */
        public  void beginTransaction() {
            logger.debug("Now enter into beginTransaction");
            try {
                Session session = (Session) localSession.get();
                tx = session.beginTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();

            }
        }

        /**
         * Commit transaction
         * @
         */
        public  void commitTransaction() {
            try {
                tx.commit();
            } catch (Exception ex) {
                ex.printStackTrace();

            }
        }

        /**
         * Rollback transaction when breaching ACID operation
         * @
         */
        public  void rollbackTransaction() {
            try {
                tx.rollback();
            } catch (Exception ex) {
                ex.printStackTrace();

            }
        }

        /**
         * Insert a record into table
         * @param obj Object
         * @throws DAOException
         * @
         */
        public  int insertObject(Object obj) throws DAOException {
         int res = 0;
            logger.debug("Now enter into insertObject");
            //obtain current share session
            try {
                Session session = HibernateTemplate.getInstance().getSession();
                beginTransaction();
                Object robj = session.save(obj);
                if (robj instanceof Integer) {
        res = (Integer) robj;
       }
                if (robj instanceof String) {
        res =1;
       }
                session.flush();
            } catch (HibernateException ex) {
                rollbackTransaction();
                logger.error("insertObject error:", ex);
                throw new DAOException(ex);
            } finally {
                commitTransaction();
                close();
            }
            return res;
        }


        /**
         * Delete a record of database table by Hibernate po object
         * @param obj Object
         * @throws DAOException
         * @
         */
        public  boolean deleteObject(Object obj) throws DAOException {
         boolean res = false;
            logger.debug("Now enter into deleteObject method");
            //obtain current share session
            try {
                Session session = HibernateTemplate.getInstance().getSession();
                beginTransaction();
                session.delete(obj);
                session.flush();
                res = true;
            } catch (HibernateException ex) {
                rollbackTransaction();
                logger.error("deleteObject error:", ex);
                throw new DAOException(ex);
            } finally {
                commitTransaction();
                close();
            }
            return res;
        }


        /**
         * Update a record of database table
         * @param ob Object
         * @throws DAOException
         * @
         */
        public  boolean updateObject(Object ob) throws DAOException {
         boolean res = false;
            logger.debug("Now enter into updateObject");
            //obtain current share session
            try {
                Session session = HibernateTemplate.getInstance().getSession();
                beginTransaction();
                session.update(ob);
                session.flush();
                res= true;
            } catch (HibernateException ex) {
             rollbackTransaction();
              logger.error("updateObject error:", ex);
              throw new DAOException(ex);
            } finally {
                commitTransaction();
                close();
            }
            return res;
        }
    }

    posted on 2009-08-20 13:22 David1228 閱讀(1016) 評論(0)  編輯  收藏 所屬分類: Hibernate/ibatis

    <2009年8月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    文章檔案

    新聞分類

    新聞檔案

    相冊

    收藏夾

    Java

    Linux知識相關

    Spring相關

    云計算/Linux/虛擬化技術/

    友情博客

    多線程并發編程

    開源技術

    持久層技術相關

    搜索

    •  

    積分與排名

    • 積分 - 358630
    • 排名 - 154

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 日本牲交大片免费观看| 国产av无码专区亚洲av桃花庵| 一级黄色免费网站| 一本色道久久综合亚洲精品| 久久成人免费大片| 亚洲国产欧美日韩精品一区二区三区| 久久久久亚洲AV成人网| 99re免费99re在线视频手机版| 亚洲精品欧美综合四区| 国产AV无码专区亚洲AV漫画| 日本成年免费网站| 精品熟女少妇aⅴ免费久久 | 日韩高清免费在线观看| 国产一级黄片儿免费看| 亚洲制服丝袜中文字幕| 亚洲人成色777777在线观看| 最近2019中文字幕免费看最新| 香蕉免费一级视频在线观看| 亚洲中文字幕无码中文| 亚洲精品狼友在线播放| 日韩特黄特色大片免费视频| 一个人免费视频在线观看www| 亚洲AV无码专区在线观看成人| 亚洲国产高清视频| 免费人成网站7777视频| 免费视频专区一国产盗摄| 中国在线观看免费的www| 久久精品熟女亚洲av麻豆| 久久精品国产亚洲av影院| 亚洲免费无码在线| 精品剧情v国产在免费线观看 | 黄a大片av永久免费| 最近中文字幕mv免费高清视频8 | 亚洲国产成AV人天堂无码| 亚洲中文久久精品无码ww16| 免费视频中文字幕| 波多野结衣中文字幕免费视频 | 亚洲制服丝袜在线播放| 亚洲国产成人久久精品影视| 中文字幕不卡亚洲| 免费不卡中文字幕在线|