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
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
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.
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: 973
posted
0
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...
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.