persistence@glassfish.java.net

Re: UPDATE after SELECT when executing em.find

From: Dies Koper <dies_at_jp.fujitsu.com>
Date: Thu, 24 Apr 2008 11:25:34 +0900

Hello James,

I had suspected that hashtables not having a predictable iteration order
might be related. Thanks for your confirmation.

When I made the field transient the problem did not occur.
I also replaced the Properties field with a LinkedHashMap as you
suggested and the problem did not occur. I hope this was not coincidence ..
I suppose I'll have to do some refactoring.

Thanks a lot,
Dies

James Sutherland wrote:
> That is quite odd. I assume it is related to you Properties LOB and change
> tracking thinking this field value has always changed. Please confirm this
> by marking the field as transient.
>
> Since the properties is serialized, the way TopLink uses to determine if it
> has changed is to serialize it and compare the bytes. So what is most
> likely occurring is that the byte are changing, either because something in
> your object or app is changing the value, or because the Properties does not
> produce a consist serialized bytes representation. My guess would be
> because Properties is a Map and hashing can be random, it is producing
> different bytes each time, or more likely some of the time.
>
> If this turns out to be the case, as a work around you could try changing
> the type to something with a consistent serialization (List or LinkedHashMap
> perhaps), or map the properties to another table such as using a TopLink
> DirectMapMapping. Also in TopLink 11g and EclipseLink there is a mutable
> setting that can be used with attribute change tracking that will track
> changes differently and most likely not have this issue.
>
>
> Dies wrote:
>> Hello,
>>
>> I'm seeing an UPDATE after SELECT when executing em.find and I hope
>> someone can help me with it.
>> I want to use a @Version field with optimistic locking, but each time I
>> do a find the version field is incremented.
>>
>> My entity has a field of type java.util.Properties, which I marked with
>> @Lob.
>> From my session bean I add some properties to it and persist it. That
>> goes fine.
>> In the next call I do an em.find in the session bean and return the
>> entity instance to the client. That's all. However, when I set the
>> toplink.logging.level.sql property to FINE I see that after the SELECT,
>> and UPDATE is executed too.
>>
>> Why?
>> When I add only one property, I see no UPDATE.
>>
>> I'm using GlassFish V2 UR1 with the Toplink Essentials that comes with
>> it, the database is Oracle and the table is created using
>> toplink.ddl-generation.
>>
>> I'm using a JTA Entity Manager. Here's my entity's source:
>>
>> import java.io.Serializable;
>> import java.util.Properties;
>>
>> import javax.persistence.*;
>>
>> @Entity
>> public class Target implements Serializable {
>> private static final long serialVersionUID = 1L;
>>
>> @Id
>> private Long id=1L;
>>
>> // @Version
>> private int version;
>>
>> @Lob
>> private Properties properties = new Properties();
>>
>> public Long getId() {
>> return id;
>> }
>>
>> public int getVersion() {
>> return version;
>> }
>>
>> public Properties getProperties() {
>> return properties;
>> }
>>
>> Thanks!
>> Dies