When you start the Java™ Persistence API (JPA) time-based updater, the ObjectGrid maps are updated with the latest changes in the database.
If you create the timeBasedDBUpdate configuration for the backing map, the time-based database updater is automatically started when a distributed ObjectGrid primary shard is activated. For an ObjectGrid that has multiple partitions, the time-based database updater only starts at partition 0.
If you create the timeBasedDBUpdate configuration for the backing map, the time-based database updater is automatically started when the local map is activated.
public synchronized void startDBUpdate(ObjectGrid objectGrid, String mapName, String punitName, Class entityClass, String timestampField, DBUpdateMode mode) {
If you want to stop the time-based database updater, you can call the following method to stop the updater:
public synchronized void stopDBUpdate(ObjectGrid objectGrid, String mapName)
The ObjectGrid and mapName parameters should be the same as those passed in the startDBUpdate method.
As a part of the optimistic locking feature, DB2 9.5 provides a row change timestamp feature. You can define a column ROWCHGTS using the ROW CHANGE TIMESTAMP format as follows:
ROWCHGTS TIMESTAMP NOT NULL GENERATED ALWAYS FOR EACH ROW ON UPDATE AS ROW CHANGE TIMESTAMP
Then you can indicate the entity field which corresponds to this column as the timestamp field by either annotation or configuration. An example follows:
@Entity(name = "USER_DB2") @Table(name = "USER1") public class User_DB2 implements Serializable { private static final long serialVersionUID = 1L; public User_DB2() { } public User_DB2(int id, String firstName, String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; } @Id @Column(name = "ID") public int id; @Column(name = "FIRSTNAME") public String firstName; @Column(name = "LASTNAME") public String lastName; @com.ibm.websphere.objectgrid.jpa.dbupdate.annotation.Timestamp @Column(name = "ROWCHGTS", updatable = false, insertable = false) public Timestamp rowChgTs; }
In Oracle, there is a pseudo-column ora_rowscn for the system change number of the record. You can use this column for the same purpose. An example of the entity that uses the ora_rowscn field as the time-based database update timestamp field follows:
@Entity(name = "USER_ORA") @Table(name = "USER1") public class User_ORA implements Serializable { private static final long serialVersionUID = 1L; public User_ORA() { } public User_ORA(int id, String firstName, String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; } @Id @Column(name = "ID") public int id; @Column(name = "FIRSTNAME") public String firstName; @Column(name = "LASTNAME") public String lastName; @com.ibm.websphere.objectgrid.jpa.dbupdate.annotation.Timestamp @Column(name = "ora_rowscn", updatable = false, insertable = false) public long rowChgTs; }
For other types of databases, you can create a table column to track the changes. The column values have to be manually managed by the application that updates the table.
Take an Apache Derby database as an example: You can create a column "ROWCHGTS" to track the change numbers. Also, a latest change number is tracked for this table. Every time a record is inserted or updated, the latest change number for the table is incremented, and the ROWCHGTS column value for the record is updated with this incremented number.
@Entity(name = "USER_DER") @Table(name = "USER1") public class User_DER implements Serializable { private static final long serialVersionUID = 1L; public User_DER() { } public User_DER(int id, String firstName, String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; } @Id @Column(name = "ID") public int id; @Column(name = "FIRSTNAME") public String firstName; @Column(name = "LASTNAME") public String lastName; @com.ibm.websphere.objectgrid.jpa.dbupdate.annotation.Timestamp @Column(name = "ROWCHGTS", updatable = true, insertable = true) public long rowChgTs; }