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 Struts and the fly likes How do i get a connection object in a java class ; am using struts 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 » Frameworks » Struts
Bookmark "How do i get a connection object in a java class ; am using struts datasource" Watch "How do i get a connection object in a java class ; am using struts datasource" New topic
Author

How do i get a connection object in a java class ; am using struts datasource

Anil Verghese
Ranch Hand

Joined: Oct 11, 2006
Posts: 155
Hi,

I have configured my appln to use the struts application. I have some Java utility classes how do i get the connections over there.
Is using struts datasource efficient way to do the connection pooling if not can any one suggest a better way to do this

Anil
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Anil Verghese:
Is using struts datasource efficient way to do the connection pooling if not can any one suggest a better way to do this

No, it is not, nor does it facilitate the Model/View/Controller model that Struts is trying to promote. It is mainly for this reason that the Struts Datasource utility has been removed from later versions.

The best practice for using data in J2EE applications is to set up the data source using application server utilities that place a reference to it in the Java Naming and Directory Interface (JNDI). In your application code, you would then retrieve this reference with a JNDI lookup.

The preferred method for setting up a JNDI datasource is different for each application server. Look in the documentation for your application server under a heading such as "JNDI DataSource".

For more information on this topic, read chapter 31 of the Sun J2EE Tutorial


Merrill
Consultant, Sima Solutions
Anil Verghese
Ranch Hand

Joined: Oct 11, 2006
Posts: 155
Hi,
Thanks for the reply. What are the advantages if I create a datasource and use JNDI.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The main advantage is that it solves the very problem you started this thread about: Accessibility of the DataSource from any class. The Struts DataSource utility stores the DataSource in Application Context (i.e. the ServletContext object). From a normal POJO class, you don't normally have access to the Application Context, so you really can't get the DataSource.

If you use a JNDI DataSource, you can access it from any POJO that is running inside the Java EE container.

This makes it so you can provide proper separation of layers between Model and Controller. It's really the domain of model objects to access the database, not the controller. So, why make it so the only way to access a DataSource is in a controller object such as an action class? I believe this is the main reason why the Struts DataSource was dropped completely from later releases.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i get a connection object in a java class ; am using struts datasource
 
Similar Threads
Connection creation @ server startup
struts with datasource problem
DataSource issues
How to use connectionpooling for a datasource
Sybase DataSource