You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by emre akbas <em...@gmail.com> on 2005/09/23 10:31:38 UTC
Storing DataSource and ServletContext in the service layer
Hi all,
I sometimes need to access the datasource within the service layer and by
default only Action classes have access to it. In order to solve this issue,
I plan to write a plugin which will get the datasource and the
servletcontext at startup and store them in the service layer as static
variables. Thus, I will be able to access to datasource and servletcontext
within my service methods.
Is this a good practice? Are there any known incoveniences?
Re: Storing DataSource and ServletContext in the service layer
Posted by Craig McClanahan <cr...@apache.org>.
On 9/23/05, emre akbas <em...@gmail.com> wrote:
>
> Yes, to me too, getting datasource and then connection and passing this
> connection object to the service layer does not seem to be a good pattern.
> I
> said, by storing the datasource and servletcontext statically in the
> service
> layer may make life easier.
> I want to know various opinions about this topic. Thanks.
One serious issue with storing the ServletContext instance in your service
layer is that you make that layer dependent on the servlet API -- making it
much more complicated to test, or to use that code in non-webapp scenarios.
Some alternatives to consider:
* Pass in references to the data source (or, more likely, just the
Connection that should be used for a particular transaction).
* Configure your data sources using the JNDI namespace supported
by your container, and let the service layer do its own JNDI lookups.
* Use a dependency injection framework like Spring to inject the
connection instances as it configures the service objects.
Craig McClanahan
Re: Storing DataSource and ServletContext in the service layer
Posted by emre akbas <em...@gmail.com>.
Yes, to me too, getting datasource and then connection and passing this
connection object to the service layer does not seem to be a good pattern. I
said, by storing the datasource and servletcontext statically in the service
layer may make life easier.
I want to know various opinions about this topic. Thanks.
On 9/23/05, Lixin Chu <li...@gmail.com> wrote:
>
> i think if you use Spring Framework or something similar, you might not
> > need to do this by yourself.
>
> action class accessing DB does not seem to be a good pattern.
>
>
>
Re: Storing DataSource and ServletContext in the service layer
Posted by Lixin Chu <li...@gmail.com>.
>
> i think if you use Spring Framework or something similar, you might not
> need to do this by yourself.
action class accessing DB does not seem to be a good pattern.