• Post Reply Bookmark Topic Watch Topic
  • New Topic

Weird Behavior  RSS feed

 
Carlos Conti
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am newbie with JavaEE and am facing a very curious problem, my managed-beans get somehow reinitialized on execution, and I am not being able to identify exactly the reason. We've recently identified that it uses to happen when more than a user is using the application at the time.

I presume that the container might re-launch initialization of the beans upon a new request in a new session, on all active sessions!, since this only happens when a second user starts the app. And as soon as that happens, PropertyNotFound Exceptions appear everywhere.

All I am using are session beans, but from my understanding that shouln't be the problem since session-beans are session bound, which means each session has its own managed-bean collection and do not interfere. Another thought is if I am properly handling the entitymanager of the persistent context, since I am starting it manually in the application, instead of letting it to be managed automatically by the container. May that be a problem? My entitymanager is a static instance, but all methods declared withing the entitymanager are synchronized, to prevent any concurrency problems.

Where do all the bean initialization process takes place? Perhaps I can debug that point and through the call-stack I can figure out the reason?

Any ideas why that strange behavior arises?

Any thoghts are welcome! Many thanks in advance.

Carlos.
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beans are instantiated on-demand. Usually the demand is a page request for a View that references the bean.

Instantiation consists of the following:

1. Creation of the blank bean by the Java classloader

2. Invocation of the bean's no-argument constructor method (if present) by the java classloader.

3. Injection of managed properties into the bean by the JSF framework using the bean's property "set" methods

4. Invocation of the bean's @PostConstruct method (if present) on platforms that support @PostConstruct.

Once initialized, the bean stays initialized. A new instance of the bean will be initialized just like the first instance was, but the first instance will never be re-initialized. I often have a separate internal "init()" method for beans where I need to reset state manually, and it's often invoked by the constructor, but that's just part of my program logic and JSF doesn't have any special support for it.

Things that can help:

1. It's always a good idea to create a no-argument constructor, even if it doesn't do anything. If nothing else, you can set breakpoints and/or add trace statements to debug situations like this. Every bean instance has a unique object ID, so it's worth watching to see if new objects are appearing when they shouldn't be.

2. Verify that the bean scope is, in fact "session", that no application code is forcibly removing it from the HttpSession object (via removeAttribute) and that the session isn't somehow being destroyed (invalidated) and re-created.

Also, in some webapp servers, sessions may be serialized out to long-term storage and brought back in (deserialized) when the server is restarted. Deserialization does not invoke the constructor or cause JSF to inject any properties, and that can cause obscure problems. This is not usually a problem, but if you suspect it, clean out the serialization storagebefore restarting (for Tomcat, it's the TOMCAT_HOME/work directory), and/or force the session to be cleared via logout/invalidate.
 
Carlos Conti
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim,

will a have a quick check through the managed-beans constructor (simple and nice check). That should help at least to get a call-stack and perhaps see what is the root of the problem.

Thank you very much.

Carlos.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!