成功捷徑,貴在堅持
          人為善,福雖未至禍已遠(yuǎn)離; 人為惡,禍雖未至福已遠(yuǎn)離
          原文來自: 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) 評論(0)  編輯  收藏 所屬分類: JAVA 、FRAMEWORK
          主站蜘蛛池模板: 沂源县| 凌海市| 巴彦淖尔市| 临桂县| 嘉善县| 博客| 灌阳县| 望谟县| 定西市| 资溪县| 凤阳县| 钦州市| 三台县| 九龙县| 新乐市| 洪洞县| 庆阳市| 合作市| 大英县| 封开县| 当阳市| 淄博市| 石泉县| 安化县| 辽阳市| 海门市| 依安县| 长宁县| 高碑店市| 格尔木市| 常州市| 广南县| 凤冈县| 绩溪县| 大余县| 万载县| 土默特右旗| 梧州市| 岳西县| 莒南县| 石城县|