Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how is better to connenct, a Class or a..

Status
Not open for further replies.

Jefhandle

Technical User
Mar 9, 2005
69
DE
hi,
i have a web application with ten classes. I hava a class DatabaseClass which provides only the connection to the database with a getConnection Method.
I thought to remove this class and put the databse concetion
String to the web.xml fild and let other classes to call
this parameter and make own connnection instead of to create an instance of DatabaseClass. What could be a problem, what is advantage, disadvantage.?

thanks
 
I suppose it depends upon things like where this web application is gonna be deployed (intranet / internet), how many hits are gonna be made on that database, will this web application likely grow in the future.

If you want to be able to grow this solution, you'll be better off having a proper 'Data Access Layer' which is dedicated to handling persistence to, and from, the DB. Specifics such as database connections would be hidden from the rest of your application in this layer. Have a google for the 'Data Access Object Pattern' and check out forum678 on this site. The guys there eat this kind of design aspect for breakfast.

For the database connection specifically, if you want your application to be responsive under loads, you'll need to use connection pooling. There are several free ones to be found in the Java world. Just google for 'Java Connection Pooling Software' or similar. Just remember, though, that things like connection pooling are PART of a good solution, and that making use of one won't magically make a poor performing web application brilliant. Good design is holistic, with all parts contributing to the whole.

Tim
 
Without going into the whole website design thing - I would certainly keep DB access to one class, that is used by all other code.

This then remains a single point of access for your classes, which makes it easier to handle updates/changes to how you connect. You could certainly still use a parameter in web.xml to define the db connection string.

As timw said also, any application using databases heavily should really look into connection pooling.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top