4 Replies Latest reply on Nov 28, 2005 3:03 AM by taggat

    Getting an EntityManager outside of the jmx bean or session

      I have written a jmx bean that runs ok and has access to the EntityManager of my project.

      However the jmx ben instantiates a thread, this thread needs access to the entity data layer.

      I tried passing the EntityManager into the constructor of the thread, however when i tried to access data it complained about accessing the EntityManager

      My next step was to do call Persistence.createEntityManagerFactory("db");

      this works, however the call seems to create a new entity manager each time. I have it in a singleton now, so it doesn't recreated it each time, however I don't want to create it again in the first place.

      Does anyone know how i get access to the EntityManager defined in the .par file.

      The code

      @PersistenceContext(unitName = "global")
      private EntityManager em;
      


      which works in the top level jmx bean does not seem to work in the helper classes, and i don't want to make them either jmx beans, or session beans, as i tend to instantiate quite a few of them at any one time.



        • 1. Re: Getting an EntityManager outside of the jmx bean or sess
          mbe1

          Hi taggat,

          ich have the same problem. I dont have access to the entitymanager from a thread in a singleton. If I try to access it I get the following Exception:

          javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction
           at org.jboss.ejb3.entity.ManagedEntityManagerFactory.getSession(ManagedEntityManagerFactory.java:124)
           at org.jboss.ejb3.entity.InjectedEntityManager.getSession(InjectedEntityManager.java:154)
           at org.jboss.ejb3.entity.InjectedEntityManager.createQuery(InjectedEntityManager.java:67)
           at helpers.KonDisEntityManager.getByPropertyList(KonDisEntityManager.java:274)
           at taskrunnables.ImportTask.checkExistence(ImportTask.java:662)
           at taskrunnables.ImportTask.handleRow(ImportTask.java:696)
           at taskrunnables.ImportTask.run(ImportTask.java:331)
          


          Did you solve the problem?

          Does anyone know, how to get a working entitymanager into a thread running in a service bean?

          Thanks Meikel

          • 2. Re: Getting an EntityManager outside of the jmx bean or sess

            I have a workaround, but it is not a very nice one.

            I am using 2 classes at the moment.

            One that gives me an entity manager for reading only

            import javax.persistence.EntityManager;
            import javax.persistence.EntityManagerFactory;
            import javax.persistence.Persistence;
            
            public class EntityManagerUtil {
            
             private static EntityManagerUtil instance = new EntityManagerUtil();
             private EntityManagerFactory emf = null;
            
             private EntityManagerUtil()
             {
             emf = Persistence.createEntityManagerFactory("global");
             }
            
             public static EntityManager getEntityManager()
             {
             return instance.emf.getEntityManager();
             }
            
            }
            


            and one that i use to write data (Jmx bean)

            import javax.persistence.EntityManager;
            import javax.persistence.EntityTransaction;
            import javax.persistence.FlushModeType;
            import javax.persistence.PersistenceContext;
            import javax.persistence.Query;
            
            import org.jboss.annotation.ejb.Service;
            
            @Service(objectName = "flytxt:service=dataServer")
            public class DataServer implements DataServerMBean {
             @PersistenceContext(unitName = "global")
             private EntityManager em;
            
             public boolean contains(Object arg0) {
             return em.contains(arg0);
             }
            
             public Query createNamedQuery(String arg0) {
             return em.createNamedQuery(arg0);
             }
            
             public Query createNativeQuery(String arg0, Class arg1) {
             return em.createNativeQuery(arg0, arg1);
             }
            
             public Query createNativeQuery(String arg0, String arg1) {
             return em.createNativeQuery(arg0, arg1);
             }
            
             public Query createNativeQuery(String arg0) {
             return em.createNativeQuery(arg0);
             }
            
             public Query createQuery(String arg0) {
             return em.createQuery(arg0);
             }
            
             public <T> T find(Class<T> arg0, Object arg1) {
             return em.find(arg0, arg1);
             }
            
             public void flush() {
             em.flush();
             }
            
             public <T> T getReference(Class<T> arg0, Object arg1) {
             return em.getReference(arg0, arg1);
             }
            
             public EntityTransaction getTransaction() {
             return em.getTransaction();
             }
            
             public boolean isOpen() {
             return em.isOpen();
             }
            
             public <T> T merge(T arg0) {
             return em.merge(arg0);
             }
            
             public void persist(Object arg0) {
             em.persist(arg0);
             }
            
             public void refresh(Object arg0) {
             em.refresh(arg0);
             }
            
             public void remove(Object arg0) {
             em.remove(arg0);
             }
            
             public void setFlushMode(FlushModeType arg0) {
             em.setFlushMode(arg0);
             }
            
             public void close() {
             // TODO Auto-generated method stub
            
             }
            
            }
            



            I know that is really horrible but it is the only way i can see to do it while not in the context of a session bean, even though i was enstantiated by a jmx bean.


            • 3. Re: Getting an EntityManager outside of the jmx bean or sess
              crazybuddy

              Dear Taggat,

              I had the same problems as mentioned in the previous posts, and I tried your code to in my project, but failed to deploy, the error message said:

              --- MBeans waiting for other MBeans ---
              ObjectName: jboss.j2ee:service=EJB3,name=org.test.ejb.DataServer
              State: FAILED
              Reason: java.lang.ClassCastException: org.jboss.ejb3.service.ServiceContainer

              --- MBEANS THAT ARE THE ROOT CAUSE OF THE PROBLEM ---
              ObjectName: jboss.j2ee:service=EJB3,name=org.test.ejb.DataServer
              State: FAILED
              Reason: java.lang.ClassCastException: org.jboss.ejb3.service.ServiceContainer

              I am afraid I must have missed something from your code, for example, the DataServerMBean interface, is it also an interface of a session bean?

              Or what is the problem in my project?

              Thank you for any help.

              • 4. Re: Getting an EntityManager outside of the jmx bean or sess

                Crazybuddy,

                the only thing i can think of is that you haven't created the DataServerMBean interface that is required to

                import javax.ejb.Remote;
                import javax.persistence.EntityManager;
                
                import org.jboss.annotation.ejb.Local;
                import org.jboss.annotation.ejb.Management;
                
                @Management
                @Remote
                @Local
                public interface DataServerMBean extends EntityManager {
                
                }
                


                other than that, I haven't put anything else in.


                I have started to change my code so that my threads are calling a session bean to do the work, however this has the overhead of the proxies for an ejb call which i wanted to get around, especially in high speed transactions.

                Taggat.