3 Replies Latest reply on Aug 7, 2008 1:17 AM by System Administrator

    Dynamically inject correct @PersistenceContext

    Guillaume Jeudy Master

      Hi,


      I have a requirement where I have an EJB that will select a different entityManager based on arguments passed to the business method call.


      I'm using

      @PersistenceContext

      injection with CMT in my app module. Using this approach I would have to statically inject all entityManagers in different instance variables and then select the right one based on conditional logic. What I don't like about this approach is the side effect of having all entityManagers join the active transaction even if only one of them is used.


      I looked at BMT but to my knowledge I cannot join an existing transaction from a BMT so I cannot use this approach either.


      Now I'm looking at the possibility of doing a lookup in JNDI for a given transaction-scoped entityManager.


      Would adding the following bit in persistence.xml work ?


      <property name="jboss.entity.manager.jndi.name" value="java:/Manager1"/>



      then manually code JNDI lookup in EJB..


      Any insights or suggestions appreciated,


      Regards,
      -Guillaume

        • 1. Re: Dynamically inject correct @PersistenceContext
          Francisco Jose Peredo Noguez Master

          Have you tried with org.jboss.seam.Component.getInstance(name) ?

          • 2. Re: Dynamically inject correct @PersistenceContext
            Guillaume Jeudy Master

            No I haven't tried that, this module of my app is not using Seam managed transactions so I cannot use Seam facilities to tackle this problem. The UI module is the only module that uses Seam.


            Sorry if I may have posted in wrong forum but the other JBoss forums have no response or very slow response times.


            Just hoping someone had to deal with a similar problem before and share his experience.

            • 3. Re: Dynamically inject correct @PersistenceContext
              System Administrator Expert

              Are you trying to access different databases dynamically?


              Perhaps you can use EL expression as below instead of injecting multiple EntityManager instances via @PersistenceContext.


              @PersistenceContext(value="#{...}")


              I've never tried the above code but give it a shot.


              I have @In(jected) multiple EntityManager instances in the past with SMPC.


              Since the @PersistenceContext or @In injection will execute prior to any method calls on your session bean, you won't need to pass a param to your method.


              And I don't think your following concern is valid:



              What I don't like about this approach is the side effect of having all entityManagers join the active transaction even if only one of them is used.

              AFAIK, EntityManager instances are used in isolation of one another unless there's a XA/2PC distributed transaction that occurs...


              Another option is to create separate session beans for each EntityManager and call the correct session bean accordingly.