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: iBatis DAO Loading dao.xml
Date Mon, 02 May 2005 05:12:07 GMT
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