1 Reply Latest reply on Aug 10, 2006 4:30 PM by ppphoenixxx

    persistent object modification by different threads safe?

      Hi,

      according to EJB3-Specs a single EntityManager object isn't thread-safe, so the same EntityManager shouldn't be used by different threads: but what about obtaining a set of persistent objects, applying some changes on them in a *different* thread, where no EntityManager methods are called or used?

      Well it works, right out of the box: no EntityManager.clear or merge calls needed in the "original" thread - but I have no idea if this is safe, supposed to work or should be avoided at all costs: I only know that I spare myself a little work by not going the usual way to get me another session bean and obtain an entity manager there.

      To give you some details about the application I am writing: Its a standard EJB3 one, with some persistent objects and a built in callback-server which listens in a different thread on a specific network port for connections and should do some persistent operations upon a callback-connection from a client.

      Thanks for any input!

        • 1. Re: persistent object modification by different threads safe

           

          Well it works, right out of the box: no EntityManager.clear or merge calls needed in the "original" thread - but I have no idea if this is safe, supposed to work or should be avoided at all costs

          Okay, avoid! First I thought it works, but now it doesn't: so if it worked at all (was quite tired when I tried it), its absolutely unreliable.

          For the record: I moved to a different approach:

          1st) cache my persistent entities
          2nd) detach from them
          3rd) work on the cached state, update them as I need to (even from different threads)
          4th) update the cached state periodically with a Timer

          Thats accurate enough for me and I can react on events like callbacks immediately.