Most AppServers allow for a DataSource, which is usually a Pool or Group of Connections to the same database. Any object (Servlet, EJB, DAO, etc...) can get a Connection from the Pool, use it, and put it back when finished. If you want to wrap that in a helper class, go ahead, but usually you can just get the Connection right from the Container (usually via JNDI).
A Connection per servlet could, if you have a lot of servlets, be a big hit on the database. It could also cause a bottle neck if a particular servlet has many request at once to route through that connection.
Thank you for your reply.But I do not understand the second part.If I use helper class I still have the same problem.But you remind me sth. with helper class the program become more portable.
I have one more question.
Why we need JNDI? We can setup DataSource in helper class,when it is initialized.
Most App Servers will create the DataSource for you via a control panel type interface. You give the class name, URL of the DB, username etc.. and it does the rest. It makes the DataSource available through JNDI.
As javax.sql.DataSource is an Interface, you would have to have an implementing class to instanciate before use. Usually, this is something the App Server/JDBC Driver provides.
> We are using Oracle,They do not limit the number
> of connection by licence. I hear that SQL server do.
Are you really sure ? I think perhaps you should check.
We also use Oracle (in the UK), licenced at the turn of the year and they certainly charge us based on connections.
> Is there any practical reason to use database helper
> class ( e.g. database.bookDB in Duke's bookstore)in
> servlet design? What if I setup a DB connection for
> every servlet?
Not sure about the specific example you quote but one reason for having a seperate servlet and database layer is to seperate the functionality.
Some of the benifits:
-one person can work on the servlet layer and another person can work on the database layer.
-easier to switch to a different database (particularily with different people.)
Obviously one of the downsides is greater complexity.The decision is probably based on the size of the project.
> to ken_robinson
> I am currently setup DataSurece in servlet init().
Usually (I know WebLogic is this way), if you configure the DataSource through the app server's configuration app, it is available to all applications.
The most efficient way to look up the DataSource in a particular app is have a ServletContextListener lookup the DataSource from JNDI and set it in the Context as an Attribute. Each servlet would then, in it's init() method, get the DataSource from the ServletContext (faster than using JNDI for each Servlet. The ServletContextListener requires the container you are running in support the 2.3 spec. If it does not, simply use a startup servlet (servlet that is loaded when the app is loaded) and do the same thing.
> Thank you for your reply.But I do not understand the
> second part.If I use helper class I still have the
> same problem.But you remind me sth. with helper class
> the program become more portable.
> I have one more question.
> Why we need JNDI? We can setup DataSource in helper
> class,when it is initialized.
I search in this forum about JNDI topics, I feel the same as using JNDI, why ... why we need JNDI? if we just looking for setup information, like the objects name, server's name .... we can using export values, that is simple!