1

I'm doing a Web application using Spring 3.1.0.RELEASE, JSF 2.x, JPA 2 with Hibernate Provider. I use PrettyFaces 3.3.2 for friendly URL. The application run on Tomcat 6.35 .

I wanted to use the Jsf ViewScope so I decided to follow the implementation found on the web : http://comdynamics.net/blog/109/spring3-jsf2-view-scope/

public class ViewScope implements Scope {

    private static final Logger logger = LoggerFactory.getLogger(ViewScope.class);

    @Override
    public Object get(String name, ObjectFactory objectFactory) {
        final Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();

        Object instance = viewMap.get(name);
        if (instance == null) {
            instance = objectFactory.getObject();
            viewMap.put(name, instance);
        }
        return instance;
    }

    @Override
    public Object remove(String name) {
        logger.debug("ViewScope::remove {}", name);
        return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
    }

    @Override
    public String getConversationId() {
        return null;
    }

    @Override
    public void registerDestructionCallback(String name, Runnable callback) {
        //Not supported
    }

    @Override
    public Object resolveContextualObject(String key) {
        return null;
    }
}

I notice that @PreDestroy are not called on them like show this question @PreDestroy never called on @ViewScoped.

Does it mean that the Managed beans with ViewScope are never destruct ? Which conduct to memory leak. Should we use this scope so?

It's only happen with custom Viewscope on Spring or also on Mojarra ?

Thanks.

Community
  • 1
  • 1
La Chamelle
  • 2,927
  • 4
  • 36
  • 54
  • On mojarra the @PreDestroy is called, at least it does in my propyect. I think you will save lot of troubles if you use pure JSF 2 backing beans. – IturPablo Jun 05 '12 at 07:16
  • @IturPablo Do you use Spring in your project ? – La Chamelle Jun 05 '12 at 08:04
  • Yess!!!, I you could use the JSF tags and to access the service beans provided use @ManageProperty("#{}") tag. In my opinion is better use pure JSF beans you avoid some troubles and you are sure about the jsf version you are using. – IturPablo Jun 05 '12 at 09:03

2 Answers2

1

Problem is incorrect implementaiton of view scope. It is creates Spring bean objectFactory.getObject(); but never destroy it.

To solve it - check correct implementation with support for registerDestructionCallback.

BWT, current Mojjara implementation will not call @PreDestory on your bean too. But it will free bean instance at least.

Community
  • 1
  • 1
Michail Nikolaev
  • 3,733
  • 22
  • 18
0

I tried the work around for Jsf view scope bean memory leaks using spring custom view scope. It works for both Jsf 2.1 & 2.2.Try the code in below link. Memory leak with ViewScoped bean?

Community
  • 1
  • 1
Sathish Kumar
  • 51
  • 1
  • 2