glassfish
  1. glassfish
  2. GLASSFISH-21060

EntityManager injected twice into single stateless session bean method when deployment descriptor exists.

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 3.1.2.2, 4.0
    • Fix Version/s: None
    • Component/s: ejb_container
    • Labels:
      None
    • Environment:

      Windows XP, RHEL, Java 1.6.0_45, Postgres 9.x, EclipseLink 2.0

      Description

      I've inherited a fairly convoluted code base that is using separate databases for regular data and archived data (postgres 9.x). There are two connection pools and two datasources defined in GF.

      The app uses JPA 2.0 (EclipseLink) over Postgres as its persistence layer and a persistence.xml file contains two persistence contexts. A stateless session bean is used to manage interactions with JPA and the 'regular' database. A second SLSB manages interactions with the 'archive' database. The archive bean inherits from the regular bean.

      A single EntityManager field is declared by the parent as protected. The parent defines a setter with a @PersistenceContext annotation referencing the regular persistence unit. The child bean overrides the parents setter method and is annotated with @PersistenceContext referencing the archive persistence unit.

      When the above is deployed as a .war, an EntityManager for archive is injected ONCE into the child session bean. i.e. system behaves as expected.

      If a deployment descriptor declaring the session beans (ejb-jar.xml) is added to the mix then the child session bean will have TWO distinct EntityManagers injected, archive followed by regular.

        Activity

        Hide
        Stephen Davies added a comment -

        @Stateless(name = "ParentBean")
        @LocalBean
        public class ParentBean {

        protected EntityManager em;

        @PersistenceContext(unitName = "regular")
        public void setEntityManager(EntityManager em)

        { this.em = em; }

        public void doIt() {}

        }

        Show
        Stephen Davies added a comment - @Stateless(name = "ParentBean") @LocalBean public class ParentBean { protected EntityManager em; @PersistenceContext(unitName = "regular") public void setEntityManager(EntityManager em) { this.em = em; } public void doIt() {} }
        Hide
        Stephen Davies added a comment -

        @Stateless(name = "ChildBean")
        @LocalBean
        public class ChildBean extends ParentBean {

        @PersistenceContext(unitName = "archive")
        @Override
        public void setEntityManager(EntityManager em)

        { ServerSession session = ((EntityManagerFactoryImpl)em.getEntityManagerFactory()).getServerSession(); System.out.println("### Session name ###"); System.out.println(session.getName()); this.em = em; }

        }

        Show
        Stephen Davies added a comment - @Stateless(name = "ChildBean") @LocalBean public class ChildBean extends ParentBean { @PersistenceContext(unitName = "archive") @Override public void setEntityManager(EntityManager em) { ServerSession session = ((EntityManagerFactoryImpl)em.getEntityManagerFactory()).getServerSession(); System.out.println("### Session name ###"); System.out.println(session.getName()); this.em = em; } }
        Hide
        Stephen Davies added a comment -

        @Singleton
        @LocalBean
        @Startup
        public class Launch {

        @EJB
        private ChildBean cb;

        @PostConstruct
        public void init()

        { cb.doIt(); }

        }

        Show
        Stephen Davies added a comment - @Singleton @LocalBean @Startup public class Launch { @EJB private ChildBean cb; @PostConstruct public void init() { cb.doIt(); } }
        Hide
        Stephen Davies added a comment -

        <?xml version="1.0" encoding="UTF-8"?>
        <ejb-jar
        xmlns = "http://java.sun.com/xml/ns/javaee"
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
        version = "3.1">

        <enterprise-beans>

        <session>
        <ejb-name>ParentBean</ejb-name>
        <ejb-class>jpa.test.ParentBean</ejb-class>
        <session-type>Stateless</session-type>
        </session>

        <session>
        <ejb-name>ChildBean</ejb-name>
        <ejb-class>jpa.test.ChildBean</ejb-class>
        <session-type>Stateless</session-type>
        </session>

        </enterprise-beans>

        </ejb-jar>

        Show
        Stephen Davies added a comment - <?xml version="1.0" encoding="UTF-8"?> <ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd " version = "3.1"> <enterprise-beans> <session> <ejb-name>ParentBean</ejb-name> <ejb-class>jpa.test.ParentBean</ejb-class> <session-type>Stateless</session-type> </session> <session> <ejb-name>ChildBean</ejb-name> <ejb-class>jpa.test.ChildBean</ejb-class> <session-type>Stateless</session-type> </session> </enterprise-beans> </ejb-jar>

          People

          • Assignee:
            Srini
            Reporter:
            Stephen Davies
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated: