3

After I commit some data into the database I want my session beans to automatically refresh themselves to reflect the recently committed data. How do I achieve this when using managed session beans in JSF 2.0?

Currently I have to restart the web server in order for the sessions to clear and load anew again.

D. Bermudez
  • 217
  • 2
  • 9
  • 20

1 Answers1

6

2 ways:

  1. Put them in the view scope instead. Storing view-specific data sessionwide is a waste. If you have a performance concern, you should concentrate on implementing connection pooling, DB-level pagination and/or caching in the persistence layer (JPA2 for example supports second level caching).

    @ManagedBean
    @ViewScoped
    public class FooBean {
        // ...
    }
    

  2. Add a public load() method so that it can be invoked from the action method (if necessary, from another bean).

    @ManagedBean
    @SessionScoped
    public class FooBean {
    
        private List<Foo> foos;
    
        @EJB
        private FooService fooService;
    
        @PostConstruct
        public void load() {
            foos = fooService.list();
        }
    
        // ...
    }
    

    which can be invoked in action method inside the same bean (if you submit the form to the very same managed bean of course):

        public void submit() {
            fooService.save(foos);
            load();
        }
    

    or from an action method in another bean (for the case that your managed bean design is a bit off from usual):

        @ManagedProperty("#{fooBean}")
        private FooBean fooBean;
    
        public void submit() {
            fooService.save(foos);
            fooBean.load();
        }
    

    This of course only affects the current session. If you'd like to affect other sessions as well, you should really consider putting them in the view scope instead, as suggested in the 1st way.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Actually this solution would work, but I found out that when you are using the same bean for 3 different JSF pages, the values of the bean are lost between pages. I need to retain those values. – D. Bermudez Oct 13 '11 at 14:18
  • Make the bean view scoped then as suggested in 1st way. It clearly shouldn't be a session scoped bean at all. See also: [Managed bean scopes](http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#ManagedBeanScopes) – BalusC Oct 13 '11 at 14:21
  • I get it...all my managed beans should be ViewScoped if I want them to retain values in session and use them in other pages. – D. Bermudez Oct 13 '11 at 14:36
  • No. They should be view scoped if you want to use one and the same bean per view. This way every browser tab/window will have its own bean instance. They are NOT shared among each other throughout the session. For that you would need a session scoped bean. – BalusC Oct 13 '11 at 14:38
  • Ahh ok. Thank you Balus C...I think now I'm getting the hang of the different types of scopes. – D. Bermudez Oct 13 '11 at 15:03
  • @BalusC you are the man! Why are you so great in JSF? Everywhere I go I find your name... – stuckedunderflow Jan 23 '15 at 09:50