This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes servletContext and database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servletContext and database" Watch "servletContext and database" New topic
Author

servletContext and database

Surya Vamshi
Greenhorn

Joined: Aug 25, 2005
Posts: 9
Hi,
I was going through HF servlets and jsp book, I read that in a class that implement servletContextListener we can define the connection to database and then use that in rest of application. I want to know how we can do that. what init parameter do we have to provide in DD.

Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If this is to be a production app, you probably don't want all requests sharing a single connection to the database.
Have you looked into the connection pooling options provided by your application server?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Surya Vamshi
Greenhorn

Joined: Aug 25, 2005
Posts: 9
Originally posted by Ben Souther:
If this is to be a production app, you probably don't want all requests sharing a single connection to the database.
Have you looked into the connection pooling options provided by your application server?


What is connection pooling. What is the disadvantage of sharing a single connection to the database.
Also want to know how to pass the parameter from to class that implements servlecontextlistener.
Makarand Parab
Ranch Hand

Joined: Dec 10, 2004
Posts: 121
Surya Vamshi
Do one thing, go to sun website and search for J2ee specification pdf. It will answer all your questions.

Regards
Makarand Parab
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Surya Vamshi:


What is connection pooling. What is the disadvantage of sharing a single connection to the database.
Also want to know how to pass the parameter from to class that implements servlecontextlistener.


The biggest disadvantage is that all your requests (Servlets are multi-threaded) will have to queue up for their turn with this single connection.

A connection pool creates and stores multiple connections and lends one to each thread, as it's needed. When the thread is done, it returns the connection to be re-used -- avoiding the overhead of creating a new connection for each request.

See:
http://jakarta.apache.org/commons/dbcp/
for a quick expanation.
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
"Makrand" let us not suggest where we can find articles. We assume that a person would have come to this forum after seeing all of them, but still if the doubts are not cleared, they look for experts to do it.

So let us provide direct answers, which help more than just pointers to links. I am sure in case of doubts we all search for it in google first ann then look out for options
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servletContext and database