users@glassfish.java.net

Re: JPA - entityManager.flush()

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Tue, 15 May 2007 20:51:28 +0200

In your code:
1:Collection<SomeEntity> entities = em.createNamedQuery(...).getResultList();
2:
3:for (SomeEntity currEntity : entities) {
4: currEntity.setSomeBooleanFlag(true);
5: mergeEntity(currEntity);
6:}

in line 5 you are invoking (indirectly) em.merge which is unnecessary,
because "currEntity" is already in touch with entity manager. Every
such entity, when you change it, it will be automatically updated in
database when transaction ends.
I have a strange feeling, that there is some strange bug in TopLink,
where unnecessary #merge operations broke something (such a merge
should be NOOP in your case).
Try getting rid of line 5 and see what happens.

Remember: em.persist or em.merge should be invoked against detached
objects to make them managed by persistence context. They are not
actually inserting/saving them into database.

If you find that removing of line 5 solves your problem, tell us here,
it should be filled as a bug. As I said, it might be possible, that
there is something wrong with TopLink. Once, I had strange expetions
(OptimisticLockException as I remember) and removing of unnecessary
#em.merge solved the problem (but I might be wrong, I mean maybe I did
some other changes, but I am quite sure it was just removing of that
em.merge).


2007/5/13, glassfish_at_javadesktop.org <glassfish_at_javadesktop.org>:
> Well,
> Just one though,
>
> This method I just described is annotated with @Timeout, It runs every X seconds and invoke a query to seek for 'entities' that were not flagged with that boolean flag,
>
> Afte flush (And also the method ends so I assume the transaction should be comitted alreardy), the next time this method time outs it query the database and finds out that the boolean flag was not flagged at all!
>
>
> I'm not sure how exactly JPA handles the state of the entities, I assume it has its internal cache mechanisms, but still,
>
> Is there any way to safely commit the transaction/flush the data into the database at some point? so I can be safely sure that the next time I query some entities the query will be executed over the comitted data?
>
>
> As always, thanks.
>
> Of course, I use EJB3 / injected entityManager...
>
>
> Thanks.
> [Message sent by forum member 'trouby' (trouby)]
>
> http://forums.java.net/jive/thread.jspa?messageID=216670
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>