File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 340

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

Any suggestions?
J. Kevin Robbins
Ranch Hand

Joined: Dec 16, 2010
Posts: 632

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.

"There is no reason for any individual to have a computer in his home" ~ Ken Olson, Co-founder of DEC, 1977
Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 359
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: Java EE 6 Web Component Developer
Oracle Certified Expert: Java EE 6 Enterprise JavaBeans 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
Question about ejbActivate and ejbPassivate
What sort of connectivity does JDBC provides.
what kind of object can be bound to JNDI?
servlets and xml