users@glassfish.java.net

Re: JPA - entityManager.flush()

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Wed, 16 May 2007 12:38:11 +0200

> So what is the meaning of em.flush() in container managed transactions if the data is only comitted at the end of the transaction? only relevant if the entities being accessed by the same EM?

You need flush, when you are querying database within same
transaction, for example you might change some entities (like you did
setting that boolean flag) and later, in the same transaction you
might need to invoke another query. If that another query would return
one or more entities that were changed, without flush you could get
stale data.

> So, I must use a JDBC connection? there's no way to modify entities and commit the changes at a specific time?

The transaction philosophy is simple. You start transaction, change
some entities and commit or rollback changes. When transaction is
still active, no one knows if it is going to be rolled back or not, so
another transactions cannot see changes, because no one knows if that
changes are going to be committed or rolled back. Only when
transaction commits database rows are updated. That is fundamental.

Maybe you should split your transaction into two or more separate
ones? Or maybe you should use locks (or optimistic locking)? There are
many ways to go.


>
> Thanks.
> [Message sent by forum member 'trouby' (trouby)]
>
> http://forums.java.net/jive/thread.jspa?messageID=217310
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>