成功捷徑,貴在堅持
          人為善,福雖未至禍已遠離; 人為惡,禍雖未至福已遠離
          原文來自: http://www.techfaq360.com/viewFreshers.jsp?tutorialId=840
          getCurrentSession() :
          The "current session" refers to a hibernate Session bound by hibernate behind the scenes, to the transaction scope.
          A Session is opened when getCurrentSession() is called for the first time and closed when the transaction ends.
          It is also flushed automatically before the transaction commits. You can call getCurrentSession() as often and anywhere you want as long as the transaction runs.
          To enable this strategy in your hibernate configuration:

          set hibernate.transaction.manager_lookup_class to a lookup strategy for your JEE container
          set hibernate.transaction.factory_class to org.hibernate.transaction.JTATransactionFactory

          Only the Session that you obtained with sf.getCurrentSession() is flushed and closed automatically.

          Example :
          try {
          UserTransaction tx = (UserTransaction)new InitialContext()
          .lookup("java:comp/UserTransaction");

          tx.begin();

          // Do some work
          sf.getCurrentSession().createQuery(...);
          sf.getCurrentSession().persist(...);

          tx.commit();
          }
          catch (RuntimeException e) {
          tx.rollback();
          throw e; // or display error message
          }

          openSession() :
          If you decide to use manage the Session yourself the go for sf.openSession() , you have to flush() and close() it.
          It does not flush and close() automatically.
          Example :
          UserTransaction tx = (UserTransaction)new InitialContext()
          .lookup("java:comp/UserTransaction");

          Session session = factory.openSession();

          try {
          tx.begin();

          // Do some work
          session.createQuery(...);
          session.persist(...);

          session.flush(); // Extra work you need to do

          tx.commit();
          }
          catch (RuntimeException e) {
          tx.rollback();
          throw e; // or display error message
          }
          finally {
          session.close(); // Extra work you need to do
          }
          posted on 2008-07-25 10:32 選寶網an9 閱讀(1501) 評論(0)  編輯  收藏 所屬分類: JAVAFRAMEWORK
          主站蜘蛛池模板: 齐河县| 临汾市| 天门市| 兴和县| 垣曲县| 新丰县| 界首市| 大邑县| 灵武市| 绥滨县| 南宁市| 玉溪市| 玛沁县| 辛集市| 饶阳县| 香港 | 天祝| 吉林省| 玉山县| 济宁市| 剑阁县| 高密市| 平遥县| 陕西省| 桂阳县| 高平市| 苗栗县| 高要市| 天台县| 博乐市| 沂水县| 苏尼特左旗| 郧西县| 河津市| 南漳县| 盐津县| 乌拉特中旗| 长沙县| 墨玉县| 神池县| 淮南市|