GeeCON Prague 2014*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Request Attributes Thread Safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Request Attributes Thread Safe?" Watch "Request Attributes Thread Safe?" New topic
Author

Request Attributes Thread Safe?

Andrew Rigsby
Greenhorn

Joined: Oct 05, 2006
Posts: 15
Are request scoped attributes thread safe?

I originally thought they were until I read an article on the Internet which seems to have confused me! Can anyone clarify this for me?

Thanks!!
Tridib Samanta
Ranch Hand

Joined: Apr 28, 2004
Posts: 128
Yes. Request attributes are thread safe until and unless you are accessing the attribute object from other part of your application.


Thanks, Tridib
SCJP 1.4, SCWCD 1.4, SCBCD 5.0
Andrew Rigsby
Greenhorn

Joined: Oct 05, 2006
Posts: 15
Thanks!!

So, if asked in the SCWCD exam whether request scoped attributes are thread safe the answer is 'Yes'.

Do you need to consider the fact that a reference to the request attribute may be being used in another part of the application in the exam?
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

They are thread safe.

Don't confuse thread safety with the pass-by-reference concept.

You only get one thread per request, so request attributes are thread safe.


A good workman is known by his tools.
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Marc Peabody wrote:They are thread safe.

Don't confuse thread safety with the pass-by-reference concept.

You only get one thread per request, so request attributes are thread safe.


Implementations of the request and response objects are not guaranteed to be thread safe. This means that they should only be used within the scope of the request handling thread. References to the request and response objects must not be given to objects executing in other threads as the resulting behavior may be nondeterministic.
-- SRV. 2.3.3.3.

what you should do on the exam? yes or not?-

best regards.
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

For exam answer should be yes. I have seen some questions in mock exams with this answer, and HFSJ also mentions this.


Piyush
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Piyush Joshi wrote:For exam answer should be yes. I have seen some questions in mock exams with this answer, and HFSJ also mentions this.


thanks Piyush service method is thread safe for the exam purpose correct?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Cristian Daniel Ortiz Cuellar wrote:Implementations of the request and response objects are not guaranteed to be thread safe.

Note that this topic is about request attributes, not the request and response objects. While it's certainly to pass those objects to other threads, that's almost willfully bad design :-)

A request attribute, OTOH, could be any old object that also exists elsewhere in the web app - for example, as context or session attribute, which would make it not thread-safe.
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Tim Moores wrote:
Cristian Daniel Ortiz Cuellar wrote:Implementations of the request and response objects are not guaranteed to be thread safe.

Note that this topic is about request attributes, not the request and response objects. While it's certainly to pass those objects to other threads, that's almost willfully bad design :-)

A request attribute, OTOH, could be any old object that also exists elsewhere in the web app - for example, as context or session attribute, which would make it not thread-safe.


i got it. but everything inside a service method is thread-safe for the exam purposes.??

thanks..
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I don't know what "the exam purposes" are, but I assume it's supposed to reflect reality - where it is perfectly possible to write thread-unsafe code in a service method. So, nothing inside a service method is thread-safe unless (and until) you ensure that it is.
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Tim Moores wrote:I don't know what "the exam purposes" are, but I assume it's supposed to reflect reality - where it is perfectly possible to write thread-unsafe code in a service method. So, nothing inside a service method is thread-safe unless (and until) you ensure that it is.


a question like this is what i mean.

which variable is thread safe.

1). local variables [is the one you declare in the service method right??]
2). session attributes.
3). request attributes
4). context attributes.

my answer would be.. 1 and 3..

is that correct... no in the real life but in the exam question they assume they are thread safe...[request and service method]..

best regards.
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Tim Moores wrote:A request attribute, OTOH, could be any old object that also exists elsewhere in the web app - for example, as context or session attribute, which would make it not thread-safe.
Please don't over-complicate the simple objective of these type of questions in exam. In these types of questions the "thread" term generally refers to the container initiated threads for servicing client requests. also request attributes refer to simple objects which are not related to any other scopes.

Also see the HFSJ page 204 which says:
Only Request attributes and local variables are thread-safe!
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Piyush Joshi wrote:
Tim Moores wrote:A request attribute, OTOH, could be any old object that also exists elsewhere in the web app - for example, as context or session attribute, which would make it not thread-safe.
Please don't over-complicate the simple objective of these type of questions in exam. In these types of questions the "thread" term generally refers to the container initiated threads for servicing client requests. also request attributes refer to simple objects which are not related to any other scopes.

Also see the HFSJ page 204 which says:
Only Request attributes and local variables are thread-safe!


Thanks Piyush i will. best regards from Venezuela.
 
GeeCON Prague 2014
 
subject: Request Attributes Thread Safe?