Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread safe Please help!!!

 
dee pale
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which model is thread safe:
import javax.servlet.http.*;
Part 1:
public class SetUpAccount extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
Account account = new Account();
req.getSession().setAttribute("Account", account);
......
req.getSession().getAttribute("Account");
}
}
PART 2
public class SetUpAccount extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
Account account = new Account();
req.setAttribute("Account", account);
......
req.getAttribute("Account");
}
}
I think its part two.
Pleas help
Thanks
Deee
 
Asher Tarnopolski
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right, while request objects are threadsafe, session objects are not .
you can read this:
http://www.coderanch.com/t/164301/java-Web-Component-SCWCD/certification/Objective-Thread-Safety-Sesion-Object
 
wei wu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are terribly wrong!
In fact, request and response objects are not thread-safe.
You can see Servlet 2.4 specification SRV.2.3.3.3 Thread Safety.
 
Andri G
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
part II is safe, because the object (attribute) is bound to the request. Each request will create a separate request object.
Actually part I is pretty safe too that the object (attribute) is bound to a session, and in practice there's only one user per session so a user can only make one request at a time, right? Forget about hacking/bot/etc
Anyway, part II is safer
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A user can make more than one request at a time if:
- an application page uses <frame>s, or
- if it includes non-static images, and if the application requires access to the session object to serve those images.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron is correct. Additionally, at least with IE on Windows, it is possible for multiple browser windows to share cookies and therefore share sessions.
hth,
bear
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right -- in any browser, a user can "Open in New Window" multiple links, potentially causing multiple requests to run simultaneously in the same session.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic