11 Replies Latest reply on Mar 8, 2010 10:18 PM by nickarls

    Don't disposes instances from programmatic lookup ?

    wildes

      hi guys, i've been observed that instances obtained from programmatic lookup don't have seem disposes . is correct behaviour ?

        • 1. Re: Don't disposes instances from programmatic lookup ?
          meetoblivion

          what scope are your beans?

          • 2. Re: Don't disposes instances from programmatic lookup ?
            nickarls

            The lifecycle is the same no matter how it's obtained, it comes from a context and goes down with a context (regardless of scope). Could you show the entire bean code?

            • 3. Re: Don't disposes instances from programmatic lookup ?
              wildes

              Yes, it is a simple test, one requestScoped backingbean that require a connection from an actionEvent.
              ...

              @Named
              @RequestScoped
              public class SampleController {
                  private Connection connection = null;
                  @Inject
                  private Instance<Connection> connectionProducer = null;
                  public void doAnything(){
                      connection = connectionProducer.get();
                  }
              }
              ...
              @Named
              @ApplicationScoped
              public class ConnectionProducer implements Serializable {
                  ...
                  @Produces
                  public Connection getConnection() throws SQLException {
                      return dataSource.getConnection();
                  }
              }

              • 4. Re: Don't disposes instances from programmatic lookup ?
                nickarls

                And you have a @Disposes annotation parameter in a method in ConnectionProducer that isn't called? And you are sure you have the CDI scope annotations and not the JSF ones?

                • 5. Re: Don't disposes instances from programmatic lookup ?
                  wildes

                  yes, i sure. And when i change the injection strategy to inject annotation on a connection field, i can see the disposes.


                  //imports on backing bean
                  import javax.enterprise.context.RequestScoped;
                  import javax.enterprise.inject.Any;
                  import javax.enterprise.inject.Instance;
                  import javax.inject.Inject;
                  import javax.inject.Named;
                  ...
                  // Disposer method on ConnectionProducer
                      public void closeConnection(@Disposes Connection connection) throws SQLException {
                          connection.close();
                      }
                  


                  • 6. Re: Don't disposes instances from programmatic lookup ?
                    nickarls

                    no method invocation = no instance = no disposal?

                    • 7. Re: Don't disposes instances from programmatic lookup ?
                      wildes

                      The method disposer is not called if an instance is obtained from a programmatic lookup. Do anyone can reproduce this scenario ?

                      • 8. Re: Don't disposes instances from programmatic lookup ?
                        nickarls
                        public class Data {
                        
                             public void ping() {
                             };
                        }
                        



                        public class DataContainer {
                             
                             @Inject @Important
                             Instance<Data> data;
                             
                             public void ping() {
                                  data.get().ping();
                             }
                        
                        }
                        



                        public class Producer {
                             
                             public static boolean disposed = false;
                             
                             @Produces @RequestScoped @Important Data produceData() {
                                  return new Data();
                             }
                             
                             public void disposeData(@Disposes @Important Data data) {
                                  Producer.disposed = true;
                             }
                        }



                             @Test
                             public void works() {
                                  DataContainer dataContainer = getReference(DataContainer.class);
                                  dataContainer.ping();
                                  getCurrentManager().getServices().get(ContextLifecycle.class).getRequestContext().destroy();
                                  assert Producer.disposed;
                             }
                        



                        works for me

                        • 9. Re: Don't disposes instances from programmatic lookup ?
                          nickarls

                          Tried @Produces @ApplicationScoped?

                          • 10. Re: Don't disposes instances from programmatic lookup ?
                            wildes

                            I observed now that my producer method is bound to dependent scope. If i change to requestScoped its works. Strange that the disposal works when direct inject on field, and programmatically not. The weld-reference doc says about dependent scope that It is strictly a dependent object of some other object. It is instantiated when the object it belongs to is created, and destroyed when the object it belongs to is destroyed. What is the correct disposal behaviour from producers bound to dependent scope ? Can you try again with producer method bound to dependent scoped ?



                            Thanks.
                                 

                            • 11. Re: Don't disposes instances from programmatic lookup ?
                              nickarls

                              I came up with https://jira.jboss.org/jira/browse/WELD-466


                              It appears as the disposer is resolved fine but it is never called when the parent bean is destroyed since the Instance<Foo> is not correctly registered as a dependent to the parent bean, it appears.


                              Which matches both our observations.