You are here: Basics Operations & Concepts > Update Concept > Transparent Persistence > Rollback And Cache

Rollback And Cache

Suppose we have Car, Pilot and Id classes stored in the database. Car class is activatable, others are not. We will modify the car and rollback the transaction:

TPRollback.cs: ModifyAndRollback
private static void ModifyAndRollback()
         {
            IObjectContainer container = Database(ConfigureTP());
            if (container != null)
             {
                try
                 {
                    // create a car
                    Car car = (Car)container.QueryByExample(new Car(null, null))
                            [0];
                    System.Console.WriteLine("Initial car: " + car + "("
                            + container.Ext().GetID(car) + ")");
                    car.Model = "Ferrari";
                    car.Pilot = new Pilot("Michael Schumacher", 123);
                    container.Rollback();
                    System.Console.WriteLine("Car after rollback: " + car + "("
                            + container.Ext().GetID(car) + ")");
                }
                finally
                 {
                    CloseDatabase();
                }
            }
        }

TPRollback.vb: ModifyAndRollback
Private Shared Sub ModifyAndRollback()
            Dim container As IObjectContainer = Database(ConfigureTP())
            If container IsNot Nothing Then
                Try
                    ' create a car
                    Dim car As Car = DirectCast(container. _ 
QueryByExample(New Car(Nothing, Nothing))(0), Car)
                    System.Console.WriteLine("Initial car: " + _ 
car.ToString() + "(" + container.Ext().GetID(car).ToString() + ")")
                    car.Model = "Ferrari"
                    car.Pilot = New Pilot("Michael Schumacher", 123)
                    container.Rollback()
                    System.Console.WriteLine("Car after rollback: " _ 
+ car.ToString() + "(" + _ 
container.Ext().GetID(car).ToString() + ")")
                Finally
                    CloseDatabase()
                End Try
            End If
        End Sub

If the transaction was going on normally (commit), we would have had the car modified in the database as it is supported by Transparent Persistence. However, as the transaction was rolled back - no modifications should be done to the database. The result that is printed to the screen is taken from the reference cache, so it will show modified objects. That is confusing and should be fixed:

TPRollback.cs: ModifyRollbackAndCheck
private static void ModifyRollbackAndCheck()
         {
            IObjectContainer container = Database(ConfigureTP());
            if (container != null)
             {
                try
                 {
                    // create a car
                    Car car = (Car)container.QueryByExample(new Car(null, null))
                            [0];
                    Pilot pilot = car.Pilot;
                    System.Console.WriteLine("Initial car: " + car + "("
                            + container.Ext().GetID(car) + ")");
                    System.Console.WriteLine("Initial pilot: " + pilot + "("
                            + container.Ext().GetID(pilot) + ")");
                    car.Model = "Ferrari";
                    car.ChangePilot("Michael Schumacher", 123);
                    container.Rollback();
                    container.Deactivate(car, Int32.MaxValue);
                    System.Console.WriteLine("Car after rollback: " + car + "("
                            + container.Ext().GetID(car) + ")");
                    System.Console.WriteLine("Pilot after rollback: " + pilot + "("
                            + container.Ext().GetID(pilot) + ")");
                }
                finally
                 {
                    CloseDatabase();
                }
            }
        }

TPRollback.vb: ModifyRollbackAndCheck
Private Shared Sub ModifyRollbackAndCheck()
            Dim container As IObjectContainer = Database(ConfigureTP())
            If container IsNot Nothing Then
                Try
                    ' create a car
                    Dim car As Car = DirectCast(container. _ 
QueryByExample(New Car(Nothing, Nothing))(0), Car)
                    Dim pilot As Pilot = car.Pilot
                    System.Console.WriteLine("Initial car: " _ 
+ car.ToString() + "(" + container.Ext().GetID(car).ToString() + ")")
                    System.Console.WriteLine("Initial pilot: " _ 
+ pilot.ToString() + "(" + _ 
container.Ext().GetID(pilot).ToString() + ")")
                    car.Model = "Ferrari"
                    car.ChangePilot("Michael Schumacher", 123)
                    container.Rollback()
                    container.Deactivate(car, Int32.MaxValue)
                    System.Console.WriteLine("Car after rollback: " _ 
+ car.ToString() + "(" + _ 
container.Ext().GetID(car).ToString() + ")")
                    System.Console.WriteLine("Pilot after rollback: " _ 
+ pilot.ToString() + "(" + _ 
container.Ext().GetID(pilot).ToString() + ")")
                Finally
                    CloseDatabase()
                End Try
            End If
        End Sub

Here we've added a deactivate call for the car object. This call is used to clear the reference cache and its action is reversed to activate.

We've used max int to deactivate car fields to the maximum possible depth. Thus we can be sure that all the car fields will be re-read from the database again (no outdated values from the reference cache), but the trade-off is that all child objects will be deactivated and read from the database too. You can see it on Pilot object. This behaviour is preserved for both activatable and non-activatable objects.

Download example code:

VB.NET c#