On Wed, 24 Jul 2002, Shaun Whyte wrote:

> Date: Wed, 24 Jul 2002 12:45:43 +0100
> From: Shaun Whyte <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
> To: Struts Users Mailing List <[EMAIL PROTECTED]>
> Subject: Using Data-source's with Struts
>
> I am trying to find a good way of using the data sources within a Stuts
> application.
>
> Supposing I have some Manager classes (like Facades); each of which have
> methods which perform a set of related use-cases, which abstract complexity
> of communicating with the beans.  The idea being that a Struts Action class
> would use the manager classes to perform the detailed operations, and
> therefore not only can a Stuts Action class use the facades (manager
> classes), but any other class which needs to perform the same functionality
> can also use them (e.g. it may not be an HTTP request).  My manager classes
> are created in a sub-class of ActionServlet and stored in the servlet
> context for use by Struts actions.  If it is not a Struts application which
> is not using them, then they are created in some other way.
>
> With regards to the DataSources, supposing my manager class needs to use
> data-source(s).  I don't want my manager classes to be tied to HTTP, so I
> don't want them to look in the ServletContext for the data source, which is
> where Struts stored them.
>
> Is the best solution, to create a List of the DataSource object in my
> sub-class of ActionServlet, then when I create the managers, just pass in
> the list for them to use.  That way, if another type of application which
> needs to use the managers (which isn't using Struts), it would create the
> DataSources itself, put them in a list, and, similar to my ActionServlet,
> pass them in to the manager classes when it created them.
>
> If anyone has any ideas for me, or if this is a good way of doing things,
> please reply.
>

To use a Struts-provided data source, you need to make it accesible to the
business logic that needs it.  That means you need to do one of the
following:
* Pass the data source instance as a method parameter on some call
* Pass a reference to the ServletContext at some point, so that the
  business logic can look up the appropriate attribute itself.
* Make the data source available through a well known static variable.
None of these are very attractive, but that's pretty much the choices.

If you are running on a J2EE app server (or Tomcat 4, which also supports
JNDI namespaces), I strongly suggest that you consider using the container
provided data sources, instead of the ones provided by Struts.  A typical
use of such a data source in a business logic class looks like this:

  InitialContext context = new InitialContext();
  DataSource ds = (DataSource)
    context.lookup("java:comp/env/jdbc/MyDatabase");
  Connection conn = ds.getConnection();
  ... do your thing ...
  conn.close();

where "jdbc/MyDatabase" matches a <resource-ref> that you have defined in
your web.xml file.  The actual database that this talks to is configured
as part of the deployment information for your app (in Tomcat, you put
stuff into server.xml for this).

But the important point is that this approach requires none of the above
alternatives for exposing the connection pool -- the container makes sure
that the "new InitialContext()" call always gives you the JNDI naming
context for the current application.

> Also as a second question, I have been looking at the JavaDoc's for the JDBC
> Connection Pool (from bitmechanic.com), and cannot find a class which
> implements javax.sql.DataSource to use within Struts, is this correct?
>

I don't know.  If it does have a DataSource implementation, you might be
able to use this instead of the one in Struts - but you don't really need
both.

> Many thanks!
>
> Shaun.
>

Craig


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to