4 Replies Latest reply on Dec 7, 2006 12:54 AM by akrishmohan

    Performance question on EntityManager

    akrishmohan

      Hi All

      I have a scenario and am trying to evaluate the performance of using the entity manager in a Message Driven Bean (MDB)

      class MDBean {
      @persistencecontext(....)
      private EntityManager em;

      public void onMessage() {
      ....
      while (someCondition) {
      ClassB b = new ClassB();
      b.setEntityManager(em);
      b.doSomething();
      }
      ............
      }

      Basically ClassB carries out persisting of about 6 tables based on the entitymanager passed to it. This process happens in a loop and the loop runs for about 10 - 20 times. I was wondering if I need to close the entity maanger at some point and re-create it? What would be the performance hit if any? Let me know what other design considerations need to be taken into account?

      Thanks
      K

        • 1. Re: Performance question on EntityManager
          mimra

          Hi

          When the entity manager is injected, it is managed by the container, and you should not close it yourself.

          /Michael

          • 2. Re: Performance question on EntityManager
            akrishmohan

            So, for my scenario what do you think is the best ? Let the container manage it after injection or I create one and manage it myself ?

            Let me know

            Thanks
            K

            • 3. Re: Performance question on EntityManager
              markvl

              In this scenario there is no advantage in using multiple entity managers, so you should let the container handle it.

              If anything, in this scenario the use of multiple entity managers may slow down your app as it may mean updates / deletes are flushed more frequently than they need to be.

              I suggest you code it the simplest way, ie container managed, then if performance is unacceptable look for bottlenecks to be optimised.

              • 4. Re: Performance question on EntityManager
                akrishmohan

                Thanks for your response.

                Coming back to the problem, basically, I am supposed to update DB with contents of a file. If anything happens during data insertion, the whole transaction needs to be rolled back. So the reading of file and updating it into the DB should be in one transaction unit. Since there are multiple files to be read, I was wondering if I could open the entity manager before file-read/DB-update and close immediately after. Will CMP still yield better performance for this case too? Sorry if I sound repetitive.

                I am just trying to understand how Container Manager Persistence (CMP) demarcates the transaction here and handles persistence.

                Thanks
                K