Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion - CMP vs Session EJB > DAO

 
Muhammad Asif
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i have a confusion regarding weather to use CMP or Session EJB ? I had designed all my sequence diagrams using CMPs, but then converted them to Session Beans with DAO. Now again, confused weather i made the right choice ?

Regards,
 
Joseph A Alexander
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DAO is just a J2EE pattern. You can have CMP/BMP/POJO or anything in this layer. [ EJB ---> DAO (CMP Bean/BMP Bean/HIbernate Classes/Any POJO).

Hope this helps.
 
Muhammad Asif
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joseph,

By DAO i mean a dataaccess object, containing data access logic. So if i write a DAO, it obviously means i shouldn't write a CMP or BMP ? or can i mix and match ? What are the pros and cons in doing this ?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A CMP + a DAO is not possible since, with DAO, your aim would be to wrap the data access logic, but with CMP the container handles the data access logic. Either use BMP + DAO or CMP. Either will be fine unless the requirement specifically asks you to use BMP or CMP. The DAO helps you move to CMP and to have better modularity. The con is that you are adding an extra layer of abstraction.
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Muhammad Asif:
Joseph,

By DAO i mean a dataaccess object, containing data access logic. So if i write a DAO, it obviously means i shouldn't write a CMP or BMP ? or can i mix and match ? What are the pros and cons in doing this ?


BMPs are generally realized by using DAOs...

you may mix and match, but you have to justify....

basically, with dao the developer has the full control of the database access layer. It is sometimes not an design issue. Nowadays, most ejb container do generate reasonably efficient database access logic for CMPs, but still it may be less efficient than those writen by a JDBC guru....

And also CMP provides a layer of caching, therefore, for those frequently accessed/quried/updated objects, CMP may be a good choice. on the other hand, BMP or SLSB+DAO may be just enough for one time data insertion, also equiped with CMT.
 
Joseph A Alexander
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To substantiate my earlier posting,

DAO (Data Access Object) is just a layer/abstraction where all the logic & code related to the database can go.

Inside DAO, You can choose to write your own data access code (POJO using JDBC, Hibernate classes, BMP) OR You can use BMP Entity beans where we will be using the Container services.

So in the second case, having a DAO doesn't mean that you are writing your database access code. You are just providing an abstraction on the DB layer and internally you are using CMP beans.

Advantages of using DAO layer is, you can change it to any database access tool (i.e, any Java Persistence APIs) in the future without modifying your business layer (Session EJBS/Other Business Services).
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Joe, we have a different understanding of DAO.

Originally posted by Joseph Alexander:

DAO (Data Access Object) is just a layer/abstraction where all the logic & code related to the database can go.


With this definition, then all of us should agree that we will use DAO.

I would rather define DAO as a layer that the developers abstract/write all the logic & code related to the database. The container does the abstraction for CMPs.

And then we have the choice of whether to use CMP or SLSB + DAO or SLSB+BMP+DAO and we have the debate...
 
Sub swamy
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add on to this discussion - what is the impact of using a stateful session bean with a DAO ? Why is it a bad practice - or is it just that stateless session + DAO is better than stateful session + DAO?
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Subramanian Narayanaswamy:
Just to add on to this discussion - what is the impact of using a stateful session bean with a DAO ? Why is it a bad practice - or is it just that stateless session + DAO is better than stateful session + DAO?


IMHO, It does not matter to the DAO, if your design mandates you to use a SFSB and that SFSB needs to do some db stuff, just go ahead with SFSB+DAO.

But since DAOs are just POJOs, you prabably want to have a SLSB as a gatewate to all the DAOs and also have a single point where transaction attribute can be configured and controled......

so it becomes SFSB->SLSB->DAO
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic