Skip Headers

# Configuring Attribute Change Tracking Policy

Previous

Next

The `AttributeChangeTrackingPolicy` provides improved unit of work commit performance for objects with many attributes and few changed attributes. In general, this is the most efficient change policy. It is the default change policy for EJB 3.0 persistent applications and EJB 2.n CMP applications deployed to OC4J. For more information, see "Attribute Change Tracking Policy").

Note: You cannot use the `AttributeChangeTrackingPolicy` if you are using any instance of `FieldsLockingPolicy` (see "Optimistic Field Locking Policies").

When you deploy a TopLink-enabled EJB 3.0 persistent application or EJB 2.n CMP application to OC4J, TopLink automatically configures your persistent classes to use the `AttributeChangeTrackingPolicy` and, using bytecode weaving (EJB 3.0) or code generation (EJB 2.n), configures your persistence classes to implement the TopLink `ChangeTracker` interface. In this case, you do not need to explicitly configure this change policy.

To configure TopLink to use an `AttributeChangeTrackingPolicy` for plain Java objects or other application servers, use this procedure:

1. Create a descriptor amendment method (see "Configuring Amendment Methods") that sets the change policy as Example: Setting the DeferredChangeDetectionPolicy illustrates.

   Setting the DeferredChangeDetectionPolicy

       descriptor.setObjectChangePolicy(new AttributeChangeTrackingPolicy());

2. Code each of your persistent classes to implement the `ChangeTracker` interface as Example: Implementing the ChangeTracker Interface for the AttributeChangeTrackingPolicy illustrates.

   Implementing the ChangeTracker Interface for the AttributeChangeTrackingPolicy

       public class Employee implements ChangeTracker {

           PropertyChangeListener listener;

           public PropertyChangeListener getTopLinkPropertyChangeListener() {
             return listener;
           }

           public void setTopLinkPropertyChangeListener(PropertyChangeListener listener) {
             this.listener = listener;
           }
       ...
           public void setId(long id) {
               _id_change(id);
           }

           protected void id_change(long id) {
               if (listener != null && this.id != id) { // throttle unnecessary events            listener.propertyChange(
                       new PropertyChangeEvent(
                           this,
                           "id",
                           new Long(getId()), // primitives must be wrapped
                           new Long(id)
                      )
                   );
               }
               this.id = id;
           }
       ...
       }