*
The moose likes JDBC and the fly likes getting a database connection object in the most efficient way  :confused: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "getting a database connection object in the most efficient way  :confused: " Watch "getting a database connection object in the most efficient way  :confused: " New topic
Author

getting a database connection object in the most efficient way :confused:

Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

Hi All,

There are various ways in which one can get a DB connection in a web-app.
What could be the best possible way of getting database connection. ?

Some of the ways I know are :

1> Create a java class and get the DB connection in that class. Now where ever one needs the connection , one can instantiate that class and get the connection.

2> Use a ServletContextListener ... >> One can use a listner.. A contextlistener is available application wide and can be used throughout the web-app.

3> Using hibernate one can define the connection params in the hbm-cfg.xml file..(if i correctly remmer) and get the connection..

4> DataSource using jndi -- (xxx-ds.xml) -- In server deploy folder one can create a xxx-ds.xml file.. and specify the connection PARAMS.. then using lookup (jndi name) one can get access to db obj..

Could smone explain which one is the best and why... And also mention other ways of get db connection that are in vogue..
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

This isn't really a Struts question. Struts is a front-end controller. It has nothing to do with back-end tasks like database connections. In any case, you will find this FAQ entry interesting.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

5> Spring and injection.

Moving to... um... JDBC?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336



3> Using hibernate one can define the connection params in the hbm-cfg.xml file..(if i correctly remmer) and get the connection..

This will just piggyback on either a DataSource or use the DriverManager directly depending on your environment. Choosing Hibernate just to get create a JDBC connection is not a sensible choice though.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getting a database connection object in the most efficient way :confused: