Results 1 to 4 of 4
Thread: Stateful bean not keeping state?
- 04-29-2011, 08:06 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
Stateful bean not keeping state?
Hi. I am fairly new to EJB's and as an example initial project I was wanting to create a car builder. At the moment, its very simple as I have an enterprise project, with two sub-projects being the client and ejb module. In my EJB module I have two classes which is one for the business interface, and the other for my EJB. I have declared the EJB @Stateful and it has several methods such as addComponent(String sComponent), removeComponent( etc...
For the client I have a JSF simple form with a text box and a button to add a component to the list and refresh the page. This all works fine, except when I run the project it adds the component, but each time the page has been refreshed the EJB re-initializes and loses state while only ever displaying the last item i'm adding. Have I not quite got the hang of how EJB's work? Are they just solely for keeping state while the page is not affected, and are best suited for one time calls? I can use a local managed bean to store the components between page refreshes, but am just wondering if there is any way to store data remotely using EJB's. It's fine if thats not how they work, just interested :)
Thanks in advance.
- 04-30-2011, 03:12 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
How are you accessing the Bean? Does your client maintain a reference to the bean you looked up, and use this reference for future calls? If you maintain a reference (for example via a session attribute), the Bean should maintain its state within the session.
- 04-30-2011, 10:47 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
I think that must be where im going wrong then. At the moment I have the following:
I guess what I should really be using is context.lookup then. I'll have to give it a try and keep a reference to the bean that way.Java Code:@ManagedBean @RequestScoped public class CarShop { @EJB private CarBuilder oBuilder; public String getComponents() { String sOutput = ""; List<String> oComponents = oBuilder.getComponents(); for (String sComponent : oComponents) { sOutput += sComponent + System.getProperty("line.separator"); } return sOutput; } //Other method including add remove from EJB }
- 05-01-2011, 08:03 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
[SOLVED] Invoking a Stateful Session Bean as Stateless
By JThangiah in forum Enterprise JavaBeans (EJB)Replies: 3Last Post: 07-17-2008, 09:35 AM -
Stateful Session Bean
By Java Tip in forum Java TipReplies: 0Last Post: 11-29-2007, 12:39 PM -
Calling a Stateful Session bean in NetBeans
By JavaForums in forum NetBeansReplies: 0Last Post: 07-31-2007, 09:21 AM -
Introduction to Stateful Session Bean In NetBeans
By JavaForums in forum NetBeansReplies: 0Last Post: 07-31-2007, 09:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks