GeeCON Prague 2014*
The moose likes Servlets and the fly likes a question about connection pooling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "a question about connection pooling" Watch "a question about connection pooling" New topic
Author

a question about connection pooling

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi there...
I'm using javaexchange (www.javaexchange.com) class as a connection pooling for my application. to set up a broker I do the following (in the init method of the servlet):

my question is: If I have several servlets, do I need to use this code in the init method of every servlet that requires database connection? What happens if I specify different pool sizes in every servlet?
The way I see it is having only one servlet with this declaration.
thanks


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Hi,
No need to use the broker object in all the Servlets.
You can use one servlet which will be instantiated when the server is started and put this broker object in the init() of the servlet and give it the application scope.
Now the broker object will have the defined connections in its pool and also you will be specifying the refresh time ( it closes the connection and creates new one ) the coneections.
This is given as connection broker parameters.
Hope this helps.


SCJP, SCWCD
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi Lakshmeenarayana,
You can use one servlet which will be instantiated when the server is started and put this broker object in the init() of the servlet and give it the application scope.

How do I give it the application scope??
The example I put is the first servlet that it is going to be invoked. That's why I start the broker in this servlet. Now, when I created a second servlet I tried to use

but I get an error. So I guess I haven't specified the application scope. How do I do that?
thanks 4 ur response
cheers
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Hi,
The HttpServlet class extends the GenericServlet class which has the implementation for ServletConfig interface methods.
One of those method is getServletContext(). This gives the context reference which holds the application level parameters.
Servlet Context allows different servlets of the application to share data.
To explain in a better way, Servlet Context is what servlets see at the servlet container. i.e. if you were in place of servlet, then the view towards the container is the Servlet context. This allows all the servlets of the application to share data.
On the context object, set the connection broker attribute. i.e.
context.setAttribute("conbroker", new ConnectionBroker(..));
Now you can access the connection broker object by using getAttribute(String name) method of ServletContext interface which is implemeted in GenericServlet class.
Cheers.
 
GeeCON Prague 2014
 
subject: a question about connection pooling