Advanced Database Persistence for Java

Examples & Reference Manual

 

Example 16 - Optimistic Locking (Row Version Control)

This example shows how to use optimistic locking strategy when updating or deleting table rows.

Example Source Code

package examples;

import java.sql.SQLException;
import java.sql.Timestamp;

import org.hotrod.runtime.exceptions.StaleDataException;

import daos.ClientDAO;

/**
 * Example 16 - Optimistic Locking (Row Version Control)
 * 
 * @author Vladimir Alarcon
 * 
 */
public class Example16 {

  public static void main(String[] args) throws SQLException {

    System.out.println("=== Running Example 16 - Optimistic Locking (Row Version Control) ===");

    Timestamp now = new Timestamp(System.currentTimeMillis());

    // ----------------------------------
    // 1. Insert with row version control
    // ----------------------------------

    System.out.println(" ");
    ClientDAO c = new ClientDAO();
    c.setCreatedAt(now);
    c.setName("John Silver");
    c.setState("WA");
    c.setDriversLicense("WT5429-3342");
    c.setReferredById(null);
    c.setTotalPurchased(0L);
    c.setVip(false);
    c.setRowVersion(12345L); // Ignored. Set to zero on insert
    c.insert();
    Integer id = c.getId();
    System.out.println("1. Insert with row version control.");

    // ---------------------------------------------------
    // 2. Update with row version control - No concurrency
    // ---------------------------------------------------

    System.out.println(" ");
    c.setReferredById(22);
    try {
      c.update(); // should succeed
      // Update succeeded; row hadn't been updated by another process.
      System.out.println("2. Update with row version control - Succeeded (expected behavior)");
    } catch (StaleDataException e) {
      // Update failed; row had been updated by another process.
      System.out.println("2. Update with row version control - Failed (should not happen)");
    }

    // -----------------------------------------------------
    // 3. Update with row version control - With concurrency
    // -----------------------------------------------------

    System.out.println(" ");

    // 3.1. Meanwhile the row is updated (could be another process or thread).
    ClientDAO example = new ClientDAO();
    example.setId(id);
    ClientDAO updateValues = new ClientDAO();
    updateValues.setDriversLicense("12345-67890");
    ClientDAO.updateByExample(example, updateValues); // always succeeds and
                                                      // increments the version

    // 3.2. Now we try to update the original row
    c.setState("NY");
    try {
      c.update(); // should fail
      System.out.println("3. Update with row version control (with concurrency) - Succeeded (should not happen)");
    } catch (StaleDataException e) {
      System.out.println("3. Update with row version control (with concurrency) - Failed (expected behavior)");
    }

    System.out.println(" ");
    System.out.println("=== Example 16 Complete ===");

  }

}

How to Run this example

The Example 16 is included in the download package. To run this example please refer to the section How to Run the Examples above.

Case #1: Insert

This case inserts a row into a table with row version control. Note the value specified for the version control row ( ROW_VERSION in this case, as specified in the configuration file) is ignored and initialized at zero. The initial, maximum, and minimum values can be configured (not this case).

The insert operation is always successful.

Case #2: Successful update (no concurrency detected)

This case retrieves a table row and later tries to update it. Since there's no other process or thread that updates it in the mean time, the update succeeds.

Case #3: Failed update (concurrency detected)

This case retrieves a table row and later tries to update it.

In between another update takes place on the same row (and the ROW_VERSION value is incremented).

The update fails (since the ROW_VERSION column does not match anymore) by throwing a StaleDataException .