Subsonic and optimistic concurrency

subsonic

Does Subsonic use optimistic concurrency in some way?

Best Answer

If by use you mean built-in to SubSonic, then no. Optimistic concurrency can however be achieved with SubSonic reasonably simply.

Assuming that you are using SQL Server (if not I'll let you translate the following instructions into a solution that works on your database provider) this is one way to go:

  1. Include a column of type timestamp on each table you wish to ensure concurrency.

    CREATE TABLE Product
    (
        ProductID int NOT NULL IDENTITY(1,1),
        Name varchar(256) NOT NULL,
        RowStamp timestamp  /* This will hold a timestamp for the table */
    )
    
  2. Read the value of the timestamp along with the data so that you can use it later to compare.

    var product = new SubSonic.Select()
        .From<Product>()
        .Where(Product.ProductIDColumn).IsEqualTo(productId)
        .ExecuteSingle<Product>();
    var rowStamp = product.RowStamp;
    
    // ...  Show a form to the user with the data from the product      
    
  3. When performing an UPDATE compare the value of the the timestamp to the database value. If the timestamp doesn't match, the row has been modified and the user can be notified of the situation (or you can handle it however you like)

    // ... After retrieving the values from the form
    
    var result = new SubSonic.Update(Product.TableSchema)
        .Set(Product.NameColumn).Equal(newName)
        .Where(Product.ProductIDColumn).IsEqualTo(productId)
        .And(Product.RowStamp).IsEqualTo(rowStamp)
        .Execute();
    
    if (result != 1)
    {
        // Notify the user there may be a problem
    }
    
Related Topic