Results 1 to 9 of 9

Thread: Session Management

  1. #1
    Join Date
    Sep 2010
    Posts
    10

    Default 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 at 04:46 PM. Reason: missed some words

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Can you post your dao method code and test code?
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Sep 2010
    Posts
    10

    Default 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>

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Sep 2010
    Posts
    10

    Default

    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.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Sep 2010
    Posts
    10

    Default

    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.

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Sep 2010
    Posts
    10

    Default

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •