成功捷徑,貴在堅(jiān)持
          人為善,福雖未至禍已遠(yuǎn)離; 人為惡,禍雖未至福已遠(yuǎn)離
          原文來(lái)自: 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 選寶網(wǎng)an9 閱讀(1508) 評(píng)論(0)  編輯  收藏 所屬分類: JAVAFRAMEWORK
          主站蜘蛛池模板: 商水县| 康乐县| 呼伦贝尔市| 且末县| 张家港市| 辽阳县| 南岸区| 吉木乃县| 嘉禾县| 奉节县| 云龙县| 敦煌市| 历史| 九龙城区| 平阴县| 聊城市| 青浦区| 肃宁县| 维西| 平塘县| 蓬莱市| 奉贤区| 左云县| 六枝特区| 南康市| 阿荣旗| 桂平市| 古交市| 南召县| 徐汇区| 邛崃市| 白城市| 墨脱县| 鸡泽县| 聊城市| 八宿县| 历史| 嵊泗县| 明溪县| 社旗县| 内乡县|