Skip to main content

JSF2 controller Bean Construction & @postconstruct -right use

3 replies [Last post]
Joined: 2004-10-14

Hello ,
I have a question about how the JSF managed beans are reused accross multiple invocation of the same xhtml screens by multiple users .
Is the @postconstruct method invoked only once whem the bean is constructed ?[which is most likely]
Suppose USER A & USER B are accessing the same screen "test.xhtml" almost simultaneously and the managed bean for test.xhtml has a @postconstruct method which reads from the a database table some live currency rate which changes very fast.
Will both the users get fresh data every time they invoke the url ? Or is it the case because the managed bean is constructed only once for both the users that the data they get to see will be a bit stale?
Is iin this case having a phase listener that t loads from data base code would be a better / correct appoach ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2009-01-29

I'm not one of the gurus, but I'm quite sure of the following:
Managed Beans are per-user-instances, with one exception: An instance of an application scoped managed bean (and may be a custom scoped, not sure of that) is shared among all users.
That means that every user has its own view scoped bean in the example of yours. Thus both users will get fresh data when they invoke the url.

Hope I could help :-)

Joined: 2004-10-14

That is of big help and thanks. I was going to arrange an elaborate experiment where each user gets userid specific data at postconstruct but with your assurance I shall postpone that and carry on with the development tasks.

Joined: 2004-10-14

after further thought on this , I would like to add, on the face of it , it appears that the scope of the managed bean has something to do with when the postconstruct is invoked. However ,e.g. if it is viewscoped , it is possible that one instance of the manageed bean is managing the responses for two users ; and it that is the case my suspecion stays that postconstruict will not be invoked twice ,once for each user but only during the single construction time of a managed bean.
If any of the respected JSF gurus would comment and clarify this ,I shall be gratefull.