This week's book giveaways are in the iOS and Features new in Java 8 forums.
We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line!
See this thread and this one 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 Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

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: 1183
    
  14

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