This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm very new to EJB3 and I'm simply trying to get a Stateful session bean to work. I obviously do not understand what stateful means because I can't get a SFSB to keep values in-state. All of the examples I've looked at (just finished reading a good JEE 5 book) are very straightforward and the concept is simple.
Here's my example.
I have a SFSB called "SessionBeanTest" with an interface "SessionTest:
I have a testservlet, aptly named "Test", where I call the SFSB:
I would expect, each time I refresh the page/servlet, that three new string values get added to the Stateful List field ("names") in the SFSB. Instead, the List is cleared on each refresh and the same three values are added:
If I had refreshed the screen twice, I would have *expected* to see something like this:
You'll notice I sprinkled the SFSB w/ output messages...here's what I'd see if I refreshed the servlet twice:
I'm using JBoss 4.0.4RC1, which is their latest release of the AS and EJB3.
I'm confused...what have I done wrong? How do I make my stateful bean stateful!?
Please see this recent post about basically the same subject. You are not using the SFSB properly... in order to reestablish the same session with your SFSB you need to store the ejb handle somewhere between requests so that you can pick it back up again. The simpliest solution for this is to put it in the user's HttpSession.
Joined: Apr 26, 2004
Yeah, I realized that after going back and doing some reading on the JBoss TrailBlazer sample app. I guess I hadn't thought about how a multi-threaded client would be able to keep track of the bean.
I'm now trying to figure out the value of using a Stateful vs. a Stateless bean since both can behave, on a general level, exactly the same while being stored in HttpSession. I realize there's a different API behind Stateful beans but I would think, in most cases, all of that overhead just wouldn't be worth it?
Joined: Jul 18, 2001
A Stateless Session Bean will definitely not behave the same as a SFSB. Even if you store the handle to the Stateless Bean in your HttpSession you are not guaranteed to get the same instance from the server.
Joined: Apr 26, 2004
Originally posted by Chris Mathews: A Stateless Session Bean will definitely not behave the same as a SFSB. Even if you store the handle to the Stateless Bean in your HttpSession you are not guaranteed to get the same instance from the server.
Really!?? Ok, that's very good to know...thank you. I guess I assumed if I stored the stub to a SLSB in HttpSession...that's it...I got it...and I would get the same basic functionality as a SFSB.
Internally, how does the AS ensure that I'm getting the same SFSB instance?
Is EJB 3.0 essentially EJB 2.0 with annotations? Before this EJB 3 adventure of mine, I had only built servlet/jsp/hibernate apps w/ Java...so this is all pretty new to me.
This is the task of EJB container. for SFSB, it the client's handle to the EJB is still valid, the container will not put the EJBean back to the pool. Thats why we need to store the handle to session. Where is for SLSB, for two different EJB business methods, the container is free to use any bean instance from the pool, and hence is not guarranteed that It will use the same bean again.