users@glassfish.java.net

When/when does JPA flush to the DB?

From: <glassfish_at_javadesktop.org>
Date: Tue, 01 Apr 2008 11:27:55 PST

We just had a strange problem, and I'm curious what the background is behind it.

We were creating some new objects, basically a parent and several different children. A whole graph of stuff. This has been fine and worked well for us before.

At one point, we added the object (which were all unmanaged at this point, being new) to the collection of a managed object that we had looked up.

As we continued with the process, we called a Query, using em.createQuery and Query.getResultList().

What confuses me is that even though this query had nothing to do with the either the previous objects (we were simply looking up some new as yet unrelated data), the EM decided to flush the previous changes.

This was consistent and completely repeatable for our scenario, and it was catching us off guard. So, it was effectively updating the managed object, and at the same time persisting the new data behind our backs.

What kind of rules does the EM use when deciding to persist data? Why would an unrelated Query trigger such a flush? We weren't at the end of a transactiion or anything, and in fact we hadn't persisted or merged any data (at least consciously) at that point.

So, just curious why it happened. Any insight would be appreciated, and whether it's a JPA behavior or a TopLink specific behavior that we encountered.
[Message sent by forum member 'whartung' (whartung)]

http://forums.java.net/jive/thread.jspa?messageID=267157