Announcement Announcement Module
Collapse
No announcement yet.
Session Management Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Session Management

    This is probably an oft repeated question and yet I have a problem understanding the difference between what the documentation says vs what I am observing in my code. I have wired a hibernate DAO into spring and the code fails when I try to access an associated object lazily with a lazy initialization failed error.

    I do a session.save() on the pojo (per documentation this should persist the object. However, I notice that this does not work when the invocation is from a web container. Strangely, it works when I run a junit test for the same service.

    When I change the code to session.persist() it works for both variations. The only difference between save and persist per documentation is that save returns a serialized id whereas persist does not return anything. Can someone help.
    Last edited by sripri; Sep 3rd, 2010, 04:46 PM. Reason: missed some words

  • #2
    Can you post your dao method code and test code?

    Comment


    • #3
      Here's the code

      Thanks for your response. Long weekend and I was out (reason for the delayed response). Here are the code fragments:

      DAO Find method FindById:

      session=sessionFactory.getCurrentSession();
      Mdl1 dealer = (Mdl1) session.get("com.dao.Mdl1", id);
      //session.persist(dealer);---> Makes the web app invocation work
      session.save(dealer);
      return dealer;

      Service Method:

      public Mdl1 getDealers(String companyCode, String dlrNo){
      Mdl1Id id=new Mdl1Id();
      id.setCompanyCode(companyCode);id.setDlrNo(dlrNo);
      Mdl1 dealer=dealerDAO.findById(id);
      return dealer;
      }

      Junit:

      public void testUpdateDealers() {
      Mdl1 dealer= dealerService.getDealers("15", "414050");
      dealer=dealerService.mergeDealer(dealer);
      List addrList=new ArrayList(dealer.getAddrs().size());
      // Do some updates for example :

      for(Iterator i=dealer.getAddrs().iterator();i.hasNext()
      {
      Addrmst addr=(Addrmst) i.next();
      addr.setLastUpdatedTimestamp(new Date());
      }
      dealerService.updateDealer(dealer); --> JUnit works with save and persist
      }

      Servlet:

      dealer=dealerService.getDealers(request.getParamet er("companyCode"), request.getParameter("dealerNo"));
      dealerService.mergeDealer(dealer);
      Set addrs=dealer.getAddrs(); ---> Does not work !

      Mapping:
      <set lazy="true" name="addrs" sort="natural" cascade="all" table="ADDRMST" inverse="true">
      <key>
      <column name="COMPANY_CODE"/>
      <column name="DLR_NO"/>
      </key>
      <one-to-many class="com.dao.Addrmst" not-found="ignore" />
      </set>

      Comment


      • #4
        Please use [ code][/code ] tags when posting code.

        Unit test 1 transaction (the test method), the servlet 2 transactions and 2 sessions... After each service method the transaction is committed and the session closed. Hence yuor exceptions.

        Comment


        • #5
          Can you explain how you counted 2 trans. for servlets? Also, this works with persist() but not with save(). Per documentation the session behavior should be the same.

          Comment


          • #6
            Persist and save are different and they work differently. Check the hibernate/jpa docs for that. Differences are subtle but they are there... Basically persist refreshes the entity, save doesn't, so after the update with persist you get a fresh instance.

            I suggest a read on how spring aop and tx management works.

            psuedo code (servlet).

            Code:
            [tx-start]
            dealer=dealerService.getDealers(request.getParamet er("companyCode"), request.getParameter("dealerNo"));
            [tx-end]
            [tx-start]
            dealerService.mergeDealer(dealer);
            [tx-end]
            Set addrs=dealer.getAddrs(); ---> Does not work !
            psuedo code (junit)
            Code:
            [tx-start]
            public void testUpdateDealers() {
            Mdl1 dealer= dealerService.getDealers("15", "414050");
            dealer=dealerService.mergeDealer(dealer);
            List addrList=new ArrayList(dealer.getAddrs().size());
            // Do some updates for example :
            
            for(Iterator i=dealer.getAddrs().iterator();i.hasNext()
            {
            Addrmst addr=(Addrmst) i.next();
            addr.setLastUpdatedTimestamp(new Date());
            }	
            dealerService.updateDealer(dealer); --> JUnit works with save and persist
            }
            [tx-end]
            Do a search for open session in view, configure the filter, that way you have 1 hibernate session for the whole request, instead of multiple (for each tx).

            Comment


            • #7
              Thanks for elaborating. I did check the doc btw. Not much detail. Persist per doc is "This is useful in long-running conversations with an extended Session/ persistence context." and makes a transient instance persist.

              I ran a trace and got some interesting results (junit with save()) - It opens 2 sessions:

              Opened new Session [org.hibernate.impl.SessionImpl@1d6747b] for Hibernate transaction
              CurrentSession hash : 30831739
              Triggering beforeCommit synchronization
              Triggering beforeCompletion synchronization
              Initiating transaction commit
              Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@1d6747b]
              Triggering afterCommit synchronization
              Triggering afterCompletion synchronization
              Closing Hibernate Session [org.hibernate.impl.SessionImpl@1d6747b] after transaction

              ----> fetching lazily:

              Opened new Session [org.hibernate.impl.SessionImpl@70610a] for Hibernate transaction
              merging Mdl1 instance
              merge successful
              Triggering beforeCommit synchronization
              Triggering beforeCompletion synchronization
              Initiating transaction commit
              Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@70610a]
              Triggering afterCommit synchronization
              Triggering afterCompletion synchronization
              Closing Hibernate Session [org.hibernate.impl.SessionImpl@70610a] after transaction

              I notice the same for the web-app: So it is not that junit has a single transaction. Both function the same way.

              Comment


              • #8
                No they don't... And again use [ code][/code ] tags when posting code/logs/xml/stacktraces

                Post the full code and configuration... And remember use [ code][/code ] tags...

                Comment


                • #9
                  Can I just send you the project in a zip file?

                  Comment

                  Working...
                  X