Skip to main content

EntityManager.refresh() loses changes that have been flushed.

2 replies [Last post]
Joined: 2005-11-10


The following code results in loss of changes made before em.flush() at (b) in B47. The application is deployed as EAR, ie. the code is executed inside of GlassFish.
@PersistenceContext(EXTENDED) EntityManager em;
MyEntity myEntity = em.find(MyEntity.class, id);
em.refresh(myEntity); // Maybe not neccesary, but I needed it for the app. to work with B36.
// Here, myEntity.getMemo() is null.
myEntity.setMemo("Hi, it's new memo");
// Here, myEntity.getMemo() is "Hi, it's new memo"
em.flush(); // (a)
em.refresh(myEntity); // (b)
// Here, myEntity.getMemo() is null, not "Hi, it's new memo"
// If you comment out (b), you will get "Hi, it's new memo" here.
The property "memo" is a simple String persistent property. Any attempt to access myEntity.getMemo() result in 'null' after (b). The MEMO column in the database is left unchanged. Changes made (a) is completely lost.

When I was using B36, I had this problem too. But (a) and (b) were not there. They were added to avoid the probelm in B36. Now, (b) causes the problem.

Any help is appreciated.
Thank you.
- Ryosuke.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-06-21

Hello Ryosuke,
Is this error still occuring in the latest promoted V2 builds? If it still occurs is there any SQL issued? if you remove the first 'refresh' call does the problem go away?

Joined: 2005-11-10

Hello Gordon,

Yes, with v2-b03, the problem is still occurring. Removing the first 'refresh' call doesn't change the situation.

The reason why I call refresh() for every entities obtained by em.find() or Query is ... : I had this kind of problems since B34 or B36 (I forgot the exact build number). And, calling flush() and refresh() at certain points in one transaction fixed the problem. In the previous code, the first refresh(), (a), and (B) are the code to fix the problem in B36.

Unfortunately, the attempt could not always stop the problem. With the same EAR, and with the same GlassFish build, the problem sometimes occured and sometimes did not occur.

With V1 B47 and V2 builds, the problem mich more frequently (almost always) occurs.

I don't know how to make Toplink write SQLs in logs. I will check the documents.

- Ryosuke.