Mar 27th, 2007, 02:47 PM
Remoting + Hibernate3 + Spring + .NET / Swing clients
The problem is pertaining to the Lazy loading issues that we face when we use Remoting + Hibernate3 + Spring + .NET / Swing clients.
I have searched endless blogs / mailing lists and tried ALL possible options to make this combination work, before posting this email to you.
We have around 55 beans most of them related to each other via OneToMany and ManyToMany relationships, some being Lazy and some Eager. Collection types vary from ArrayList / HashMap and HashSet. This is a quite a complex maze of relationships that we have drawn. I have avoided getters / setters as advised by Java Beans and all our properties are public defined.
We have loaded the servlet using latest versions of Hessian / Burlap and HTTP Invoker. We would prefer to use Hessian because we are also connecting to our service via .NET clients. The servlet loads all 3 remoting methods and I try to connect via each of the transports individually from the client.
NOTE: I am using the latest version of Hibernate3 whose List / Map / Set definitions have changed since the earlier versions of Hibernate2. Most of the solutions provided pertained to Hibernate2 with simple class structures.
Solutions tried for the problem:
We have a maze of relationships and many such objects loading, I have tried the following options and NONE of them worked in any of the remoting methods:
1) I am trying various options to detach loaded Objects from Hibernate Cache, which I think would then make it easier to transmit data using any of the remoting methods.
- Do a Session.clear() so that all the loaded hibernate objects in the current session get detached!
- Recuersively traverse the Hibernate Tree and do a evict of all the objects, so that the objects get detached from the cache
2) Another option was to build a custom serializer for Hessian which will convert the Hibernate Collections to Java Collections.
- CustomSerializerFactory for Hessian that overrode ListSerializer / MapSerializer / SetSerializer for Hibernate3 (and not List / Map / Set interfaces of Hibernate2)
When using HTTPInvoker I get
nested exception is java.io.StreamCorruptedException: invalid type code: AC
Caused by: java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
When using Hessian / Burlap I get
SEVERE: failed to lazily initialize a collection, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollect ion.throwLazyInitializationException(AbstractPersi stentCollection.java:358)
When using Hessian with Custom Serializer I get
Caused by: com.caucho.hessian.io.HessianProtocolException: expected end of map ('z') at '?'
at com.caucho.hessian.io.HessianInput.error(HessianIn put.java:1647)
at com.caucho.hessian.io.HessianInput.readMapEnd(Hess ianInput.java:1225)
Can someone help / guide / advise me please.
Thanks in advance,
Last edited by rahuldevblore; Mar 27th, 2007 at 03:03 PM.
Mar 28th, 2007, 10:54 PM
Can someone PLEASE help / guide me on how we could proceed? Our customer is a leading banker here in India and their services are currently stuck until we manage to get a solution to this LazyLoading issue with Hibernate 3.
Mar 29th, 2007, 01:56 PM
Hi, I haven't used hessian but I'll try to help.
There're two things here: LazyInitializationException and the transmission problem.
1) LazyInitializationException: objects cannot be lazily loaded after the session has been closed or after you send them to another JVM. I assume you know all about this.
2) Transmission problems: I read a while ago that hessian/burlap doesn't play well with CGLIB generated classes (which hibernate uses a lot), so I suppose you'll have a very hard time trying to make things work that way.
Maybe you should consider creating DTOs for the conflicting cases, this doesn't sound very nice, but at least you have a workaround.
I don't know why the HTTPinvoker version failed (you mean spring http invoker, right?). That uses standard java serialization and it should work without problem.
Mar 29th, 2007, 03:30 PM
Thanks Federico for your response,
on your response on "1) LazyInitializationException: objects cannot be lazily loaded after the session has been closed or after you send them to another JVM. I assume you know all about this."
Yes, I am aware of the LazyInitializationException that occurs when the session is closed. To avoid this I have tried to evict ALL objects by recursively spanning the entire hibernate tree!!! I do this before the session is closed.
I am told that when you evict() the objects, the objects loaded by Hibernate will become detached. This means that Lazy loaded objects will then be NOT loaded in ANY case and LazyInitializationException will NOT occur. I am also "assuming" that the CGLIB issues will also be resolved this way.
on 2) yes it is HTTP remoting that spring released, that i was talking about. I think the exception that occurred "nested exception is java.io.StreamCorruptedException: invalid type code: AC", is again probably because the class files would have been changed due to instrumentation. Hence, during Serialization (at the Server) / Deserialization process (at the Swing client), maybe the class structures / data read would be inconsistent.
I have tried to write a Custom Hessian Serialization routine, which will convert all Hibernate related classes to java.util.Collection related classes. Still I continued to face this problem.
I think in Hibernate3, they have changed the way Hibernate Collections were represented. So Hibernate List is now PersistantHibernateList. So during Hessian Custom Serialization, instead of copying Hibernate List -> java.util.List I recursively convert PersistantHibernateList -> java.util.List. Still the same problems :-(
I have been breaking my head over this for the last 2 weeks now. I am using the latest versions of Hibernate and Spring. I am sure many people would face the same problems as I am facing.
Unfortunatley, the Hibernate forum is not responding to our questions. I presume they have mis understood the problem. And we are desperately STUCK!
How I wish the Hibernate team would have placed some documentation and supporting code to resolve this common need.
Thanks for your response. Do let me know if you see any solution to my problem OR if we are doing it the wrong way!
Mar 29th, 2007, 04:12 PM
Actually I think evicting the objects is not necessary to detach them, because hibernate automatically detects its state, i.e. they become detached when you close the session. However the important issue here is that lazy initialization exceptions will still happen if you access properties that were not previously fetched. CGLIB issues will still exists as the "special" objects are still there. I recommend you to review chapter 10 of the hibernate documentation.
Originally Posted by rahuldevblore
Actually the problem you're having is more closely related to hessian and its inability to transfer CGLIB generated classes, I think it will be better to look for answers in caucho's site.
Originally Posted by rahuldevblore
As I said creating DTO might be a valid workaround to your problem.
Mar 29th, 2007, 04:30 PM
Thank you Federico for the pointer provided.
I will do some reading and implementation on DTO and try it out.
Thanks again and appreciate your help in this matter.
Mar 30th, 2007, 03:31 PM
Your objects must be fully initialized (the hibernate way, i.e. no more proxies or lazy collections) when they go on the wire.
Take a look to this blog entry, it may help you.
When using spring httpinvoker, I think you need to provide hibernate collection classes to the client. You have to put the hibernate jar in the client classpath.
With burlap/hessian, I think there is an option to tell to send the interface rather than the implementation (List vs ArrayList), so no need of hibernate jar on the client side.
Anyway your objects need to be initialzed whatever remoting solution you chose.
Last edited by croudet; Mar 30th, 2007 at 03:37 PM.
May 5th, 2012, 09:39 AM