This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes Confusion - CMP vs Session EJB > DAO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "Confusion - CMP vs Session EJB > DAO" Watch "Confusion - CMP vs Session EJB > DAO" New topic
Author

Confusion - CMP vs Session EJB > DAO

Muhammad Asif
Ranch Hand

Joined: Jul 13, 2001
Posts: 202
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,


SCJP, SCBCD, SCWCD 1.4, IBM-OOAD, SCEA, TCP, TCSS, TCIS, ATCP
Joseph A Alexander
Greenhorn

Joined: Oct 31, 2003
Posts: 23
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.


Regards,<br />Joe, SCEA
Muhammad Asif
Ranch Hand

Joined: Jul 13, 2001
Posts: 202
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
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.


Meng Yi
Joseph A Alexander
Greenhorn

Joined: Oct 31, 2003
Posts: 23
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

Joined: May 07, 2003
Posts: 270
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

Joined: Oct 02, 2002
Posts: 121
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

Joined: May 07, 2003
Posts: 270
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion - CMP vs Session EJB > DAO
 
Similar Threads
Add and test Custom Finder Methods in WSAD 4.0
J2EE Applicability
Passed Exam - 93%
Toplink
BMP + DAO or CMP ?????