You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Larry Meadors <lm...@apache.org> on 2006/10/25 18:30:13 UTC

Re: Singleton DAO and mutilple threads

Use Spring. It is IMO the best way to deal with this - spend 4 hours
with it and you'll never go back to this kind of code. Really, it's
that simple.

If you absolutely can't use Spring, this works better than the example you sent:

===
Class MyService {
  private DaoManager daoManager;
  private Table1DAO table1Dao;
  private Table2DAO table2Dao;
  private static MyService myServiceInstance = new MyService();

  ...

  public static MyService getInstance() {
      return myServiceInstance;
  }

  ...

}
===

Larry


On 10/25/06, Tony Qian <da...@aol.com> wrote:
>
>  All,
>
>  I have a question regarding singleton DAO and multiple clients (threads) in
> IBATIS. If I use following codes, is thread safe if there are multiple
> clients (after I read mailing list, it gave me impression that sqlMapClient
> is thread safe)?. Is there any performance concern if I use singleton
> Service (DAO)?
>
>  Appreciate your inputs and suggestions.
>
>  Tony
>
>  Class MyService {
>    private DaoManager daoManager;
>    private Table1DAO table1Dao;
>    private Table2DAO table2Dao;
>    private static MyService myServiceInstance = null;
>
>    private MemberService() {
>      daoManager = DaoConfig.getDaoManager();
>      table1Dao = (Table1DAO) daoManager.getDao(Table1DAO.class);
>      table2Dao = (Table2DAO) daoManager.getDao(Table2DAO.class);
>    }
>
>    public static MyService getInstance() {
>       synchronized(MyService.class) {
>           if(myServiceInstance == null) {
>               myServiceInstance = new MyService();
>           }
>        }
>        return myServiceInstance;
>    }
>
>    public List getDataFromTable1(Integer someValue){
>         try{
>              Table1Example example = new Table1Example();
>               example.setValue(someValue);
>               example.setValue_Indicator( Table1Example.EXAMPLE_EQUALS)
>               return table1Dao.selectByExample(example);
>          }catche(Exception ex){
>             // do something
>          }
>     }
>
>   public void insertDataToTable1 (Table1 table1)
>       try{
>               table1Dao.insert(table1);
>       }catch(Excpetion ex){
>        }
>  }
>

Re: Singleton DAO and mutilple threads

Posted by Nathan Maves <na...@gmail.com>.
One small addition to Larry's example.  Just make sure that you use a
private constructor to ensure that only one instance is ever created.

BTW I second him to the point of using spring.

Nathan

On 10/25/06, Tony Qian <da...@aol.com> wrote:
>
> Larry,
>
> Thanks for prompt response and suggestions.
>
> Tony
>
> Larry Meadors wrote on 10/25/2006, 12:30 PM:
>
> > Use Spring. It is IMO the best way to deal with this - spend 4 hours
> > with it and you'll never go back to this kind of code. Really, it's
> > that simple.
> >
> > If you absolutely can't use Spring, this works better than the example
> > you sent:
> >
> > ===
> > Class MyService {
> >   private DaoManager daoManager;
> >   private Table1DAO table1Dao;
> >   private Table2DAO table2Dao;
> >   private static MyService myServiceInstance = new MyService();
> >
> >   ...
> >
> >   public static MyService getInstance() {
> >       return myServiceInstance;
> >   }
> >
> >   ...
> >
> > }
> > ===
> >
> > Larry
> >
> >
> > On 10/25/06, Tony Qian <da...@aol.com> wrote:
> > >
> > >  All,
> > >
> > >  I have a question regarding singleton DAO and multiple clients
> > (threads) in
> > > IBATIS. If I use following codes, is thread safe if there are multiple
> > > clients (after I read mailing list, it gave me impression that
> > sqlMapClient
> > > is thread safe)?. Is there any performance concern if I use singleton
> > > Service (DAO)?
> > >
> > >  Appreciate your inputs and suggestions.
> > >
> > >  Tony
> > >
> > >  Class MyService {
> > >    private DaoManager daoManager;
> > >    private Table1DAO table1Dao;
> > >    private Table2DAO table2Dao;
> > >    private static MyService myServiceInstance = null;
> > >
> > >    private MemberService() {
> > >      daoManager = DaoConfig.getDaoManager();
> > >      table1Dao = (Table1DAO) daoManager.getDao(Table1DAO.class);
> > >      table2Dao = (Table2DAO) daoManager.getDao(Table2DAO.class);
> > >    }
> > >
> > >    public static MyService getInstance() {
> > >       synchronized(MyService.class) {
> > >           if(myServiceInstance == null) {
> > >               myServiceInstance = new MyService();
> > >           }
> > >        }
> > >        return myServiceInstance;
> > >    }
> > >
> > >    public List getDataFromTable1(Integer someValue){
> > >         try{
> > >              Table1Example example = new Table1Example();
> > >               example.setValue(someValue);
> > >               example.setValue_Indicator( Table1Example.EXAMPLE_EQUALS
> )
> > >               return table1Dao.selectByExample(example);
> > >          }catche(Exception ex){
> > >             // do something
> > >          }
> > >     }
> > >
> > >   public void insertDataToTable1 (Table1 table1)
> > >       try{
> > >               table1Dao.insert(table1);
> > >       }catch(Excpetion ex){
> > >        }
> > >  }
> > >
> >
>
>
>

Re: Singleton DAO and mutilple threads

Posted by Tony Qian <da...@aol.com>.
Larry,

Thanks for prompt response and suggestions.

Tony

Larry Meadors wrote on 10/25/2006, 12:30 PM:

 > Use Spring. It is IMO the best way to deal with this - spend 4 hours
 > with it and you'll never go back to this kind of code. Really, it's
 > that simple.
 >
 > If you absolutely can't use Spring, this works better than the example
 > you sent:
 >
 > ===
 > Class MyService {
 >   private DaoManager daoManager;
 >   private Table1DAO table1Dao;
 >   private Table2DAO table2Dao;
 >   private static MyService myServiceInstance = new MyService();
 >
 >   ...
 >
 >   public static MyService getInstance() {
 >       return myServiceInstance;
 >   }
 >
 >   ...
 >
 > }
 > ===
 >
 > Larry
 >
 >
 > On 10/25/06, Tony Qian <da...@aol.com> wrote:
 > >
 > >  All,
 > >
 > >  I have a question regarding singleton DAO and multiple clients
 > (threads) in
 > > IBATIS. If I use following codes, is thread safe if there are multiple
 > > clients (after I read mailing list, it gave me impression that
 > sqlMapClient
 > > is thread safe)?. Is there any performance concern if I use singleton
 > > Service (DAO)?
 > >
 > >  Appreciate your inputs and suggestions.
 > >
 > >  Tony
 > >
 > >  Class MyService {
 > >    private DaoManager daoManager;
 > >    private Table1DAO table1Dao;
 > >    private Table2DAO table2Dao;
 > >    private static MyService myServiceInstance = null;
 > >
 > >    private MemberService() {
 > >      daoManager = DaoConfig.getDaoManager();
 > >      table1Dao = (Table1DAO) daoManager.getDao(Table1DAO.class);
 > >      table2Dao = (Table2DAO) daoManager.getDao(Table2DAO.class);
 > >    }
 > >
 > >    public static MyService getInstance() {
 > >       synchronized(MyService.class) {
 > >           if(myServiceInstance == null) {
 > >               myServiceInstance = new MyService();
 > >           }
 > >        }
 > >        return myServiceInstance;
 > >    }
 > >
 > >    public List getDataFromTable1(Integer someValue){
 > >         try{
 > >              Table1Example example = new Table1Example();
 > >               example.setValue(someValue);
 > >               example.setValue_Indicator( Table1Example.EXAMPLE_EQUALS)
 > >               return table1Dao.selectByExample(example);
 > >          }catche(Exception ex){
 > >             // do something
 > >          }
 > >     }
 > >
 > >   public void insertDataToTable1 (Table1 table1)
 > >       try{
 > >               table1Dao.insert(table1);
 > >       }catch(Excpetion ex){
 > >        }
 > >  }
 > >
 >