<StateOptimisticLock> Element

Makes the object available for state optimistic locking using PersistentOptimisticLockServer. This locking strategy uses the object state to detect changes to the object.

<Type name="type">

       <StateOptimisticLock [view="view-name"]/>

</Type>

type: A class specifying a non-abstract persistent type.

view-name: Name of the view to be used for detecting state changes. By default the full view is used, which contains all mapped database fields of the persistent type (value type properties and foreign-key values of object references).

Remarks

·    Types marked with the <StateOptimisticLock/> are available for optimistic locking with PersistentOptimisticLockServer. When changes are committed, the state of the locked object (as it was loaded from the database) is compared with the current database state.

·    Unlike <OptimisticLock/>, which requires a version field, this locking strategy can be implemented without modifying the database schema.

·    Compared to <OptimisticLock/>, the disadvantages of this locking strategy are higher resource consumption (because the old state has to be stored in memory) and poorer database performance (because all state fields have to be compared when committing).

·    To exclude large fields, e.g. images mapped to binary[], specify a custom view to be used.

Examples

To use state optimistic locking, you need to:

·    specify <StateOptimisticLock/> strategy in the mapping

·    set up a PersistentOptimisticLockServer in the DataDomain.

·    configure optimistic locking semantics for the Context

Specifying StateOptimisticLock strategy in the mapping

The following code enables state optimistic locking on Customer and specifies MyLockView to be used:

<Type name="Customer">

       <StateOptimisticLock view="MyLockView"/>

 

       <View name="MyLockView">

              <Member>CustomerId</Member>

              <Member>CompanyName</Member>

       </View>

</Type>

Set up a PersistentOptimisticLockServer in the DataDomain

The following code sets up the infrastructure for maintaining persistent optimistic locks in the DataDomain. Please note that the lock server can only be set up when you instantiate the DataDomain. You cannot add a lock server to an existing DataDomain instance:

DataDomainConfiguration ddConfig = new DataDomainConfiguration();

ddConfig.Schema = DataDomainSchema.LoadFrom(SCHEMA_ASSEMBLY);

ddConfig.ConnectionString = "Server=(local);Database=Northwind;Integrated Security=True;User ID=;Pwd=";

ddConfig.Role = DATADOMAIN_ROLE;

 

ddConfig.LockServers.Add(typeof(object), PersistentOptimisticLockServer.Value);

 

DataDomain newDataDomain = new DataDomain(ddConfig);

The lock server is set for a given type (in this case System.Object) and automatically applies to all derived types as well.

Please refer to the User’s Guide to learn more about how to initialise and use the DataDomain in your project.

Configuring optimistic locking semantics for the context

Finally, before using locking, you need to configure optimistic locking semantics for the context:

// context locking semantics setup

LockingSemanticsBindings lockingSemantics = new LockingSemanticsBindings();

lockingSemantics.Add(typeof(object), OptimisticLockingSemantics.Value);

 

// instantiate context with locking semantics

using(Context.Push(LocalContext.Create(lockingSemantics)))

{

  Customer c = …

  Context.Lock(c);

  Context.CommitCurrent();

}

Requirements

Editions:Professional, Evaluation, Express

Database Platforms:Microsoft SQL Server 2000, Microsoft SQL Server 2005, Oracle 9i Release 2, Oracle 10g Release 2, IBM DB2

See Also

Optimistic object-level locking in the User’s Guide, <OptimisticLock/> element, <View/> element