Search code examples
cdilifecycle

Call @RequestScoped Bean from @PreDestroy method


I have application logic to write a protocol file in a @RequestScoped bean. I would like to write the protocol just before a @SessionScoped bean expires.

@SessionScoped
class Anybean implements Serializable {

@Inject
private ProtocolBean protocolBean;

@PreDestroy
private void writeFinalProtocol() {
    protocolBean.writeProtocol();

}

}

I get the error: WELD-000019 Error destroying an instance Managed Bean. Is it generally disallowed to call other beans from @PreDestroy?


Solution

  • OK I found out: As soon as I change the ProtocolBean to @SessionScoped or @ApplicationScoped everything works fine. It seems as if you cannot instantiate a RequestScoped bean wihtin a @PreDestroy method and call a method on it.

    The underlaying exception is: com.sun.jdi.InvocationException occurred invoking method