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.
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...
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?
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....
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.
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?
Creator of Enthuware JWS+ V6