Question

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

Was it helpful?

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,

OTHER TIPS

Try changing @Resource to @PersistenceContext:

@PersistenceContext(name="userEntityManager/em")
private EntityManager em;

Update What about injection via ejb-jar.xml? Define EntityManager em without an annotation and add the following to your xml:

<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>
    <injection-target>   
      <injection-target-class>com.abc.app.dao.UserEM</injection-target-class>   
      <injection-target-name>em</injection-target-name>  
    </injection-target>
  </persistence-context-ref>
</session>

<session>
...
</session>

I've also noticed you have two enterprise-beans element. Define only one and define session beans with session element inside that one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top