• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database connections in Servlet

 
surendar prabu
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best practice would be to use Tomcat's built-in connection pooling rather than trying to roll your own.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instantiate it in context listener. I won't be overhead and you won't be tied to a particular server.
 
Durgaprasad
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
surendar prabu
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Kumar,
Please don't hijack other people's threads.
Start a new topic for your question.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic