Results 1 to 4 of 4
  1. #1
    tronied is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    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.

  3. #3
    tronied is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    I think that must be where im going wrong then. At the moment I have the following:

    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
    }
    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.

  4. #4
    tronied is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    I have got it working now. The problem was that I was using @RequestScoped meaning the bean only lasts as long as that call. I changed it to @SessionScoped so it keeps state now.
    Last edited by tronied; 05-01-2011 at 11:07 PM.

Similar Threads

  1. [SOLVED] Invoking a Stateful Session Bean as Stateless
    By JThangiah in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 07-17-2008, 09:35 AM
  2. Stateful Session Bean
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-29-2007, 12:39 PM
  3. Calling a Stateful Session bean in NetBeans
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-31-2007, 09:21 AM
  4. Introduction to Stateful Session Bean In NetBeans
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-31-2007, 09:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •