Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
Persistent Object Access Outside of a Transaction
Programmer's Guide > OpenAccess ORM Classic (Old API) > Programming With OpenAccess > The OpenAccess ORM Object Lifecycle > Persistent Object Access Outside of a Transaction

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

The state for objects outside of a transaction is hollow. (You may have inferred this from the description of object state changes, appearing above.) When the fields of a hollow instance are accessed from outside of an active transaction, OpenAccess ORM will throw an exception. However, there exist use cases where it is necessary to access a persistent class instance outside of a transaction boundary. For such situations, OpenAccess ORM provides an additional object state, persistent nontransactional. Instances of persistent classes in this state can be accessed outside of a transaction without generating an OpenAccess ORM exception.

To move persistent class instances to the persistent nontransactional state when the transaction has ended and allow access to the objects outside the transaction, you need to set the appropriate Transaction Properties (see RetainValues and RestoreValues).

Retain Values

The default behavior for objects is that they move to the hollow state when a transaction ends. Also, objects may not normally be accessed outside of a transaction. The transaction property RetainValues determines whether objects keep their data values following a transaction commit, and whether the objects may be accessed outside of an active transaction. By setting the RetainValues property to true, you can continue to access your persistent class instances even after the transaction that controlled them ends. You must set the property before you begin the transaction.

C# Copy Code
scope.Transaction.Properties.RetainValues = true;

scope.Transaction.Begin();

// . . .

 

VB .NET Copy Code
scope.Transaction.Properties.RetainValues = True
scope.Transaction.Begin()
' . . .

Any changes made by other processes or by other transactions will not affect the persistent nontransactional objects. Also, if you have not resolved the object in the transaction (accessed the object so as to move it into the persistent clean or persistent dirty state) the object data will not move to the persistent nontransactional state but will remain hollow when the transaction ends. In this case, the object data will not be available even if you have set the RetainValues property.

Retain Values

In the following example, a Customer instance is retrieved from the database. The name is changed within a transaction with the RetainValues property set to true. The transaction is then rolled back, followed by the name is being accessed outside the transaction:

Code

State Change

C# Copy Code
scope.Transaction.Properties.
  RetainValues = true;
VB .NET Copy Code
scope.Transaction.Properties.
  RetainValues = true;

set the RetainValues transaction property to true

C# Copy Code
scope.Transaction.Begin();
VB .NET Copy Code
scope.Transaction.Begin()

begin the transaction

C# Copy Code
Customer c = GetCustomer( . . . );
VB .NET Copy Code
Dim c as Customer = GetCustomer(...)

a Customer instance, c, is retrieved from the database in the "hollow" state

C# Copy Code
c.Name = "Jane";
VB .NET Copy Code
c.Name = "Jane"

a persistent field of c is modified and the object state is moved to "persistent dirty"

C# Copy Code
scope.Transaction.Commit();
VB .NET Copy Code
scope.Transaction.Commit()

c is stored; the RetainValues = true setting means the object is moved to the "persistent nontransactional" state (and does not goes to the "hollow" state) and keeps its current values

C# Copy Code
Console.WriteLine( c.Name );
VB .NET Copy Code
Console.WriteLine( c.Name )

access outside of the transaction is allowed; prints " Jane" (i.e., the modified value)

Restore Values

The RestoreValues property is exactly like the RetainValues property described above except that the object values are reloaded from the database if the object is in the persistent dirty state and the transaction is rolled back. If the transaction is committed, the values are written to the database and these values are kept in the persistent nontransactional state.

Restore Values

In the following example, a Customer instance is retrieved from the database. The name is changed within a transaction with the RestoreValues property set to true. The transaction is then rolled back, followed by the name is being accessed outside the transaction:

Code

State Change

Copy Code
scope.Transaction.Properties.
  RestoreValues = true;

set the RestoreValues transaction property to true

Copy Code
scope.Transaction.Begin();

begin the transaction

Copy Code
Customer c = GetCustomer( . . . );

a Customer instance, c, is retrieved from the database in the "hollow" state

Copy Code
c.Name = "Jane";

a persistent field of c is modified and the object state is moved to "persistent dirty"

Copy Code
scope.Transaction.Rollback();

c is not stored; the RestoreValues = true setting means the object is moved to the "persistent nontransactional" state and the original object values are restored

Copy Code
Console.WriteLine( c.Name );

access outside of the transaction is allowed; prints " Alex" (i.e., the original value from the database)