Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which object is better for a Servlet Thread-safe implementaion? 1

Status
Not open for further replies.

royc75

Programmer
Jun 1, 2006
127
0
0
GB
Hello,

Suppose I would like to keep my attributes in a Servlet which suppose to be Thread-safe but it does not implement the SingleThreadModel.
Which is better: To store the attributes at the HttpServletRequest object (where it is per request, therefore will be kept for each user) or at the ServletContext object (Where it is shared globaly and one can be sure everyone are reading and updating the same data)?
 
If the data is read-only, then it does not matter about thread safety.

If it is read-write, you could consider putting the mutating code in a synchronized block.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hey sedj,

10x for the anser, I assume you mean I will synchronize on the session itself. But which object considers more "Thread safe"?
 
No I don't mean to sync the session.

From your post, I took it that you have a servlet which has a bunch of data in it, which needs to be modified in a thread-safe manner. Is this not the case ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
No, it is more of a theoretical question which I saw in a certain exam than practical (This is why I wrote Suppose... :) ).
 
bbking88, we're really part of this forum to answer questions for *real* problems which people are having. A great deal of us juggle answering posts to this forum with full-time jobs and struggling to answer theoretical questions runs the risk of upsetting someone. (IMO)

Tim
 
Tim,
I know and appriciate it but does it mean that helping someone who need help in a certain question is forbidden? Is that not forums are ment? I am sorry but this answer sounds a bit patronizing to me.
 
Well, if the answer will help you, then it's not a theoretical question. I think I'm being a little too critical today. Sorry.

Tim
 
Humm, I have two cups of coffee here. Here you have one, Tim, you need it :)

We've discussed a lot of times about crab immortality, sometimes it's good to forget about real world.

But bbking, to be sincere, I didn't answser because I still don't understand the question. I don't know what a "servlet attribute" is.

When you ask real questions, you're supposed to know all details when asked for. If you're asking somethnig theorical, you should make suer you've thought about it enough to provide details enough to make it looks like an actual situation.

Cheers,
Dian
 
Hello Dian,

Allow me to present the question as is:

A developer chooses to avoid using SingleThreadModel but wants to ensure that the data is updated in a thread-safe manner.
Which two can support this design goal?

1. Store the data in a local variable.
2. Store the data in an instance variable
3. Store the data in HttpSession object.
4. Store the data in the ServletContext object.
5. Store the data in the ServletRequest object.

My two answeres would be 1 and either 4 or 5.
What would you choose?
Tim: I really apologize for wating to get out a little bit from the "real world", I too work full time yet I find the time to submit the question...
 
If you avoid using the SingeThreadModel, then your data will be accessed by multiple threads (I'm assuming).

Therefore, if your data is updatable by those threads, you'll need to make use of the java 'synchronized' keyword to protect the blocks of code which do the updating.

Here something which explains this :-

Tim
 
OK, first, if you use the HttpSession or HttpServletRequest, then it is already thread safe - because these are localized variables. Similarly if you use local variables.

You only have to worry about thread-safety when you have global data accessed by multiple threads (in which case you used the synchronized keyword, like I said in my first reply).

too work full time yet I find the time to submit the question

Well thats great for you mate - but have you seen how many posts we REPLY to ? More than the "odd question or two" !!!!!!!

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I still don't understand the question.

How can you compare local variables with instance variables? Of course local variables are thread safe, but they don't allow sharing data, same for request attributes or session variables.

I'd rephrase the question:

Which of these is a safe way to travel?

1.- Travel by car
2.- Travel by plane
3.- Stay home
4.- Dream with travelling

Of course, 3 and 4 are totally safe ... but you don't travel!

@sedj: stop complaining, confess you can't live without this ;)

Cheers,
Dian
 
Dian,
>> I still don't understand the question.
That's OK me neither, this is why I uploaded it over here... :)

Sedj,
When I asked whether I put synchronize on the session object you said no. So which Object should I synchronize? Or do you simply mean bound the critical code in a synchronized block. I will be happy to see a simple example.
 
No, I said that if you use an HttpSession object, or a HttpServletRequest object, then there is no need to synchronize - but if you have global variables for the servlet, then you need to synchronize the access to that data.

Seeing as you keep refusing to give any concrete examples of how you would propose to have this data (ie are the varibales/data global or local, or in the session object or what ?), I can't give you any examples !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I don't have a concrete example since I am still trying to understand the concept of synchronizing which I am weak at.
>>If you use an HttpSession object, or a HttpServletRequest object, then there is no need to synchronize.
Is this true because each user has his own HttpSession and HttpServletRequest objects?
And what if I am using ServletContext to store data? Is this considered as global data therefore needs to be synchronized?

 
>>>> Is this true because each user has his own HttpSession and HttpServletRequest objects?

That is EXACTLY why you do NOT need to synchronize access to those objects if you are accessing them in the servlet.

Don't start talking about ServletContexts - no-one in their right mind would follow that path IMO.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
>> That is EXACTLY why you do NOT need to synchronize access to those objects if you are accessing them in the servlet.

10X, now it's much clearer :)

>> Don't start talking about ServletContexts - no-one in their right mind would follow that path IMO.

So you don't ever use ServletContext to put attributes inside it?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top