Meaningless Drivel is fun!*
The moose likes EJB and other Java EE Technologies and the fly likes DAO and EJB etc.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "DAO and EJB etc.." Watch "DAO and EJB etc.." New topic
Author

DAO and EJB etc..

Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
If I use a DAO with my EJB do I have to include that DAO class and package in my EJB Jar File that is deployed to the remote server?

In this case then the DAO class, and the EJB Class files also must reside on my Web App Server(a different physical machine)?

Thanks.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Stu,
The caller of the DAO needs the class file available. It is common to have the remote client call a session bean to insulate it from change. The session bean can call the DAO which calls the EJB. You don't want to keep redistributing the DAO everytime something changes.


[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
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Why would you think that the EJB and DAO would require also being on the Web Server.

You should look at the chain of calls as each call is calling down to a layer below it. And one layer should never call a layer above it.

So with a Web Server to call an EJB, the Web Server will need the EJB interfaces but that is all. The Web Server will lookup the EJB from the EJB Container and get back a proxy object. The EJB in the EJB container calls the DAO, so the DAO will only ever reside in the EJB Container. No other layer above should be calling the DAO directly.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Hi,

Thanks both Jeanne and Mark for your replies.

Why would you think that the EJB and DAO would require also being on the Web Server.


I'm brand new at developing with EJB and distributed systems. Basically I have little knowledge regarding the packaging requirements, project structure, and deploying of Enterprise Applications, but I am learning by hand coding deployment descriptors, and building with ANT, no Deploy Tool and I like doing it this way!

Specifically, while I was making the EJB more useful by adding some Arguments and Params to the methods, I recompiled the EJB classes and deployed the EJB jar without redeploying the Web App, then I received a Marshalling Exception. I fixed it by redeploying the web app which also contained all of the EJB Classes and not just the Interfaces. My Web Application contained all of the EJB files because I build the EJB files from within a directory in the Web application project and then deploy the EJB jar separately to the EJB server. This led me to believe that I needed the EJB Class and Interfaces in both Web App and EJB Jar. Hope that makes sense the way I explained it.

Now I know all that is required in the Web Application are the Interfaces, and I completely understand what you are saying about the DAO. Thanks for your help in clarifying this.

Stu
[ April 06, 2007: Message edited by: Stu Higgs ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Yes, I understand your plight very well. The issue that you see is related to what we call ClassLoaders. And without explaining ClassLoaders right now, it is a heavy topic.

What you need to do in your WebApp is make sure you are doing a lookup and not trying to create EJB classes directly. OK but the real thing is the packaging. What you will learn in a bit is to package your application into an .ear file that will contain both your .war and your .jar file.

What an ear does is save from having to do a remote lookup of your EJBs. If both the jar and the war are in an ear, the App Server will make sure the classes are all loaded by the same ClassLoader and you don't get a Marshalling Exception.

Does that help.

Mark
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Mark,

Thanks for your reply. It does help and also raises another question or two or three...

Background:
My EJB.jar is currently deployed to a separate Managed Server named EjbServer and the Web Application is on another managed server named WebAppServer. Currently the two managed servers are on the same physical machine but I imagine in the real world they may be on separate machines(?), where each one will be registered with an Administration server as a managed server?

Right now I am looking up my EJB like this from Servlet:

//move to ServiceLocator
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://192.168.1.3:8002");
Context ctx = new InitialContext(ht);
Object h = ctx.lookup("HelloWorldEJB");
home = (IHelloWorldHome)
PortableRemoteObject.narrow(h, IHelloWorldHome.class);

My question is if my EJB's are on a separate physical machine from the Web Application how can I deploy both Web App Module .war and the EJB Module in a single .ear file? Is there some deployment descriptor in the EAR file that specifies which managed server will host the EJB Module and which managed server will host the Web App Module? Does the EAR file get deployed to or by the Adminsitration Server? Very confusing right now! Thanks for your help.

Stu
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

In your case where they are really two seperate server, meaning different JVMs running. In that case, your web application will do remote lookups on the EJBS, so the war file that is deployed on the seperate server will only need the Remote interface, based on EJB3. In EJB 2.x I think you also need the Home objects, but it has been a while since I used EJB 2.x that I can't remember, nor do I really want to remember pre EJB3 times.

Mark
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Mark,

Thanks again for clearing things up about the interfaces and look ups. Much appreciated.

Stu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DAO and EJB etc..