You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Carles Pi-Sunyer <ca...@good.com> on 2001/03/12 18:35:16 UTC

Question about struts application model (DataSource, Business Log ic beans)

Hi,

I'm using Struts for a new application that I'm developing. I have a
question about the separation between the Action classes and the Business
Logic beans. 

My Business Logic beans are making database accesses to manipulate the
underlying data model. I am planning on using the struts db connection pool,
which is supplied through the DataSource. My problem is that access to the
DataSource is through the servlet.getDataSource() call. How do I make the
Business Logic beans unaware of the servlet/http layer (as suggested in the
developers guide) while still making the DataSource available to the
Business Logic beans? Do I have to pass a reference to the DataSource on
every call the Business Logic beans (not an option that I like), or is there
a cleaner way to do this? I also have a similar issues with logging
services.

I believe that a common way to solve this problem is to bind the DataSource
to a jndi service. I am using tomcat 3.2.1, and I don't believe that this
functionality is supplied in either Tomcat or Struts.

Thank You,
Carles

Re: Question about struts application model (DataSource, Business Log ic beans)

Posted by Michael McCallister <r2...@email.sps.mot.com>.
I resolved this by creating two pieces:

First is a class in your business logic package that manages the context 
required by the business logic beans.  This class should be a Singleton and 
should manage access to resources needed by your business logic beans such 
as configuration parameters and your DataSource.  I modeled my context 
object after the Servlet session API, but you could make it much more 
structured if you like.

Second, depending on your servlet environment, create some mechanism to 
initialize your application context object.  Using Tomcat, you could do 
this using something like the DatabaseServlet from the Struts example.  In 
this case, your DatabaseServlet gets started by the servlet container and 
it initializes your application context object (using servlet 
initialization parameters, things like a GenericDataSource object, 
etc.).  Now when your business logic beans are called, they retrieve the 
application context, ask it for a DataSource, and use it to get a connection.

When you decide to re-implement your application as a two-tier 
Client/Server application with a Swing front-end, all you need to do is 
implement some alternate method for your application context object to be 
initialized and your business logic beans are shielded from the change.

I'm not sure this is the best way to decouple your layers, but it's what 
I've come across so far.


Mike

At 12:22 PM 3/12/2001, you wrote:

>Hi,
>
>I'm using Struts for a new application that I'm developing. I have a 
>question about the separation between the Action classes and the Business 
>Logic beans.
>
>My Business Logic beans are making database accesses to manipulate the 
>underlying data model. I am planning on using the struts db connection 
>pool, which is supplied through the DataSource. My problem is that access 
>to the DataSource is through the servlet.getDataSource() call. How do I 
>make the Business Logic beans unaware of the servlet/http layer (as 
>suggested in the developers guide) while still making the DataSource 
>available to the Business Logic beans? Do I have to pass a reference to 
>the DataSource on every call the Business Logic beans (not an option that 
>I like), or is there a cleaner way to do this? I also have a similar 
>issues with logging services.
>
>I believe that a common way to solve this problem is to bind the 
>DataSource to a jndi service. I am using tomcat 3.2.1, and I don't believe 
>that this functionality is supplied in either Tomcat or Struts.
>
>Thank You,
>Carles


Re: Question about struts application model (DataSource, Business Log ic beans)

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Mon, 12 Mar 2001, Carles Pi-Sunyer wrote:

> Hi,
> 
> I'm using Struts for a new application that I'm developing. I have a
> question about the separation between the Action classes and the Business
> Logic beans. 
> 
> My Business Logic beans are making database accesses to manipulate the
> underlying data model. I am planning on using the struts db connection pool,
> which is supplied through the DataSource. My problem is that access to the
> DataSource is through the servlet.getDataSource() call. How do I make the
> Business Logic beans unaware of the servlet/http layer (as suggested in the
> developers guide) while still making the DataSource available to the
> Business Logic beans? Do I have to pass a reference to the DataSource on
> every call the Business Logic beans (not an option that I like), or is there
> a cleaner way to do this? I also have a similar issues with logging
> services.
> 
> I believe that a common way to solve this problem is to bind the DataSource
> to a jndi service. I am using tomcat 3.2.1, and I don't believe that this
> functionality is supplied in either Tomcat or Struts.
>

Binding the data source to a JNDI context is definitely attractive -- and
it is also the approach that is suggested if you are running in a
J2EE-compatible application server (which is required to support this
approach).  In such a scenario, you would configure the data source using
the tools provided by your app server, rather than declaring it in
struts-config.xml, and you'd define a <resource-ref> entry in your web.xml
file to link the data source you need to the facilities set up by your app
server.

Tomcat 3.2 does not support a JNDI context for this kind of thing, but
Tomcat 4.0 does.  By default, it uses the data source and transaction
implementations from Tyrex (http://tyrex.exolab.org), but you can also
create your own object factories if you want to do something different.

 
> Thank You,
> Carles
> 

Craig