0

i am currently faced with strange problems with @PreDestroy methods in Mojarra 2.2.14: The behaviour seems to be some kind of undefined. sometimes it is called one time directly after @PostConstruct, sometimes it is called twice when triggering the GET again. same when leaving current page: sometimes clear() is called, but most times not. @PostConstruct on the the other hand works as expected.

the result ist, that all fields of ProductDetailBean are null, wenn calling page "productDetails.xhtml".

are there some known issues about that @PreDestroy? Would be very nice, if someown could give some helpful info about this. what would be suitable workarounds? this bean has a lot of fields and i have a strong interest to clear everything up when user leaves page.

Sadly i can not change to CDI-Beans for now...

@ManagedBean(name="productDetail")  //javax.faces.bean.ManagedBean
@ViewScoped                        //javax.faces.bean.ViewScoped
public class ProductDetailBean implements Serializable {

      private String field;

      @PostConstruct
      public void init(){
        System.out.println("init Behaviour is ok, callend only once!");
        this.field = "blah";
      }


      @PreDestroy
      public void clear(){
        System.out.println("called one or multiple times after init() ");
        this.field = null;
      }

   public void setField(String field) {
     this.field = field;
   }

   public String getField() {
       return this.field;
   }

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Steve
  • 384
  • 1
  • 7
  • 17
  • For the plain jsf/cdi viewscoped annotation, you've go to know that is it called when the jvm needs memory, there is no guarantee that it is called immediately (for jsf 2.2 it is called on session expire, for jsf 2.0/2.1 it is not. If you need to have it called 'immediately', you need to look at the Omnifaces viewscoped... http://showcase.omnifaces.org/cdi/ViewScoped – Kukeltje Jun 19 '17 at 12:41
  • the omnifaces ViewScope is a CDI-Scope and i can't switch to CDI at the moment. i thought the PreDestroy should invoke immediately https://stackoverflow.com/questions/6368840/predestroy-never-called-on-viewscoped. the problem here is, that it PreDestroy is invoked multiple times directly after PostConstruct. – Steve Jun 19 '17 at 13:23
  • btw: where have all the links gone now: ? http://www.oracle.com/splash/java.net/maintenance/index.html – Steve Jun 19 '17 at 13:31
  • How is your last comment related to this question? Try using google... – Kukeltje Jun 19 '17 at 13:37

0 Answers0