You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Michael McCallister <r2...@email.sps.mot.com> on 2001/03/12 19:43:30 UTC

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

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