This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes ServeltContextListener for DataSource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "ServeltContextListener for DataSource" Watch "ServeltContextListener for DataSource" New topic

ServeltContextListener for DataSource

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 347

While discussing about ServletContextListener, HFSJSP 2e says that it is used for initializing a DataSource. Is this a good approach? I've seen the DataSource objects are handled by the container itself. What is the best way?
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 347

Any suggestions?
J. Kevin Robbins

Joined: Dec 16, 2010
Posts: 853

My take on this is that it's a good way to do it because the datasource lookup only happens once. I've seen other code where the lookup happens over and over again which is a waste of resources. I've tried different ways of doing it and based on feedback that I received here, this is the context listener that I now use.

This is the DataSourceHolder class:

Suggestions for improving this are welcome.

"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Dieter Quickfend

Joined: Aug 06, 2010
Posts: 512

If you're using Java EE 6, there are many good ways, one of the good ways is contextInitialized, the easiest good way is @Resource.

Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Singh Harmeet
Ranch Hand

Joined: Aug 05, 2011
Posts: 115

is there also possible to initialize DataSource in context.xml file?

With Regards :-
Harmeet Singh
I agree. Here's the link:
subject: ServeltContextListener for DataSource
Similar Threads
JDBC - As/400 CCSID = 65535 problem
What sort of connectivity does JDBC provides.
servlets and xml
Question about ejbActivate and ejbPassivate
what kind of object can be bound to JNDI?