aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Why use Session Beans? 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 "Why use Session Beans?" Watch "Why use Session Beans?" New topic
Author

Why use Session Beans?

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
I've been thinking of this over and over again. Why do I have to use EJB session beans? What's the benefit? What's the drawback?

For example, I have a shopping cart... The user adds items to the shopping cart. But you can do this with regular POJO's and then add that as an attribute to an HttpSession, right? So if I use a Stateful session bean in place of the first method I mentioned, what would be the potential benefits and drawbacks? Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30309
    
150

Timothy,
You don't "have to" use a session bean. The main advantages are the container providing services for you like security and transaction support. A disadvantage is that you need an application server.


[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
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Yeah, much of the EJB backlash has to do with people using them when they're not needed.

Why might you need a stateless session bean? Standard EJB reasons apply - simplified remote access, methold level security, integration with an authentication mechanism, assistance with interacting with transaction management, inherent multithreaded access. These are are extremelly compelling reasons to use an EJB if you need them. If you don't need any of these features, well, why not just use a POJO?

I wrote this article about when to use EJBs a few years ago, but I think it's wisdom still applies:

When to use EJBs?


-Cameron McKenzie
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
I decided to add to this thread as I was wondering a similar thing
See I am in the process of writing a system which (again) contains a shopping basket and stuff. I'm looking at EJB3.0 mainly because I've used EJB2.1 before and am familiar with the structure (although 3.0 has changed alot to 2.1... for the better)

But I got to thinking, do I need Session beans? The shopping cart isn't such a bad thing because of the actual transaction when the user is ready to hit the 'BUY' button.

But i also have a Stateful Session bean that holds the users details (name, email, address, telno, etc). Now my original system used a standard JavaBean held in the HttpSession, I then changed it to use a Stateful Session Bean (collects the information on login).
The thing is, I've designed my system with seperate Presentation(JSP, Servlets) and Business(EJB-3.0) layers. Now everytime I call say .getName() or .getEmail() i will be making TWO remote calls accross the network from one server to the other and back, this will obviously become a very heavy load if I have say 1,000 on at the same time!!!

I then thought "Hmmm maybe I'll make one call, send the data from the Busness end (via DB query) to the Presentation server and store it in a standard Bean". Then I realised that this would defeat the point of using Session Beans!

So basically I'm guessing it's not recommended to use REMOTE Session Beans for somethign like UserDetailsBean (name, address, etc).
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
In most cases, it makes sense for the JSPs, servlets and EJBs to be in a single application. This means that you will build one EAR file for the application and then deploy that file onto your EJB server. (Most of these servers also have a built-in servlet container to handle the JSPs and servlets.)

It also means that the EJBs need only expose local interfaces to the clients, thus improving performance (compared to remote interfaces).


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
My plan was to try and spread the load between JSP/Servlet stuff and EJB. Maybe it's not worth it, but if a person is just spending time browsing the site running searches look at details of items then there's no need to use the Business Server, that would only come into operation if the user logged in, created a cart (click ADD) and completed a transaction.

Presentation Server - JSP/Servlets
- goto site
- browser by keyword
- browse by category
- view details

Business Server - EJB 3.0
- run login validation
- get user details
- create/add to cart
- complete transation

But then as I say, if a user logs in the they would have a Session Bean created by the Business Server, which my understanding would mean each time I ran a .getName() .getEmail() etc the Presentation Server would have to make a call (2 calls to and from) the Business Server (getName followed by retuned name)

Unless in the instance of the 'UserDetailsBean' I created a standard JavaBean and held it in the HttpSession then the Presentation Server would be making calls to it's self for getName() etc. and used the Business Server for just 'CartBean' (Session) etc...
[ December 11, 2007: Message edited by: Keith Seller ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why use Session Beans?