• Post Reply Bookmark Topic Watch Topic
  • New Topic

MyFaces getter methods are called twice by two simultaneous threads  RSS feed

 
Swaminathan Natarajan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Im using MyFaces 1.2 and Tomahawk 1.1.8 . I have a problem where a drop down in my page is getting populated twice (each value in the lsit gets repeated) at times.
My managed bean is a session scoped bean.I have a selectOneMenu (with selectItems) . This drop down must be populated when the page loads.




My getter method is as follows





When i run the page TWO THREADS are running on this method which sometimes populates my list twice. That is, when the first thread completes before the start of the second one the list is proper . But if the threads execution gets mixed up my list is containing the same value twice. Plese see the system outs from the above code.

Works properly
18 creating new statusList ....
18 configValue size is .... 8
18 statusList size before setting is .... 0
18 statusList size after setting is .... 8
18 statusList size returning is .... 8
11 statusList is not null .So resetting ....
11 creating new statusList ....
11 configValue size is .... 8
11 statusList size before setting is .... 0
11 statusList size after setting is .... 8
11 statusList size returning is .... 8

List gets populated twice
18 creating new statusList ....
11 statusList is not null .So resetting ....
11 creating new statusList ....
18 configValue size is .... 8
18 statusList size before setting is .... 0
18 statusList size after setting is .... 8
18 statusList size returning is .... 8
11 configValue size is .... 8
11 statusList size before setting is .... 8
11 statusList size after setting is .... 16
11 statusList size returning is .... 16

I tried having null chceks surrounding this method but in that case both the threads returned null and my list was not polulated at all. Having synchronized block solves this problem but i feel thats not the right solution.


My question is why does JSF calls my getter method twice in simultaneous threads . I read in few forums that this happens only in case of session scoped beans. But i dint find the reason for this or the workaround to prevent this.


Can someone please help me in this..

Thanks,
Swami.
 
Rajneesh K Rajput
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swaminathan,

Actually, it is simple and expected for each new user (web-request) a different thread is created. So, don't consider just two the no. of threads will vary based on the various parallel request submitted for that specific page view. Also, you should consider that JSF sends separate request for each of images, scripts etc also, and the situation will become worst if your images and scripts are binding on the same page view and using render etc on the same method and which solely is responsible to insert update this list.

1) You simply, have to handle the list update/insert in thread safe mode if you are putting page bean to session scope, or higher.

So, what I see in your code is that this method fetchStatusList() is public. And is there any possibility that it mighe be called directly by some other component (mentioned above) also in your JSF page, please recheck that?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!