aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes bean calling a method from DAO or another bean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "bean calling a method from DAO or another bean?" Watch "bean calling a method from DAO or another bean?" New topic
Author

bean calling a method from DAO or another bean?

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Hello All,

I have a MDB which calls the methods that updates or inserts rows to the DB table.
My question is: What is the right way of the method call?
1. Calling method A() directly from DAO
2. Calling method A() from another sessionbean which in turn calls method of DAO?

are there any advantages ,disadvantages in above two approaches?

Thanks in advance,

Trupti
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

Trupti,
Option 2 makes the code more reusable since you can call it from other (non-mdb) clients as well. Either option provides transaction and security support though.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Option 1 is better. Why do we need a extra Session bean when a call to DAO will do. Jeanne, Could you please explain why 2 is more reusable. Isn't it a simple call. thanks.
Or I have not understood the question.
[ April 13, 2005: Message edited by: Pradeep Bhat ]

Groovy
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

Originally posted by Pradeep Bhat:
Option 1 is better. Why do we need a extra Session bean when a call to DAO will do.]

Pradeep,
Sometimes a level of indirection is good. There's a quote about any problem being solveable by adding another layer of indirection. (Obviously, this isn't always the case, but its a good quote.) So the session bean isn't inherently good or bad.

Jeanne, Could you please explain why 2 is more reusable. Isn't it a simple call.

Right now the DAO is only called from the MDB. What happens if someone decides that it should be called from a servlet? In that case, it would be good to have a session facade in place to provide the benefits of EJB.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Hi Pradeep,

Jeanne's explanation was clear. I've a way that could, perhaps, help you understand the reasoning, easier.

Just ask yourself why would you put all the CRUD methods inside a DAO rather than having them as private methods inside the MDB? Then you apply this same reasoning to why you should be using a facade instead of invoking the DAO's method directly inside a MDB.

In short, its all about reuse.



SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Right now the DAO is only called from the MDB. What happens if someone decides that it should be called from a servlet? In that case, it would be good to have a session facade in place to provide the benefits of EJB.


Yeah Transaction in particualar. I havent worked on EJB's for more that 2.5 years.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bean calling a method from DAO or another bean?