Subsonic and optimistic concurrency

Does Subsonic use optimistic concurrency?

+3
source share
2 answers

If using use , you mean built-in for SubSonic, then no. However, optimistic concurrency can be achieved with SubSonic quite simply.

Assuming you are using SQL Server (if not, I will let you translate the following instructions into a solution that works with your database provider), this is one way:

  • Include a type column timestampin each table that you want to provide 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 */
    )
    
  • Read the value of the timestamp along with the data to use it later for comparison.

    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      
    
  • UPDATE . , , ( , )

    // ... 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
    }
    
+3

Source: https://habr.com/ru/post/1719046/


All Articles