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

  • Session closing prematurely - LazyInitializationException

    Hi everyone,

    I am getting LazyInitializationException while I am trying to add a child to a Parent....

    I am using Hibernate_v3.0.5 and Spring v1.2.3

    What's wrong with my code

    How can I let the session remain open... ?? How do I initialize Proxy? What do I need to change in my mapping file or pojo files?

    Code:
         
    [java] Country ID = 1
         &#91;java&#93; DEBUG - SessionImpl.<init>&#40;250&#41; | opened session at timestamp&#58; 4602997693628416
         &#91;java&#93; DEBUG - DefaultLoadEventListener.proxyOrLoad&#40;143&#41; | loading entity&#58; &#91;com.csam.ccp.cdc.persistence.Country#1&#93;
         &#91;java&#93; DEBUG - DefaultLoadEventListener.createProxyIfNecessary&#40;216&#41; | creating new proxy for entity
         &#91;java&#93; DEBUG - JDBCContext.afterNontransactionalQuery&#40;322&#41; | after autocommit
         &#91;java&#93; DEBUG - SessionImpl.afterTransactionCompletion&#40;403&#41; | after transaction completion
         &#91;java&#93; DEBUG - SessionImpl.close&#40;269&#41; | closing session
    Code:
         &#91;java&#93; ERROR - LazyInitializationException.<init>&#40;19&#41; | could not initialize proxy - the owning Session was closed
         &#91;java&#93; org.hibernate.LazyInitializationException&#58; could not initialize proxy - the owning Session was closed
         &#91;java&#93; at org.hibernate.proxy.AbstractLazyInitializer.initialize&#40;AbstractLazyInitializer.java&#58;53&#41;
         &#91;java&#93; at org.hibernate.proxy.AbstractLazyInitializer.getImplementation&#40;AbstractLazyInitializer.java&#58;84&#41;
         &#91;java&#93; at org.hibernate.proxy.CGLIBLazyInitializer.intercept&#40;CGLIBLazyInitializer.java&#58;134&#41;
         &#91;java&#93; at com.csam.ccp.cdc.persistence.Country$$EnhancerByCGLIB$$b6a743b5.getLabel&#40;<generated>&#41;
    And here's some code from my DAOImpl...
    Code:
    ....
    
    public String addCountry&#40;&#41; 
    	&#123;
    		Country country = new Country&#40;&#41;;
    		
    		country.setLabel&#40;"USA"&#41;;
    		
    		getHibernateTemplate&#40;&#41;.save&#40; country &#41;;
    		
    		return country.getCountryID&#40;&#41;.toString&#40;&#41;;
    	&#125;
    
    	public String addState&#40;String countryID, String label&#41;
    			throws NonExistentParentException 
    	&#123;
    		final Country country = &#40;Country&#41; getHibernateTemplate&#40;&#41;.load&#40; Country.class, Long.valueOf&#40;countryID&#41; &#41;;
    		
    		if&#40; country == null &#41;
    		&#123;
    			throw new NonExistentParentException&#40;"The Country identified " +
    					"by the supplied countryID &#40;" + countryID + "&#41; " +
    							"does not exist"&#41;;
    		&#125;
    		
    		System.out.println&#40;"Country Id = " + country.getCountryID&#40;&#41; &#41;;
    		
    		State stateToBeAdded = new State&#40;&#41;;
    		
    		stateToBeAdded.setLabel&#40; label &#41;;
    		
    		country.addState&#40; stateToBeAdded &#41;;
    		
    		getHibernateTemplate&#40;&#41;.save&#40; stateToBeAdded &#41;;
    		
    		getHibernateTemplate&#40;&#41;.update&#40; country &#41;;
    		
    		return null;
    		//return stateToBeAdded.getStateID&#40;&#41;.toString&#40;&#41;;
    	&#125;
    
    ....

  • #2
    Are you doing all of this in one transaction, using Spring's HibernateTransactionManager or JtaTransactionManager? Otherwise a sep session will be used for each operation, meaning that sessions will be closed. Look at how transaction demarcation is done in the PetClinic sample app shipped with Spring if you're unsure.

    Comment


    • #3
      Originally posted by Rod Johnson
      Are you doing all of this in one transaction, using Spring's HibernateTransactionManager or JtaTransactionManager? Otherwise a sep session will be used for each operation, meaning that sessions will be closed. Look at how transaction demarcation is done in the PetClinic sample app shipped with Spring if you're unsure.
      Hi Rod,
      any advices, how to set up the transaction boudaries (declaratively)? which methods on which objects should be in transaction? Is it good to wrap everything to a transaction, to avoid lazyInitExceptions? thanx

      Comment


      • #4
        Declarative transaction management is normally simplest and best. Transaction boundaries should normally be around service layer methods. You can use read-only transactions as an optimization if you know you won't be updating. Generally you should only access the database in a transaction.

        Note that you can still have lazy loading issues in the presentation layer, as transaction boundaries don't normally go out that far. In that case you can use fetch joins, "touch" lazy relationships, or Open Session in View approach.

        Comment


        • #5
          Originally posted by Rod Johnson
          Declarative transaction management is normally simplest and best. Transaction boundaries should normally be around service layer methods. You can use read-only transactions as an optimization if you know you won't be updating. Generally you should only access the database in a transaction.

          Note that you can still have lazy loading issues in the presentation layer, as transaction boundaries don't normally go out that far. In that case you can use fetch joins, "touch" lazy relationships, or Open Session in View approach.
          ok, thank you.
          I'm aware of OSIV approach, now not need it (at service layer).

          I like the declarative transaction idea; currently we had declared transactions for service interfaces. However, we are having LazyInitExceptions coming from DAOs (which are not in transactions, and are manipulating hibernate collections). I noticed in petclinic sample, that you have declared transaction for DAO. Do you recommend to wrap dao methods to transactions also? Should I expect effectiveness problems, if I wrap every method to transaction?

          Comment


          • #6
            Do you recommend to wrap dao methods to transactions also?
            Not necessary, transactions propagate from service layer to DAO methods, which should be called by the service layer.

            Should I expect effectiveness problems, if I wrap every method to transaction?
            I guess you mean "efficiency". No.

            Comment


            • #7
              Originally posted by Rod Johnson
              Do you recommend to wrap dao methods to transactions also?
              Not necessary, transactions propagate from service layer to DAO methods, which should be called by the service layer.

              Should I expect effectiveness problems, if I wrap every method to transaction?
              I guess you mean "efficiency". No.
              yes, that's exactly what I wanted to hear :-)
              thanx for answers

              Comment

              Working...
              X