File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "storing object in servletcontext" Watch "storing object in servletcontext" New topic

storing object in servletcontext

Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
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.
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.
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.
I agree. Here's the link:
subject: storing object in servletcontext
It's not a secret anymore!