Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9340
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5> Spring and injection.

Moving to... um... JDBC?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic