<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Basics Operations & Concepts|Update Concept|Transparent Persistence|Transparent Persistence Pitfalls|Rollback Strategies" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Rollback And Cache </title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../../../../../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../../../../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../../../../../Resources/Code/prettify.js"> </script> <script src="../../../../../Resources/Code/lang-vb.js"> </script> <script src="../../../../../Resources/Code/jquery.min.js"> </script> <script src="../../../../../Resources/Code/ZeroClipboard.js"> </script> <script src="../../../../../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../../../../index_CSH.html#basics/update_concept/transparent_persistence/transparent_persistence/rollback_strategies/rollback_and_cache.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../../../../basics.htm">Basics Operations & Concepts</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../../../../update_concept.htm">Update Concept</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../../../transparent_persistence.htm">Transparent Persistence</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Rollback And Cache</span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1>Rollback And Cache</h1> <p>Suppose we have <a href="car.htm">Car</a>, <a href="pilot.htm">Pilot</a> and <a href="id.htm">Id</a> classes stored in the database. Car class is activatable, others are not. We will modify the car and rollback the transaction:</p> <p MadCap:conditions="Global.Primary:java" /> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <pre class="prettyprint" xml:space="preserve">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(); } } }</pre> </p> <p MadCap:conditions="Global.Primary:cs" /> <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages"> <pre class="prettyprint lang-vb" xml:space="preserve">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</pre> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>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:</p> <p MadCap:conditions="Global.Primary:java" /> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <pre class="prettyprint" xml:space="preserve">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(); } } }</pre> </p> <p MadCap:conditions="Global.Primary:cs" /> <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages"> <pre class="prettyprint lang-vb" xml:space="preserve">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</pre> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>Here we've added a <code>deactivate</code> call for the car object. This call is used to clear the reference cache and its action is reversed to <code>activate</code>. </p> <p>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.</p> <p MadCap:conditions="Primary.Online">Download example code:</p> <p MadCap:conditions="Primary.Online"> <MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages"><a href="../rollbackvb.zip">VB.NET </a> </MadCap:conditionalText> <MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><a href="../rollbackcs.zip">c# </a> </MadCap:conditionalText> </p> <script type="text/javascript" src="../../../../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>