Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-1076

Deal with non-serializable dependent instances of a passivation capable bean

XMLWordPrintable

      Since https://github.com/jboss/cdi/pull/47 it is legal for a normal-scoped component to have a non-passivation capable dependency, e.g:

      @SessionScoped
      public class Foo implements Serializable {
      
         @Inject
         public Foo(Bar bar) {
         }
      }
      
      public class Bar {
      }
      

      Although the Bar instance reference is not retained by the Foo instance, the Bar instance is still technically a dependent instance of Foo and therefore the container needs to keep a reference to it in order to eventually destroy it properly.

      This is a problem because dependent instances of a bean are currently supposed to be serialized together with the normal-scoped instance but the Bar instance is not serializable.

      We may work around by making use of the following part of the spec:

      Finally, the container is permitted to destroy any @Dependent scoped contextual instance at any time if the instance is no
      longer referenced by the application (excluding weak, soft and phantom references)."

      and destroy every non-serializable dependent bean instance of a normal-scoped bean when the creational context for the normal-scoped bean instance is serialized.

      This is not 100% equivalent to "the instance is no longer referenced by the application" however covers all the sensible cases including:

      • Foo no longer holds a reference to Bar, therefore we can safely destroy Bar
      • Foo holds a reference to Bar - does not matter if we destroy Bar prematurely or not since serialization of Bar is going to fail anyway
      • Foo holds a reference to Bar in a transient field and is therefore either going to "forget" the reference no serialization (therefore we can destroy it) or is able to recreate the Bar instance itself on deserialization

            rhn-engineering-jharting Jozef Hartinger
            rhn-engineering-jharting Jozef Hartinger
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: