File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes em.refresh required?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "em.refresh required?!" Watch "em.refresh required?!" New topic

em.refresh required?!

Jesse van Houten

Joined: Aug 21, 2008
Posts: 2

I've got a big application which uses @version tags for the optimistic locking.

The thing i'm getting is that in transaction1 a version gets incremented.
- This is also done in the db
- This returns correctly if i do a entityManager.find
- This returns correctly if i do a entityManager.createQuery with simple eql.

But in transaction2, the version is set back when i retrieve the object from the enitity manager (still correct in the database though).

At first i suspected a rollback, that seemed most logical, but it doesnt appear so, since some weird things get me to retrieve the correct version as well.

Let me explain with some pseudo code.

I think i'm doing something else wrong, but the refresh is the only (non-hardcoded) way of getting the real version of the child in the entityManager.

It appears that my parent is in the enitity manager with a old version of child. And when the parent is retrieved, it's old reference of child overwrites the updated (post-transaction1) version of child?

Am i supposed to use refresh, or is something else going wrong?


Jesse van Houten
[ August 21, 2008: Message edited by: Jesse van Houten ]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

It sounds like transaction 1 is not committed when transaction 2 is executed (since its throwing an optimistic locking error), have you tried forcing the commit in transaction 1?

Also keep in mind many EJB/J2EE server implementations are beta-ish and filled with unfixed bugs, what software are you using? You might want to consider taking a look at the source code for the server (if its available) to understand what's going on behind the scenes.

[OCA 8 Book] [Blog]
Jesse van Houten

Joined: Aug 21, 2008
Posts: 2
Yes, but if the changes are not commited, how come the Entity manager still remembers them?

Remember that if i do a direct find on the child, before i do a find on the parent, the child IS correct. Just not if i call the parent first.

It's as if when i call the parent, it has the outdated version of the child, and it replaces the updated one in the entitymanager?!

Btw: Using glassfish v2
[ August 22, 2008: Message edited by: Jesse van Houten ]
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
Have you considered cascading the update from the parent's point of view?

Instead of merging the state of the child, merge the state of the parent.

Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
I agree. Here's the link:
subject: em.refresh required?!
It's not a secret anymore!