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!
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.
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:
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).
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
Joined: Feb 18, 2005
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 ]