This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Problem in handling threading issue in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem in handling threading issue in servlet" Watch "Problem in handling threading issue in servlet" New topic
Author

Problem in handling threading issue in servlet

faisal khan
Ranch Hand

Joined: Mar 13, 2002
Posts: 47
Hi all.
Please tell me,what are the cases that i must handle the threading issues in MVC pattern.

Main servlet for handling request
Command classes for handling business logic
Jsp for view.

Since in my case Command class has instance method
public string execute (req,res){
call method of another class instance method()// for db access
}
Tod Checker
Greenhorn

Joined: Feb 19, 2004
Posts: 27
public string execute (req,res){
call method of another class instance method()// for db access
}
This is thread safe if it is a method in another class that is declared and instaniated within a service method. It is not thread safe if it is another method within a servlet class, or thread class.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

THIS IS YOUR THIRD WARNING
"faisal"-
Please adjust your displayed name to meet the
JavaRanch Naming Policy. This being your third notification, if you do not change it, your account could be closed.
You can change it
here.
Thanks! and welcome to the JavaRanch!
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
faisal khan
Ranch Hand

Joined: Mar 13, 2002
Posts: 47
thanks for answering the query.

i am still confuse that when i update/delete/add record in command execute method, it is still need of synchronization?
regards
Faisal Ishaque :roll:
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5

If that instance is local to the execute method, and it (the instance) does not use any objects that are shared by all requests, you do not have to synchronize since only one Thread can execute it.
Bill
faisal khan
Ranch Hand

Joined: Mar 13, 2002
Posts: 47
thanks
Are u saying this...
It mean that DB may be share/ not share to all requests. If share (same table then use synchronize). if not then do not use synchronize..
What abt the session , when setting cart to session ? synchronize is required?
Faisal
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
1. If you only have one DB connection, it can't be used by multiple requests "at the same time" - read up on database connection pools
2. The purpose of a session is to keep the data for a particular client separate from other clients. Putting an object reference into a session does not need to synchronize on the session. However, note that a particular client may have multiple requests running "at the same time" - with frames, images, etc. so there are circumstances in which you have to synchronize on objects in a session.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in handling threading issue in servlet
 
Similar Threads
Thread Controlling Techniques
HttpSessions getting mixed up
Does this help prevent writing to a common file at the same time ?
Handling Threading Issue ???
threading in JSE