aspose file tools*
The moose likes Servlets and the fly likes Database connections in Servlet 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 » Java » Servlets
Bookmark "Database connections in Servlet" Watch "Database connections in Servlet" New topic
Author

Database connections in Servlet

surendar prabu
Ranch Hand

Joined: Jul 24, 2006
Posts: 102
In my application i have around 7 or 8 servlets and some jsps.

for database connections i have my own class say, DBConnector, which when instantiated will connect to the specified database.

at present i am instantiating this class DBConnectgor in every servlet for making a database connection. but i feel that this may overload and consume the server's time if the application becomes large.
So i want this class to get instantiated when the server starts, so that the connection is made with the database and further i must be able to use this single connection all through the application for all the clients requests.

please help me in this regard. which is the best practice of maintaining database connection? should i register my database in the server's JNDI? i am using tomcat 5.0 server

regards
surendar prabu


SCJP 1.4
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

The best practice would be to use Tomcat's built-in connection pooling rather than trying to roll your own.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Instantiate it in context listener. I won't be overhead and you won't be tied to a particular server.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Durgaprasad
Greenhorn

Joined: Oct 02, 2006
Posts: 17
Yeah it will be better if you instantiate it in the context listener.
Another better solution is you need not have to instantiate the class. You can have static method which serves you the connection Object. Further you can go for singleton pattern if you want to have only one connection
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Durgaprasad,
Welcome to JavaRanch!

We're pleased to have you here with us in the servlets forum, but there
are a few rules that need to be followed, and one is that proper names are
required. Please take a look at the
JavaRanch Naming Policy and
adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

You can change it here


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
surendar prabu
Ranch Hand

Joined: Jul 24, 2006
Posts: 102
thanks for your replies guys.

Now i have used context listener and using the single instance.

some one gave me an idea of configuring the database connections directly on the server through JNDI/JDBC will give much better performance than using single instance through context.

If JNDI/JDBC gives better performance, will it be the same for all servers?]
now i using Tomcat5.0. The client may use the same or opt for some other server.

regards
surendar prabu
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,
I have a doubt....
Suppose,
you write code to create connection in the context listener...for a
database that resides at a remote location..

in due course of time..if there is a problem in network where in you are not able to access the database... and sometime later..the network problem is restored...

then what happens to the connection ??

Regards
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

A Kumar,
Please don't hijack other people's threads.
Start a new topic for your question.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by surendar prabu:
thanks for your replies guys.

Now i have used context listener and using the single instance.

some one gave me an idea of configuring the database connections directly on the server through JNDI/JDBC will give much better performance than using single instance through context.

If JNDI/JDBC gives better performance, will it be the same for all servers?]
now i using Tomcat5.0. The client may use the same or opt for some other server.

regards
surendar prabu


Yes connection pooling can go a long way toward improving performance.
Configuring it will not necessarily be the same for ever container but your code shouldn't need to change.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database connections in Servlet