Is there any practical reason to use DB helper class?

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?
[179 byte] By [fzlxa] at [2007-9-27 12:32:16]
# 1

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.

ken_robinsona at 2007-7-9 7:38:26 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

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.

fzlxa at 2007-7-9 7:38:26 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

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.

ken_robinsona at 2007-7-9 7:38:26 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
> What if I setup a DB connection for every servlet?Generally not a good idea.Typically Databases Licenses are charges based on the number of Connections. So the connection pooling concept was developed as a response. Connections are recycled between clients as needed.
MartinS.a at 2007-7-9 7:38:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
Thank you for you reply!But we are using Oracle,They do not limit the number of connection by licence. I hear that SQL server do.to ken_robinson I am currently setup DataSurece in servlet init().
fzlxa at 2007-7-9 7:38:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

> 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.

MartinS.a at 2007-7-9 7:38:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

> 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.

jschella at 2007-7-9 7:38:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 8

> 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.

ken_robinsona at 2007-7-9 7:38:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 9
To clarify, you do not setup the DataSource in your servlet, the container has it up and ready before as a result of starting the server. Your code will simply go get it from JNDI (where the server makes it available) for use.
ken_robinsona at 2007-7-9 7:38:27 > top of Java-index,Other Topics,Patterns & OO Design...
# 10

> 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!

developer@uka at 2007-7-9 7:38:27 > top of Java-index,Other Topics,Patterns & OO Design...