aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Sharing Static Objects across servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Sharing Static Objects across servlets" Watch "Sharing Static Objects across servlets" New topic
Author

Sharing Static Objects across servlets

Sanjeev BhimaRao
Ranch Hand

Joined: Jul 17, 2006
Posts: 71
Hi,

I have a question of sharing DB Connection object across different servlets of the same webapp.

The current design is as follows.

Class 1 provides methods to connect to DB.
Methods of Class 1 are not static. (For each query it does a new con.createStatement()).

Class 2, 3, 4 etc declare static instance of Class 1.
During servlet context initialization - the static instance of Class 1 is initialized to new Object.
And both doPost() and doGet() of Classes 2,3 and 4 use the above created instance.

I have the following doubts.
1. how this will work if the application is scaled (when multiple servlet instances are created by the container)
2. how the single static object will be shared by the different servlet instances.
3. When the same servlet handles two different requests like GET and POST at the same time, how to ensure that something bad does not happen with the Class 1 instance.

Should I synchronize the methods of Class 1?

Kindly advice.

Regards,
Sanjeev
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Double post
http://www.coderanch.com/t/498210/Servlets/java/Sharing-Static-Objects


luck, db
There are no new questions, but there may be new answers.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

The other post is now closed so we can continue the discussion here.

First of all if I'm not wrong, Class2,3 and 4 are servlets right?? Per servlet declaration in the web.xml a single instance of the servlet is maintained by the container. But still the servlet can serve multiple requests concurrently as each request is handled by the servlet in a separate thread. To avoid synchronization issues, you can synchronize the code in Class2,3 and 4 on the static instance of Class1...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sanjeev BhimaRao
Ranch Hand

Joined: Jul 17, 2006
Posts: 71
Hi,

Sorry for the double post. I thought I could reach wider audience.

Yes, Class 2,3 and 4 are individual servlets and I could synchronize the code accessing the static objects across the servlets.
I would like to know, if there is an alternative way of sharing same DBConnection object (contained within Class 1 that implements other DB access methods like executeQuery and executeUpdate, and returns ResultSet or int values respectively) across servlets and without adding synchronization code in servlets (makes code bloat).

Is it ok (in terms of performance) to synchronize the methods of Class 1?

Regards,
Sanjeev
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

Hi Sanjeev,

There is a way which is slightly different from the way you are doing it. That is by creating a Singleton DBConnection object in the contextInitialized() method of the ServletContextListener (and using the getInstance() method to get hold of the object in the Servlets doGet() and doPost()) This way you don't need a Servlet class variable.

If your clients are potentially working on the same data then synchronizing the query methods is not enough. If two clients do a read-query before an update-query the update of client2 might be on data that has already been changed by client1....

Just for curiousity: why don't you use EJB3.0?

Regards,
Frits
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

You can also configure a DataSource for your data source in your web container / application server with a JNDI name, add the JNDI name as a <env-entry> in web.xml, and do a JNDI look up in your sign on class to get a database connection.


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Sanjeev BhimaRao
Ranch Hand

Joined: Jul 17, 2006
Posts: 71
Dear Fritz and Rajeev,

Thanks for your replies. I completed EJB 2.1 based SCBCD and did not update myself to EJB 3.0.
Also, JBoss seems to be an overkill for my requirements (too much config to learn) - I am in rapid protyping mode.

So, I am looking for a trouble free synchronization method without taking a major performance hit.

In DBConnection object the variables are method local and there are seperate methods for update and query.
Will synchronizing critical sections of the code with (this) object, resolve the issue - since there is only one static instance of the DBConnection object per servlet?


Regards,
Sanjeev




Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

Hi Sanjeev,

Synchronizing the methods will prevent al lot of problems like overwriting ResultSets but you might still have a possible problem on the data part if two clients access the same data concurrently.

Look at a simple example:
  • Client 1 reads address of Frits into the browser and shows the result: name, street, zipcode, country
  • Client 2 reads address of Frits into the browser and shows the result: name, street, zipcode, country
  • Client 1 hits the update button on the jsp and only changes the street name (leaving the other properties the same)
  • Client 2 hits the update button on the jsp and only changes the zipcode (leaving the other properties the same)
  • Client 1 submits data
  • Client 2 submits the new zipcode with the "old" street name

  • Although code-wise everything has been done with synchronized methods the data is still not as it was suppose to be...

    What is the possibility that such a thing happens in your application?
    Regards,
    Frits
    Sanjeev BhimaRao
    Ranch Hand

    Joined: Jul 17, 2006
    Posts: 71
    Hi Frits,
    Thanks for the detailed explanation.

    The situation described in your post does not apply to my application, since updates to existing records are not supported.

    Only inserts (auto_increment + by all) and deletes (only by admin) are allowed.

    So, I think I will synchronize my methods for now.

    Thanks for your continuous support.

    Regards,
    Sanjeev
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Sharing Static Objects across servlets