1 Reply Latest reply on Nov 17, 2006 8:11 AM by mfishman

    EntityListener, EntityManager, and Threading

    justinmiller

      We have a situation where we have specified an @EntityListener with @Post* annotated methods for a set of entity beans (ejb3 pojo's that is). From the @Post* methods, we then pass the pojo to a MBean service where we know there is both threading and further database access occurring.

      It is when the pojo is passed to the mbean that we are running into errors. A forum posting says that EntityManager methods may not be invoked in an @EntityListener - so that could be one cause of our errors. The other contributing factor could be the threading that is occurring in the MBean; the ejb spec prohibits spawning threads from an EJB.

      Are we correct in one or both of these conjectures?

      Is there a better and/or correct way to accomplish what we're trying to do?

      The caveat with the post-handling is that we will take generic action based on the fact that we have an entity bean in our hands. We don't care about the actual declared type of the pojo, just that it is, in fact, an entity bean.

      Thanks,
      Justin

        • 1. Re: EntityListener, EntityManager, and Threading
          mfishman

          To try and further explain what we are attempting to do, we basically have the following case:

          1. An EntityListener is listening for when an entity is persisted, updated, or deleted
          2. When that happens, the EntityListener wants to evaluate some other DB state to make a decision. To make that decision we had wanted to use an EntityManager to lookup the values in the DB that we needed.
          3. Based on the evaluation in step 2, we wanted to do a call to a JMX Service that we have, which happens to be running in the same instance of JBoss in this case, to do some further work. This service has some threading in it and has its own references to some EJB3 Session beans.

          Our questions are:
          1. Is it true that you can not access an EntityManager from an EntityListener?
          2. If the answer to question one is true, what is the correct way to relatively generically trigger some work to be done when the DB changes (assuming that work requires access to other information in the DB)?
          3. It is our understanding that the MBean spec allows threading. Since EJBs are not supposed to spawn their own threads does this mean that you should really never access a MBean from within a EJB?

          Thanks a lot.

          -- Mark