ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brandon Goodin <brandon.goo...@gmail.com>
Subject Re: Storing the Dao or the DaoManager in the application context
Date Mon, 07 Mar 2005 18:45:36 GMT
considering everyone uses the same dao anyways... it shouldn't be a
problem. If you store an instance variable that does not change based
on the thread accessing it, you will be fine. Your Dao classes should
be thread safe anways. So, it is perfectly fine to place an instance
variable on you action class so long as it is itself thread safe.

Also, i would encourage you to use your dao as a more fine-grained
data access and to user a service layer as a place for more
coarse-grained business logic. Take a look at jpetstore4.

Brandon


On Mon, 7 Mar 2005 18:10:54 -0000, Yee, Richard K, CTR,, DMDCWEST
<Richard.Yee.ctr@osd.pentagon.mil> wrote:
> Brandon,
> Thanks for the responses. I'm using Struts. Is it still ok to store the dao
> as an instance var of an Action class? Typically, this would cause threading
> problems.
> 
> -Richard
> 
> 
> -----Original Message-----
> From: Brandon Goodin [mailto:brandon.goodin@gmail.com]
> Sent: Monday, March 07, 2005 9:24 AM
> To: ibatis-user-java@incubator.apache.org
> Subject: Re: Storing the Dao or the DaoManager in the application context
> 
> technically i guess you would. But, considering it is accessed in a static
> only once upon the class loading it wouldn't gain you much. It would get
> garbage collected regardless. Also, if it fails everything fails. But, it
> wouldn't hurt to add the close.
> 
> Usually, if i am using a dao in a class I will store it as an instance
> variable that is initialized in the constructor.
> 
> public class MyClass {
> ...
> 
> private DaoManager daoManager;
> private MyDao myDao;
> 
> ...
> 
>   public MyClass() {
>     this.daoManager = DaoConfig.getDaoManager();
>     this.myDao = daoManager.getDao(MyDao.class);
>   }
> 
> ...
> 
>   public String myMethod() {
>     return this.myDao.getSomeString();
>   }
> ...
> 
> }
> 
> Another variation to this is to use a constructor that takes the daoManager
> and myDao as parameters. This would be useful for IoC containers. If you
> don't use IoC it is still useful for mock object testing.
> 
> Brandon
> 
> On Mon, 7 Mar 2005 17:05:49 -0000, Yee, Richard K, CTR,, DMDCWEST
> <Richard.Yee.ctr@osd.pentagon.mil> wrote:
> > Brandon,
> > So basically you are saying that the DaoManager should be stored and
> > the DAOs should be obtained as they are needed, right? Also, shouldn't
> > the Reader be closed in a finally statement?
> >
> > Thanks,
> >
> > Richard
> >
> >
> > -----Original Message-----
> > From: Brandon Goodin [mailto:brandon.goodin@gmail.com]
> > Sent: Monday, March 07, 2005 7:34 AM
> > To: ibatis-user-java@incubator.apache.org
> > Subject: Re: Storing the Dao or the DaoManager in the application
> > context
> >
> > Do something like the following. Then you simply call the static
> > getDaoManager() method.
> >
> > package com.foo.myapp.dao.sqlmap;
> >
> > import com.ibatis.common.resources.Resources;
> > import com.ibatis.dao.client.DaoManager;
> > import com.ibatis.dao.client.DaoManagerBuilder;
> >
> > import java.io.Reader;
> >
> > public class DaoConfig {
> >
> >   private static final DaoManager daoManager;
> >
> >   static {
> >
> >     try {
> >       String resource = "com/foo/myapp/config/dao.xml";
> >       Reader reader = Resources.getResourceAsReader(resource);
> >       daoManager = DaoManagerBuilder.buildDaoManager(reader);
> >     } catch (Exception e) {
> >       throw new RuntimeException("Could not initialize DaoConfig.
> > Cause: " + e);
> >     }
> >   }
> >
> >   public static DaoManager getDaomanager() {
> >     return daoManager;
> >   }
> >
> > }
> >
> > Brandon
> >
> > On Mon, 07 Mar 2005 07:09:03 -0800, Richard Yee <ryee@cruzio.com>
> > wrote:
> > > Hi,
> > > I'm developing an application using the iBATIS DaoManager and
> > > currently store the dao's that I get by calling DaoManager.getDao( )
> > > in the application context at the application startup.. I was
> > > wondering if this is correct. Should I instead be storing the
> > > DaoManager in the application context at startup and then calling
> > > the
> > > getDao() method from it for each request?
> > >
> > > Thanks,
> > >
> > > Richard
> > >
> > >
> >
>

Mime
View raw message