Quoting e-denton Java Programmer <[EMAIL PROTECTED]>:
> Merry Christmas,
>
> Wow, I finally connected to my data source! Now, I want to put the code
> somewhere it will be executed only once, and save the DataSource object
> where Actions, beans, etc. can get at it. That way, I don't have to perform
> the lookup all the time.
>
That turns out not to be the recommended pattern, for at least two reasons:
* If your app server supports the ability to dynamically
reconfigure DataSource objects while your app is running,
the object retrieved via JNDI might change each time.
* If you retrieve it once and then put it someplace (such as
in a servlet context attribute) then any code that wants to
retrieve it will need a reference to the ServletContext object
(and therefore be dependent on the servlet API).
> Any suggestions on where to put the one time setup code, and where to save
> the DataSource object reference (in the application?).
>
Consider setting up a utility class with a public static method called
getDataSource() that actually does the lookup every time. Then you can call it
like:
DataSource ds = MyUtils.getDataSource("jdbc/TestDB");
or, even go a step farther and have it return a connection for you ... that way
you're hiding how the connection pooling is being done as well, and just
operating at the SQL level.
> Thanks again,
>
> Will
>
Craig
> ----- Original Message -----
> From: "Vic Cekvenich" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Saturday, December 20, 2003 12:14 PM
> Subject: Re: How find DataSource?
>
>
> > See sample source code section here:
> >
>
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html
> >
> > Ex:
> > Context ctx = new InitialContext();
> > if(ctx == null )
> > throw new Exception("Boom - No Context");
> >
> > DataSource ds =
> > (DataSource)ctx.lookup(
> > "java:comp/env/jdbc/TestDB");
> >
> >
> > Of course what you do with the data source depends on your DAO
> > implementataion (iBatis, Hibrenate, etc.)
> >
> > .V
> >
> >
> > hylepc wrote:
> > > Hi,
> > >
> > > From a Data Acess Object (DAO), I need to access a DataSource (like
> > > getServletContext().getAttribute (Globals.DATA_SOURCE_KEY)). But, I
> don't
> > > have access to the servlet variable.
> > >
> > > What is the proper way to get the DataSource from a DAO which is called
> by
> > > an Action but doesn't extend Action?
> > >
> > > Thanks,
> > >
> > > Will
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]