ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gregg D Bolinger <gthou...@gmail.com>
Subject Re: iBatis DAO Loading dao.xml
Date Mon, 02 May 2005 19:02:53 GMT
Ok, looking at the source would tell me that DaoManagerBuilder by
itself will parse the XML file every single time it is called.  So you
have to manually check to see if the daoManager instance is null or
not and provide the appropriate action accordingly.  So the following
statement is false:

> The dao manager does not parse it every time. It only parses once.

Am I missing something or is this correct.

Gregg Bolinger

On 5/2/05, Gregg D Bolinger <gthought@gmail.com> wrote:
> Soooo, you are saying that calling DaoManager.buildDaoManager alone
> will parse the dao.xml every single time??  Which is it?  It does or
> it doesn't.  I've got conflicting answers here now.  I guess I'll just
> break down and look at the source code for that static method.
> 
> 
> On 5/2/05, Brandon Goodin <brandon.goodin@gmail.com> wrote:
> > You will notice that your DaoManager only performs a buildDaoManager
> > when the DaoManager instance variable of your DaoManagerLoader is
> > null. After that it will simply return the existing DaoManager. It is
> > the responsibility of the developer to make sure it is loaded only
> > once. If you did not preform a null check it would definitely be
> > possible to cause the dao.xml to be parsed every time.
> >
> > Brandon
> >
> > On 5/2/05, Gregg D Bolinger <gthought@gmail.com> wrote:
> > > >The dao manager does not parse it every time. It only parses once.
> > >
> > > Is this documented somewhere?  How does that work if my code is
> > > telling the builder to build everytime a service request is made?  Is
> > > it cached somewhere in memory much like the hand made singleton class
> > > I am using?  I don't want to perform extra logic if I don't have to
> > > but I need proof it is doing as expected. :)
> > >
> > > Thanks.
> > >
> > >
> > > On 5/2/05, Brandon Goodin <brandon.goodin@gmail.com> wrote:
> > > > It would be perfectly fine to instantiate the Service class in your
> > > > web layer. But, you might want to follow a similar pattern as the
> > > > service layer does with the Dao classes. Instantiate your service
> > > > classes in the constructor and assign them to instance variables in
> > > > your Servlet, Action(Struts) or whatever you are using on the web
> > > > layer. Considering your Service classes and your Dao classes should be
> > > > thread safe you will have no problem setting them as instance
> > > > variables in the constructor.
> > > >
> > > > Brandon
> > > >
> > > > On 5/1/05, Gregg D Bolinger <gthought@gmail.com> wrote:
> > > > > Thanks for the reply.  I actually did something similar to this before
> > > > > I saw your response.  I made the following class
> > > > >
> > > > > DaoManagerLoader.java
> > > > > public class DaoManagerLoader {
> > > > >
> > > > >     public static final String DAO_XML_PATH =
> > > > > "com/intrust/anykey/database/dao/iBatis/dao.xml";
> > > > >     public static DaoManager daoManager = null;
> > > > >
> > > > >     public static DaoManager getInstance() {
> > > > >         if (daoManager == null){
> > > > >             System.out.println("Need a new one");
> > > > >              try{
> > > > >                  final Reader reader =
> > > > > Resources.getResourceAsReader(DAO_XML_PATH);
> > > > >                  daoManager = DaoManagerBuilder.buildDaoManager(reader);
> > > > >             }catch(IOException e) {
> > > > >                 e.printStackTrace();
> > > > >             }
> > > > >         }else{
> > > > >             System.out.println("Don't need a new one");
> > > > >         }
> > > > >         return daoManager;
> > > > >     }
> > > > > }
> > > > >
> > > > > And then in my XxxxServiceImpl I do:
> > > > >
> > > > > daoManager = DaoManagerLoader.getInstance();
> > > > >
> > > > > That seems to work pretty good.  With that being said, can I ask
> > > > > another question. If I have my UserService, UserServiceImpl, UserDao,
> > > > > and UserDaoImpl all setup like the documentation states, and then
in
> > > > > my application/web application I need to access the UserDAO, is it
> > > > > appropriate to instantiate it like this....
> > > > >
> > > > >  UserService userDAO = new UserServiceImpl();
> > > > >
> > > > > Or am I supposed to do it a differnet way.  I have attached the files.
> > > > >  I'd really like to know if I am doing this correctly.  Thanks.
> > > > >
> > > > > Gregg
> > > > >
> > > > >
> > > > > On 5/2/05, Brandon Goodin <brandon.goodin@gmail.com> wrote:
> > > > > > Saw a typo! oopsie :-)
> > > > > >
> > > > > > " I would not place the dao in my base service class."
> > > > > >
> > > > > > Should be:
> > > > > > "I would not place the dao manager in my base service class."
> > > > > >
> > > > > > On 5/1/05, Brandon Goodin <brandon.goodin@gmail.com> wrote:
> > > > > > > Hi Gregg,
> > > > > > >
> > > > > > > The dao manager does not parse it every time. It only parses
once.
> > > > > > >
> > > > > > > Also, I would do things a bit different than you are doing.
I would
> > > > > > > not place the dao in my base service class. I would use
a DaoConfig
> > > > > > > class that holds the DaoManager reference as an static
final instance
> > > > > > > variable. I would setup my Service classes to contain instance
> > > > > > > variable DAO classes that are loaded via the DaoConfig
class in the
> > > > > > > constructor. See the examples below.
> > > > > > >
> > > > > > > --- DaoConfig.java ---
> > > > > > > public class DaoConfig {
> > > > > > >
> > > > > > >   private static final DaoManager daoManager;
> > > > > > >
> > > > > > >   static {
> > > > > > >
> > > > > > >     try {
> > > > > > >       String resource = "org/apache/ibatis/jgamestore/dao/sqlmap/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;
> > > > > > >   }
> > > > > > >
> > > > > > > }
> > > > > > >
> > > > > > > --- CatalogServiceImpl.java ---
> > > > > > >
> > > > > > > public class CatalogServiceImpl implements CatalogService
{
> > > > > > >
> > > > > > >   private DaoManager daoManager;
> > > > > > >   private CategoryDao categoryDao;
> > > > > > >   private ProductDao productDao;
> > > > > > >   private ProductImageDao productImageDao;
> > > > > > >   private ImageDao imageDao;
> > > > > > >
> > > > > > >   /**
> > > > > > >    * regular empty constructor
> > > > > > >    */
> > > > > > >   public CatalogServiceImpl() {
> > > > > > >     this.daoManager = DaoConfig.getDaoManager();
> > > > > > >     this.categoryDao = (CategoryDao) daoManager.getDao(CategoryDao.class);
> > > > > > >     this.productDao = (ProductDao) daoManager.getDao(ProductDao.class);
> > > > > > >     this.productImageDao = (ProductImageDao)
> > > > > > > daoManager.getDao(ProductImageDao.class);
> > > > > > >     this.imageDao = (ImageDao) daoManager.getDao(ImageDao.class);
> > > > > > >   }
> > > > > > > ...
> > > > > > >
> > > > > > > public void addProduct(Product product) {
> > > > > > >     try {
> > > > > > >       daoManager.startTransaction();
> > > > > > >
> > > > > > >       // save product
> > > > > > >       productDao.addProduct(product);
> > > > > > > ...
> > > > > > >       daoManager.commitTransaction();
> > > > > > >     } finally {
> > > > > > >       daoManager.endTransaction();
> > > > > > >     }
> > > > > > > }
> > > > > > >
> > > > > > > Hope that helps,
> > > > > > >
> > > > > > > It is a BAD policy to make your service layer dependent
on the
> > > > > > > view/web layer. Your web layer can depend on your service
layer. But,
> > > > > > > your service layer should never depend on your web layer.
Likewise
> > > > > > > your Service Layer can depend on your DAO layer. But, your
DAO layer
> > > > > > > should never depend on the Service layer. Make it a rule
to maintain
> > > > > > > healthy separation in your development.
> > > > > > >
> > > > > > > _______Model______
> > > > > > > |                |             |
> > > > > > > Web-->Service-->Dao
> > > > > > >
> > > > > > > Ciao,
> > > > > > > Barndon
> > > > > > >
> > > > > > > On 5/1/05, Gregg D Bolinger <gthought@gmail.com>
wrote:
> > > > > > > > I am testing using iBatis DAO with iBatis SqlMaps
in a web
> > > > > > > > application.  Right now I have a BaseService that
looks like this:
> > > > > > > >
> > > > > > > > public abstract class BaseService {
> > > > > > > >
> > > > > > > >     public static final String DAO_XML_PATH =
> > > > > > > > "com/intrust/anykey/database/dao/iBatis/dao.xml";
> > > > > > > >
> > > > > > > >     protected DaoManager daoManager;
> > > > > > > >
> > > > > > > >     public BaseService(){
> > > > > > > >         try{
> > > > > > > >             final Reader reader = Resources.getResourceAsReader(DAO_XML_PATH);
> > > > > > > >             daoManager = DaoManagerBuilder.buildDaoManager(reader);
> > > > > > > >         }catch(IOException e) {
> > > > > > > >             e.printStackTrace();
> > > > > > > >         }
> > > > > > > >     }
> > > > > > > > }
> > > > > > > >
> > > > > > > > All my Service class extend this base service.  What
I would like to
> > > > > > > > know is if there is a way to load the daoManager into
the
> > > > > > > > ServletContext somehow so that I am not parsing the
dao.xml file with
> > > > > > > > every service request.  I'd like to do it once when
the Webapp loads
> > > > > > > > and just pull the daoManager from the ServletContext.
 Is it as easy
> > > > > > > > as creating a ContextListener to do this?  Is there
one in the API
> > > > > > > > that I don't know about?  Writing my own isn't a problem
though.
> > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > > >
> > > > >
> > > >
> > >
> >
>

Mime
View raw message