#271 CMP field <state-factory>

JBossCMP (36)

In jbosscmp-jdbc.xml, a cmp-field may have an optional
element <state-factory>. The value should the fully
qualified class name of the implementation of
interface which has the following two methods:

* Calculates and returns an object that represents
the state of the field value.
* The states produced by this method will be used
to check whether the field
* is dirty at synchronization time.
* @param fieldValue field's value.
* @return an object representing the field's state.
Object getFieldState(Object fieldValue);

* Checks whether the field's state <code>state</code>
* is equal to the field value's state (possibly,
calculated with
* the <code>getFieldState()</code> method).
* @param state the state to compare with field
value's state.
* @param fieldValue field's value, the state of
which will be compared
* with <code>state</code>.
* @return true if <code>state</code> equals to
<code>fieldValue</code>'s state.
boolean isStateValid(Object state, Object fieldValue);

There are default implementations for Map, List, Set
and arrays which are set automatically for fields of
the corresponding types.
For primitives, their wrappers and java.lang.String,
the implementation relies on equals(Object o) method to
check the dirty state.
The default implementation for other types always
returns false from isStateValid() unless one of the
arguments is null and the other one is not.


  • Juha Lindfors
    Juha Lindfors

    Logged In: YES

    What do I use this for?

  • Logged In: YES

    This is used for dirty checking.
    Earlier, we had a reference to the old field value and the
    current value. At synchronization we relied on equals() to
    check the field for dirty state.

    There are two problems with this approach:
    1. the old and the current values refer to the same object.
    If the type of the field is mutable, you called the getter
    and modified the value (w/o calling the setter), the field
    will never be find as dirty.
    This problem is solved by the getFieldState() method. For
    immutable types, this method may return the field value
    itself but for mutable types you would return a copy of the
    value or some other object that will represent the state
    (hash code, etc) to avoid, potentially expensive, deep copying.
    2. relying on equals(). As it occurs, sometimes, we can't
    rely on equals(). For example, in case of arrays or some
    custom user types (see [ 837092 ] CMP Field Update
    Optimization is Broken).
    This is solved with isStateValid().

  • Juha Lindfors
    Juha Lindfors

    Logged In: YES

    Thanks Alex.