2 Replies Latest reply on Aug 9, 2007 4:15 PM by dahunter

    EntityManager.flush not flushing

    dahunter

      Hi,

      I have created an EJB 3.0 application that consists of EJBs and JSP pages. I have a problem where a call to EntityManager.flush in a stateless session bean seems to be ignored. Here is what is happening:

      A timer kicks off a stateless session been every X minutes. This bean does some work, sends an HTTP POST to another server and then logs an entry in an Oracle database that records that it sent the POST. This is done in a loop that could run 10 times, or hundreds of times. So every iteration of the loop makes an HTTP POST and creates 1 database record. After the end of each iteration I am calling flush to (try) and force the record to be written out to Oracle, and not just stored in JBoss? cache.

      While the above loop is running, the server I sent the HTTP POST to in the loop above starts connecting to a JSP page I have created to say that it completed the work I sent it. The JSP page uses another stateless session bean, which looks up the database record created in the loop by its primary key and attempts to update it. The problem I am having occurs here. If the method with the loop above is still running, the database record is not found.

      So JBoss is not storing the record in Oracle until the method completes, even though I am calling flush. And since my lookup for the record is based on its primary key, JBoss doesn?t check its cache but goes directly to the database (according to the docs I read anyway).

      I am using JBoss 4.0.4. Does anyone have any tips or ideas as to how I can overcome this? Thanks!

      DaHunter

        • 1. Re: EntityManager.flush not flushing
          lpmon

          I am having a similar problem.

          It appears entityManager.flush() is ignored. I never see data making it to the DB unless all methods in a stack of SLSB methods exit.

          If I run the same identical code with a few inserts all is well. If I run it with thousands of inserts my app crashes due to lack of memory. The whole point of calling flush is to release the memory!

          This seems like such a fundamental thing I am wondering if there is some setup I am missing. I find it hard to believe this bug would exist since it is so important for it to work properly.

          Using:

          JBoss AS 4.0.5
          MySQL 5.???
          Injected EntityManager

          Ideas anyone?

          • 2. Re: EntityManager.flush not flushing
            dahunter

            I did find a work around to this. It seems that any method that referenced the EntityManager had to be in a transaction, and the whole method had to complete before the commits occured.

            To work around it I removed all references to the EntityManager from my session bean with the timer and set my long running (looping) method so that it doesn't support transactions.

            I then created another bean that performs the actual database writes. I then called those methods that write to the database from within the long running (looping) method.

            It seems like a lot of overhead to require this to be done in different beans, but it did work.

            DaHunter