Search code examples
jakarta-eecode-injectionpersistence-unit

Inject entity manager into multiple beans of the same class


I have a session bean class that has some database operations. I need to use the class with multiple database so I config ejb-jar.xml to create session beans from this class, each bean for each database. Here is the code.

public class UserEM
{
    /** The entity manager */
    @Resource(name="userEntityManager/em")
    private EntityManager em;

    public EntityManager getEntityManager()
    {
        return this.em;
    }

    ... Database operations using the `em` ...
}

And here is the ejb-jar.xml

 ...
  <enterprise-beans>
    <session>
      <ejb-name>UserEM1</ejb-name>
      <ejb-class>com.abc.app.dao.UserEM</ejb-class>
      <session-type>Stateless</session-type>

      <persistence-context-ref>
        <persistence-context-ref-name>userEntityManager/em</persistence-context-ref-name>
        <persistence-unit-name>DataSource1</persistence-unit-name>
      </persistence-context-ref>
    </session>
    <session>
      <ejb-name>UserEM2</ejb-name>
      <ejb-class>com.abc.app.dao.UserEM</ejb-class>
      <session-type>Stateless</session-type>

      <persistence-context-ref>
        <persistence-context-ref-name>userEntityManager/em</persistence-context-ref-name>
        <persistence-unit-name>DataSource1</persistence-unit-name>
      </persistence-context-ref>
    </session>
  </enterprise-beans>
</ejb-jar>
...

When I only create one session bean, it works as expected. BUT I create more than two, it throws an exception on deploy as:

Caused by: java.lang.IllegalArgumentException: JBAS011053: Incompatible conflicting binding at java:comp/env/userEntityManager/em source: org.jboss.as.jpa.injectors.PersistenceContextInjectionSource@937b07ef
    at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.addJndiBinding(ModuleJndiBindingProcessor.java:237)
    at org.jboss.as.ee.component.deployers.ModuleJndiBindingProcessor.deploy(ModuleJndiBindingProcessor.java:136)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

What is going on here. Can anyone help? Any suggestions or comments are appropriated.

Nawa


Solution

  • After playing with it for a while I found the solution. Here is the code.

    And here is the ejb-jar.xml

     ...
      <enterprise-beans>
        <session>
          <ejb-name>UserEM1</ejb-name>
          <ejb-class>com.abc.app.dao.UserEM</ejb-class>
          <session-type>Stateless</session-type>
    
          <persistence-context-ref>
            <persistence-context-ref-name>userEntityManager/em1</persistence-context-ref-name>
            <persistence-unit-name>DataSource1</persistence-unit-name>
          </persistence-context-ref>
        </session>
        <session>
          <ejb-name>UserEM2</ejb-name>
          <ejb-class>com.abc.app.dao.UserEM</ejb-class>
          <session-type>Stateless</session-type>
    
          <persistence-context-ref>
            <persistence-context-ref-name>userEntityManager/em2</persistence-context-ref-name>
            <persistence-unit-name>DataSource2</persistence-unit-name>
          </persistence-context-ref>
        </session>
      </enterprise-beans>
    </ejb-jar>
    ...
    

    What I didn't know was that we were not only declare two EJBs here but we are also declaring two persistence context reference names which have to be unique in the application. Another word the value in 'persistence-context-ref-name' must also be unique. Once this is done the application is deployable and two session beans are created with different data source. The bean can be looked up with JNDI.

    Cheers,