Design question : Caching DAO instance in ejbCreate()

Discussions

EJB design: Design question : Caching DAO instance in ejbCreate()

  1. Hi,

    Can I cache the instance of DAO in ejbCreate() ? I want to re-use the same instance of DAO across many ejbs.

    Thanks
  2. Hi,
    As long as the DAO is thread safe I gues is ok.
    Best regardds, Mircea
  3. Wrong .. don'ttake into account this .. I didn't know what I was thinking ... EJBs are thread safe by themselfs.
    Hi, As long as the DAO is thread safe I gues is ok.Best regardds, Mircea
  4. So, the simple answer is 'yes". We can cache the instance of DAO in ejbCreate().
  5. Correct me if i am wrong. Most of the time the DAO is going to be stateless.if that is the case i don't see any advantage in caching the DAO instance. If it is going to be stateful, you might end up in data integrity problem unless you do some sophisticated data sync. For some reason if you would like to cache, make sure you are not holding any connection.
  6. What about static methods in the DAO? Are they thread safe too?

    Thanks,
    KK
  7. By nature, the static methods are thread-safe so long as the usage is as per guidelines.
  8. Why do you want to use the same DAO instance? Is there anything you want to hold inside the DAO? Maybe the JDBC connection?

    If yes, you can cache your DAO instances in ejbCreate - but I don't think it's a good idea. If you do so the lifetime of a connection is bound to the lifetime of the EJB. This is not the most efficient usage of database connections.

    If you use a connection pool there is usually no need to cache connections somewhere else. The container will handle everything for you. There is no need to hold the connection inside the DAO, get it just before you want to make the JDBC call - and release it right afterwards.

    Cheers,

    Andreas Berg.
    http://www.triona.de/