Results 1 to 6 of 6

Thread: Lazyinitializationexceptions with HttpInvoker

  1. #1
    Join Date
    Jul 2007
    Posts
    4

    Default Lazyinitializationexceptions with HttpInvoker

    Hi,

    I am new to remoting with Spring. I am trying to expose some services using the spring HttpInvoker. The service is setup exactly as given in the spring reference documentation. (http://www.springframework.org/docs/.../remoting.html)

    Our app uses hibernate for persistence and as such has quite a few lazy loaded collections. With HttpInvoker I am constantly getting -

    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: model.Account.characters - no session or session was closed

    what do I need to do to avoid these exceptions with HttpInvoker and return the collections to the client ?

    Any help is appreciated.

    Thanks in advance

  2. #2

    Default

    Hello,

    how do you open Hibernate sessions in your application ? Or in you unit tests ? If you use a filter in your application is it also configured for the remoting servlet ?

    -Patrick

  3. #3
    Join Date
    Jul 2007
    Posts
    4

    Default

    We use OpenSessionInViewFilter for hibernate sessions and it is configured for all url patterns

    <filter-name>OpenSessionFilter</filter-name>
    <url-pattern>/*</url-pattern>


    I tried initializaing my collection before sending it and I now get SocketException- write error.

    java.net.SocketException: Software caused connection abort: socket write error
    ClientAbortException: java.net.SocketException: Software caused connection abort: socket write error
    at org.apache.catalina.connector.OutputBuffer.realWri teBytes(OutputBuffer.java:366)
    at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(B yteChunk.java:403)
    at org.apache.tomcat.util.buf.ByteChunk.append(ByteCh unk.java:323)
    at org.apache.catalina.connector.OutputBuffer.writeBy tes(OutputBuffer.java:392)
    at org.apache.catalina.connector.OutputBuffer.write(O utputBuffer.java:381)
    at org.apache.catalina.connector.CoyoteOutputStream.w rite(CoyoteOutputStream.java:88)
    at java.io.ObjectOutputStream$BlockDataOutputStream.d rain(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.f lush(Unknown Source)
    at java.io.ObjectOutputStream.flush(Unknown Source)
    at java.io.ObjectOutputStream.close(Unknown Source)
    at org.springframework.remoting.httpinvoker.HttpInvok erServiceExporter.writeRemoteInvocationResult(Http InvokerServiceExporter.java:225)
    at org.springframework.remoting.httpinvoker.HttpInvok erServiceExporter.writeRemoteInvocationResult(Http InvokerServiceExporter.java:197)
    at org.springframework.remoting.httpinvoker.HttpInvok erServiceExporter.handleRequest(HttpInvokerService Exporter.java:83)
    at org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:44)
    at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:717)
    at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:658)
    at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:392)
    at org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:357)


    Caused by: java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)

    Am I missing some configuration or something here ?

  4. #4
    Join Date
    Jun 2007
    Posts
    16

    Default

    This type of architecture doesn`t support Lazy loading. Since your session is on the server side. When you receive the hibernate object on the client side the session is close and when you try to access collection you get a LazyInitializationException. search the forum you will see many post about that.

  5. #5
    Join Date
    Jul 2007
    Posts
    4

    Default

    Right...the server session closes down and is not available on client. So what do I need to do to avoid exception on the client and get back the list returned from the server ? I didn't see a specific solution given on the forums.

  6. #6
    Join Date
    Jun 2007
    Posts
    16

    Default 2 solutions

    Hello

    You need to load completely the object on the server side before sending back to client side. This has a real impact of performance because you lost the lazy loading possibility.

    I am doing the same kind of project at the moment. And I have found 2 ways to do this.

    You have to test them both.

    First way : use the join fetch mode.

    Criteria criteria = getSession() .createCriteria(Master.class)
    .add(Restrictions.eq("id", aId))
    .setFetchMode("details", FetchMode.JOIN)
    return (Master) criteria.uniqueResult();
    Or you can also set the fetch mode in the xml.hbm.

    This method will generate sql query with a join on the details table.
    If you have many object you want to load this query can have poor performance.
    Also since you use the Criteria the cache will not be hit.

    Second way : Use the Hibernate.initialize

    Master master = (Master) getSession().get(Master.class, aId);
    Hibernate.initialize(master.getDetails());
    return master
    This method will generate multiple select instead of one big select.
    This method also uses the cache because you use the get method.


    Both can be use. You have to test each of your query to see with solution fit the best.

    If anyone have another solution. Please reply to this post I am working right now to improve performance of my application.

    Francis

Posting Permissions

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