It's not a secret anymore!
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
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: 355

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: 355

Any suggestions?
J. Kevin Robbins

Joined: Dec 16, 2010
Posts: 1522

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: 543

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
It's not a secret anymore!