How concerned should we be about thread safety with JSF managed beans? - multithreading

I'm working on a JSF 1.2 project which has AJAX functionality on it's pages (using RichFaces).
My bean builds a list of objects to be edited and then has methods to support the editing and the bean is session-scoped. I will be using a a4j:queue so that only one AJAX call can happen at a time.
I'm curious if it is wise to use synchronization (locks on objects, or perhaps collections from java.util.concurrent) in the managed bean. Is the extra work needed to implement synchronization/thread safety really needed? The site I am working on has many users and needs to be reliable but it has a LOT of managed beans, and I'm curious how concerned I should be about the thread safety of managed beans overall.
Do you take extra steps in backing beans for thread-safety?
Thanks in advance.

a4j:queue won't prevent the user from reloading the page / clicking another link while the AJAX call is in progress.
Yes, we can probably trust the user not to click many different links right after each other, but what about requests not triggered by the user, for instance by a4j:poll?
Note that replacing all collections with their thread-safe equivalent might not be enough to make your application thread safe.
That said, depending on the degree of reliability your application needs to meet, this problem might or might not deserve your attention.

You need to keep scope in mind.
Request scope - thread safe, session scope - not thread safe
If you need to be able to open multiple browser windows or tabs, then you can use something like a Seam conversation to protect from editing the same object from two windows/tabs.

If it is SessionScoped you must take care to use some thread-safety mechanisms. If it is RequestScoped or ViewScope'd, then it is safe to share class variables between methods.

Related

Is all access based on Thread's?

my question is about thread's,
I am studying ejb and I see that it has many thread access controls, and working with ejb I could realize or at least imagine that all the access from the "remote client" (my doubt is this, only ejb?) Is thread based?
  Or in the ejb control the accesses of the thread's, my doubt is can I say that all the access coming from the remote client are thread's or can I manipulate those accesses with the use of api Thread?
I do not know if I was very specific, if so, is this also true for JSP'S and Servlet? Or just in case the EJB's can see I'm a bit confused.
Thank you
One of the basic goals of EJBs, since the beginning, is to relieve programmers from the pains and dangers to deal with thread management. Thread management is the responsibility of the Containers and it's not allowed to developers to deal with them.
As you can see from the link below "creation and management of threads" it's not allowed:
EJB Restrictions

JavaEE 6 retrieving another user's data

Another programmer told me about a problem, that one user sometimes sees the data of another logged-in user. Probably they are requesting the same context at the same time. I thought, that is impossible to happen? Since Garbage Collection, Container-managed Transactions and JSessionID
Without looking at the code, it is hard to guess. But maybe you have hint.
He is using this structure:
JavaEE 6 coded Web Application, using EJB and Web Container seperatly on a Glassfish v3
JSF + PrimeFaces Framework
Thanks in advance
The good news is that the EJB architecture is absolutely capable of isolating data, so this will be a bug in your code.
One thing to look for is the kind of beans you're using:
If you have stateful beans, the container will make sure each client gets the right instance.
If you use stateless beans, these are shared between clients. If you're storing any client-specific state in these, this could easily get shared across sessions.
If you use singletons, you need to make sure that no session-specific state is stored, and that any shared state uses appropriate locks.
It's also worth checking your application logic - if it appears data is being shared across sessions, is it possible it's just the wrong data?
Finally, the big thing you're going to need is appropriate debug logging. You'll need to get enough information about what's going on from the log to identify where the problem is going wrong. Unfortunately these kind of contention issues can be fiddly and hard to catch, especially with a debugger, but appropriate logging will make your life much better in any case.
Of course, this is all quite vague and generic, but without more detail on the system that's inevitable. I would suggest looking for state stored on stateless beans as a first step though!

JSF/CDI session scoped bean corruption

I'm afraid this question will be a bit vague but here goes...
We're noticing some very strange, occasional behaviour in our JEE7 web application. Sometimes a user's page will suddenly start displaying data from an entirely different user's session! So far I haven't been able to replicate this phenomenon nor find any indication of the problem in the logs, however it seems like one user's page starts displaying data that is stored in a #SessionScoped CDI bean that should belong to a different user's session.
Does this behaviour ring any bells for anyone? Any ideas about where to start looking, logging, or researching?
Our app is using Glassfish 4's SSO system. We're using JSF facelets, CDI backing beans, JPA entities. All reference implementations. All pretty close to latest versions.
We've also recently introduced a couple of simple SOAP based web services. Not closely linked to the areas we're having troubles with but maybe worth mentioning.
Any pointers or ideas greatly appreciated.
I'm afraid not to be able to yield an answer to your problem, but I am not allowed to comment on your question.
We are experiencing the exact behaviour you describe in our JEE7 web application on Glassfish 4 as well. In my SO post I am describing how we tracked down the problem and found a way to bypass it. Did you meanwhile find a trace or maybe even a solution on this matter?

Appropriate use of .recycle in xpage app

I have seen several references to using .recycle to ensure you do not have memory issues with an xPage, however I am not sure how and where it should be used. I have checked the mastering xpages book, and have not found any specific reference to it. I suspect that this may be more obvious to those from a java background.
Should it be used when someone logs into the application, navigates between xpages? And what is this implication of use? Will it clear current sessions, or the sessionScope variables stored by a user? Is it user specific?
A
The recycling is only required for domino java objects and is not a XPage-specific issue.
You should recycle every domino object as soon you are not need it further, that's the golden rule.
Domino Objects are f.e. NotesSession, NotesDatabase, etc. They are accessed internally as C-Objects, and that is why it is important to "destroy" them manually. If you are accessing types of these objects in your code directly, you have to recycle them by yourself. XPages-specific objects like scoped variables are plain Java code and will be killed correctly by the Garbage collector.
For more details please have a look at this technote: Why it is important to use the Recycle() method on every Java object

Update of xPages application removes sessions variables on Domino server ... what to do with this?

During the rather larges xPages application development a realized the fact that Domino deletes alls sessionScope, viewScope, applicationScope variables in application after any change in application design(that causes some internal application reload on server). I understand this for development process, but its realy unacceptable in production because it makes inconsistences for connected users. Even simple typo correction in code or on xpage(on any xpage, not the one the user is working on) and applying changes to production application causes this deletes. Is there a way how to overcome this behaviour? (I know I can update application outside of business hours or something but its problem for new application when you need to deliver changes quickly e.g. typo fixes ...)
This has to be done because any changes in your application could cause the scoped variables ( and its contents) to invalidate. Updating an application ( any not only xpage apps ) should be done in a specified trimeframe at which there are none / limited amount of users.
This answer to a previous question might provide an option for you. In the context of the original question, the suggestion for defining listeners was just to provide an opportunity to do some cleanup prior to the scopes being destroyed. These types of listeners, however, could also be used to save and restore the state of these scopes. I'd urge caution for the reasons JJTB mentions, but certainly in situations where you're making completely unrelated changes (e.g. structural, not logical), this would give you a way to prevent users from being impacted by frequent scope clearing.
jjtbsomhorst explained the reason. I want to add: don't rely on scoped variables and use beans. Beans contain "formula for cooking a fresh one" what is much safer for application. Any scoped variable computed on specific event (on load typically) will be lost forever, if you make update to application as mentioned in your question. But if you used bean - its values will be recreated whenever needed again.

Resources