0

If I'm informed correctly, every request for/from a XPage is processed in a new thread in the JVM on the Domino server.

Hence, all objects stored in the applicationScope which might be modified concurrently by different users should be thread-safe. However, all the articles about thread safety I have read so far never say anything about the necessity of using thread-safe objects in the sessionScope.

A user could, for example, run the same XPage (which modifies a sessionScope object) in two different browser tabs at the same time.

In my opinion, that sessionScope object has to be thread-safe too, or did I get something wrong?

Valijon
  • 12,667
  • 4
  • 34
  • 67
xpages-noob
  • 1,569
  • 1
  • 10
  • 37
  • 2
    "*All objects stored in the application scope which might be modified concurrently by different users should be thread-safe.*" That statement itself is not needed. Only store real application scoped data in application scoped beans i.e. make sure you always use a right scope. They are by nature thread-unsafe (session scoped or application scoped data). Nothing is apparently done for them to be thread-safe by the server but if you ensure that you always use a correct scope for every bean, there will be no need to worry about thread-safety yourself. – Tiny Jan 03 '16 at 14:17
  • @Tiny: Thanks for the comment. I totally agree with you, except for the last part. IMO one always has to worry about thread-safety when using any of the scopes higher than the requestScope... – xpages-noob Jan 03 '16 at 14:48
  • Only if you abuse a bean scope which is inapplicable to properly designed Java EE applications. – Tiny Jan 03 '16 at 14:55
  • Related/better Q&A: http://stackoverflow.com/q/35009907 – BalusC Mar 03 '16 at 13:57

1 Answers1

1

In general, whenever there is more than one thread trying to access the same object and at least one of them tries to modify the state of the object it needs to be threadsafe.

So in this case if two browser tabs try to access same xpage or different xpages but trying to access the same session scope object and at least one of them trying to modify that object, we need to ensure it is threadsafe.

As @Tiny commented regarding the view scoped beans, "According to the JSF specification, AJAX requests made from the same view are queued on the client side. Therefore, the possibility of concurrent access in a view scoped bean is zero. A view scoped bean is not shared across different tabs / windows of the same browser".

Refer to Section 13.3.2 of jsf 2.0 specification.

  • Thanks for your answer. In summary that means that one has to consider thread safety for all objects stored in any scope but the requestScope? Because obviously a user can also send two AJAX requests that cause modifications of a viewScope object at the same time from the same XPage... – xpages-noob Jan 03 '16 at 14:32
  • @xpages-noob that is right and it applies for viewScoped objects as well. So if it is the same viewScope object that two or more AJAX requests are accessing (atleast one of them trying to update its state) then it needs to be threadsafe as well. – Madhusudana Reddy Sunnapu Jan 03 '16 at 14:42
  • 3
    @xpages-noob : According to the JSF specification, AJAX requests made from the same view are queued on the client side. Therefore, the possibility of concurrent access in a view scoped bean is zero. One has to think of using `synchronized` in a managed bean if and only if the bean is put into a wrong scope, not in properly designed Java EE applications. (A view scoped bean is not shared across different tabs / windows of the same browser). – Tiny Jan 03 '16 at 14:53
  • @Tiny: Thanks very much for that valuable information. – xpages-noob Jan 03 '16 at 15:30
  • @Tiny thanks for that helpful information. Could you post the link regarding "AJAX requests from the same view...", if you have it handy? – Madhusudana Reddy Sunnapu Jan 03 '16 at 15:39
  • 1
    @Madhusudana Reddy Sunnapu [Section 13.3.2](http://download.oracle.com/otn-pub/jcp/jsf-2.0-fr-full-oth-JSpec/jsf-2_0-fr-spec.pdf) is it it what you are looking for? – Geinmachi Jan 03 '16 at 15:57
  • @Geinmachi thanks for post the link. +1 to Tiny & Geinmachi – Madhusudana Reddy Sunnapu Jan 03 '16 at 16:08
  • @MadhusudanaReddySunnapu: Thanks again for your answer - I just realized that I had forgotten to accept it. There is one more thing I'd like to ask from you: Please replace your statement about the AJAX requests with the information about the AJAX request queueing that has been provided by Tiny. – xpages-noob Mar 03 '16 at 13:21
  • @xpages-noob you are welcome. Included Tiny's comment regarding view scoped beans. That is a good point. – Madhusudana Reddy Sunnapu Mar 03 '16 at 13:50