Uploaded image for project: 'Quarkus'
  1. Quarkus
  2. QUARKUS-661

[GSS](1.7.z) @TransactionScoped Context does not call @Predestroy on TransactionScoped Beans

XMLWordPrintable

    • False
    • False
    • +
    • Undefined
    • Hide

      CDI transactional observer methods works fine. So, the following "@Observes @Destroyed(TransactionScoped.class)" method can be used as a possible workaround:

      import javax.enterprise.context.Destroyed;
      import javax.enterprise.context.Initialized;
      import javax.enterprise.event.Observes;
      import javax.transaction.Transaction;
      import javax.transaction.TransactionScoped;
      import org.jboss.logging.Logger;
      
      public class TransactionScopedObserverBean {
          private static final Logger log = Logger.getLogger(TransactionScopedObserverBean.class);
      
          void init(@Observes @Initialized(TransactionScoped.class) final Object event) throws Exception {
              // do some initilize tasks after TransactionScoped bean has been initialized
          }   
      
          void destroy(@Observes @Destroyed(TransactionScoped.class) final Object event) throws Exception {
              // do some cleanup tasks after TransactionScoped bean has been destroyed (= after commit or rollback on JTA transaction)
          }   
      }
      
      Show
      CDI transactional observer methods works fine. So, the following "@Observes @Destroyed(TransactionScoped.class)" method can be used as a possible workaround: import javax.enterprise.context.Destroyed; import javax.enterprise.context.Initialized; import javax.enterprise.event.Observes; import javax.transaction.Transaction; import javax.transaction.TransactionScoped; import org.jboss.logging.Logger; public class TransactionScopedObserverBean { private static final Logger log = Logger.getLogger(TransactionScopedObserverBean.class); void init(@Observes @Initialized(TransactionScoped.class) final Object event) throws Exception { // do some initilize tasks after TransactionScoped bean has been initialized } void destroy(@Observes @Destroyed(TransactionScoped.class) final Object event) throws Exception { // do some cleanup tasks after TransactionScoped bean has been destroyed (= after commit or rollback on JTA transaction) } }
    • Hide

      1. Extract the attached reproducer.zip
      2. Start Quarkus dev mode

      $ cd reproducer
      $ ./mvnw clean compile quarkus:dev
      

      3. Send a request

      $ curl -v http://localhost:8080/hello
      

      Then, you will see the following log messages that indicate @PreDestroy destroy method is not invoked:

      INFO  [com.exa.qui.GreetingService] (executor-thread-1) GreetingService initialized.
      INFO  [com.exa.qui.TransactionScopedObserverBean] (executor-thread-1) @Observes TransactionalScopeBean initialized. event = TransactionImple < ac, BasicAction: 0:ffffc0a80a03:ae2b:5fe4e0d0:1 status: ActionStatus.RUNNING >
      INFO  [com.exa.qui.ExampleTransactionalScopeBean] (executor-thread-1) ExampleTransactionalScopeBean initialized. bean value is 1608835295772
      INFO  [com.exa.qui.ExampleTransactionalScopeBean] (executor-thread-1) ExampleTransactionalScopeBean value = 1608835295772
      INFO  [com.exa.qui.TransactionScopedObserverBean] (executor-thread-1) @Observes TransactionalScopeBean destroyed. event = io.quarkus.narayana.jta.runtime.CDIDelegatingTransactionManager@3f951e8
      INFO  [com.exa.qui.GreetingService] (executor-thread-1) GreetingService destroyed.
      
      Show
      1. Extract the attached reproducer.zip 2. Start Quarkus dev mode $ cd reproducer $ ./mvnw clean compile quarkus:dev 3. Send a request $ curl -v http: //localhost:8080/hello Then, you will see the following log messages that indicate @PreDestroy destroy method is not invoked: INFO [com.exa.qui.GreetingService] (executor-thread-1) GreetingService initialized. INFO [com.exa.qui.TransactionScopedObserverBean] (executor-thread-1) @Observes TransactionalScopeBean initialized. event = TransactionImple < ac, BasicAction: 0:ffffc0a80a03:ae2b:5fe4e0d0:1 status: ActionStatus.RUNNING > INFO [com.exa.qui.ExampleTransactionalScopeBean] (executor-thread-1) ExampleTransactionalScopeBean initialized. bean value is 1608835295772 INFO [com.exa.qui.ExampleTransactionalScopeBean] (executor-thread-1) ExampleTransactionalScopeBean value = 1608835295772 INFO [com.exa.qui.TransactionScopedObserverBean] (executor-thread-1) @Observes TransactionalScopeBean destroyed. event = io.quarkus.narayana.jta.runtime.CDIDelegatingTransactionManager@3f951e8 INFO [com.exa.qui.GreetingService] (executor-thread-1) GreetingService destroyed.
    • ---

      As mentioned in the guide, quarkus-narayana-jta extension provides javax.transaction.TransactionScoped support.

      However, even if quarkus-narayana-jta extension is enabled, @PreDestory method is not invoked for @TransactionScoped bean at the end of the transaction (commit or rollback). (@PostConstruct method is invoked correctly, though.)

      import javax.annotation.PostConstruct;
      import javax.annotation.PreDestroy;
      import javax.transaction.TransactionScoped;
      import org.jboss.logging.Logger;
      
      @TransactionScoped
      public class ExampleTransactionalScopeBean implements ExampleTransactionalScope {
      
          private static final long serialVersionUID = 1L;
          private static final Logger log = Logger.getLogger(ExampleTransactionalScopeBean.class);
          private long value;
      
          @PostConstruct
          void init() {
              this.value = System.currentTimeMillis();
              log.infof("ExampleTransactionalScopeBean initialized. bean value is %s", this.value);
          }
      
          @PreDestroy
          void destroy() {
              log.infof("ExampleTransactionalScopeBean destroyed. bean value is %s", this.value);
          }
      
          public long getValue() {
              log.infof("ExampleTransactionalScopeBean value = %s", this.value);
              return value;
          }
      
      }
      

              rhn-engineering-mmusgrov Michael Musgrove
              rhn-support-mmiura Masafumi Miura
              Jose Carvajal Hilario Jose Carvajal Hilario
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: