• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in handling threading issue in servlet

 
faisal khan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
faisal khan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic