Return-Path: Delivered-To: apmail-incubator-ibatis-user-java-archive@www.apache.org Received: (qmail 21227 invoked from network); 7 Mar 2005 18:11:02 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 7 Mar 2005 18:11:02 -0000 Received: (qmail 34090 invoked by uid 500); 7 Mar 2005 18:11:01 -0000 Delivered-To: apmail-incubator-ibatis-user-java-archive@incubator.apache.org Received: (qmail 34073 invoked by uid 500); 7 Mar 2005 18:11:01 -0000 Mailing-List: contact ibatis-user-java-help@incubator.apache.org; run by ezmlm Precedence: bulk Reply-To: ibatis-user-java@incubator.apache.org List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list ibatis-user-java@incubator.apache.org Received: (qmail 34046 invoked by uid 99); 7 Mar 2005 18:11:00 -0000 X-ASF-Spam-Status: No, hits=0.1 required=10.0 tests=FORGED_RCVD_HELO X-Spam-Check-By: apache.org Received-SPF: neutral (hermes.apache.org: local policy) Received: from ddsmttayz003.osd.mil (HELO ddsmttayz003.ptc.pentagon.mil) (140.185.1.132) by apache.org (qpsmtpd/0.28) with ESMTP; Mon, 07 Mar 2005 10:10:59 -0800 Received: by ddsmttayz003.ptc.pentagon.mil with Internet Mail Service (5.5.2657.72) id ; Mon, 7 Mar 2005 18:11:21 -0000 Message-ID: From: "Yee, Richard K, CTR,, DMDCWEST" To: "'ibatis-user-java@incubator.apache.org'" Subject: RE: Storing the Dao or the DaoManager in the application context Date: Mon, 7 Mar 2005 18:10:54 -0000 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2657.72) Content-Type: text/plain X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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 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 > 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 > > > > >