This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes storing object in servletcontext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "storing object in servletcontext" Watch "storing object in servletcontext" New topic
Author

storing object in servletcontext

Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
Hi,
I am using MVC pattern in my web application, I have a question regarding using context to store my dataoperation object(which handles all the data base related operations). What essentially I am doing is calling my action class from Controller Servlet this action class then uses dataoperation class to perform db calls. For sake of simplicity I have stored dataoperations in servlet context and all my action classes uses the servletcontext to obtain handle to data operations.
I want to know whether this is the correct approach or should I use session scope to store an instance of Dataoperation. Or is there any other way to do it.
Hope I made myself clear.
Regards,
Vijay
danny liu
Ranch Hand

Joined: Jan 22, 2004
Posts: 185
First, you must synchronize the data operations to guarantee data integrity.
Even though, the dataoperation object still may become the bottle neck of the whole web application. Especially when many requests compete for that object.
Putting it into session is more scalable.
Hope it helps.
Dan
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
If your "data operations" use a connection pool (JDBC DataSource) and don't use any member variables, you're okay. No need to synchronize it at all. Should work out just fine and would be just as scalable as storing it in the session. Remember that sessions CAN be accessed by two different threads at the same time!


James Carman, President<br />Carman Consulting, Inc.
Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
Jammy, Danny thanks for your reply.
Well I am using connection pooling and not using any member varaibles, so I hope this will work out all fine.
Cheers,
Vijay
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: storing object in servletcontext
 
Similar Threads
Thread safe
Values persisting in browser after save
ServletContext & synchronization?
Servlet, JSP, MySQL webb app
Calling Servlet method from Bean