File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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
Bartender

Joined: Dec 16, 2010
Posts: 956
    
  13

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
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

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: http://aspose.com/file-tools
 
subject: ServeltContextListener for DataSource