4 Replies Latest reply on Feb 14, 2014 6:23 AM by peterlustig

    Stateless Bean with @PreDestroy-Annotation on WildFly 8 Final

    peterlustig

      Hello everyone,

       

      I have a Webapplication (WAR-Archive) within a EAR-Application an in this WebApp I have a Stateless Session Bean (SLSB) that connects to a database for example.

      This SLSB has a method annotated with @PreDestroy to close the connection cleanly when the EJB-Container wants to destroy the instance of the SLSB.

      I can give a simple example:

       

      @Stateless

      public class DatabaseConnectorBean {

         public void start() {

           //open connection to database and do some stuff...

         }

        

          @PreDestroy

          public void stop() {

          // close the connection before destruction

          }

      }

       

      The SLSB is injected and used in a RESTful-Webservice where I can call the start() and stop() methods. When I call the start() method and it returns then the stop() method (annotaded with @PreDestroy) is called immediately.

      When I remove the @PreDestroy-Annotation the stop() method wasn't called understandably.

      I don't know why the EJB-Container wants to destry the instance direct after creation?

      When I annotate the EJB with @Singleton the instance will not be destroyed after creation.

       

      Maybe someone can explain what is happening?!

       

      Many thanks in advance and regards,


      Peter