Feb 10th, 2010, 04:05 PM
Singletons and Session scoped replicated beans
Hi! I need help to solve a clustering problem.
I have a session scoped bean that has a property injected with a singleton bean (let's call it "the service").
It'all right in dev env, but when I put the application on the clustered env, I get replication error as the service class is not serializable.
The problem is the service object should not be replicated, but the replicated session object shlould be injected with the local service instance.
I cannot figure out how to make this happen.
Can anybody help?
Feb 10th, 2010, 04:10 PM
Without seeing any code/config/stacktrace it'd be difficult to say. First thing, does the object that it's complaining about not being able to replicate implement Serializable?
Feb 10th, 2010, 04:43 PM
Well, I thought it was not a specific problem, I thought I needed some hot-to-do guide.
Originally Posted by dvestal
Anyway, I'll try to give some more names.
Bean "client", Serializable class Client, is session scoped (well, it is in a custom "process" scope that is between session and request, but it's hold in session anyway).
Bean "server", NOT Serializable class Server, is a singleton scoped.
Class Client has a property of type Server.
At runtime, on a single node env, the client bean gets it own server instance without problems.
This is also true in the cluster env, but I see errors about Server class not being Serializable.
This is true, but I don't want the server instance to be serialized and replicated on the other node: what I expect is the client bean is replicated and injected with the server instance on the other node.
Are my expectaions wrong?
I believe that if each session client bean is replicated WITH its server reference, I'll come up with... several singletons on the other node! Wouldn't I?
Thanks a lot.
Feb 16th, 2010, 08:35 AM
I think you have some misunderstanding (or no understanding) how clustering works.
When the "client" class needs to be synchronized between nodes (the state of session changes) the clustering framework (not Spring but the app server) SERIALIZES the Session object with all of its subclasses RECURSIVELY to a bytestream and broadcasts it. On all other nodes this bytestream will be DESERIALIZED into objects and voila: the session object is replicated. So your "client" class will be serialized with ALL FIELDS RECURSIVELY. So a session scope bean not only must support Serializable, but MUST NOT CONTAIN ANY OBJECT THAT IS NOT SERIALIZABLE.
If you need the local service object in the code of the "client" class, you cannot use Autowiring. You must explicitely ask the Service object form code via ApplicationContext's getbean. (To get the appropiate ApplicationContext instance you may need to create an ApplicationContextHolder class or something also...)