<?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>Automatic Deactivation </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="../../../../../SkinSupport/MadCapAll.js"> </script> <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> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../../../../index_CSH.html#basics/update_concept/transparent_persistence/transparent_persistence/rollback_strategies/automatic_deactivation.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">Automatic Deactivation</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>Automatic Deactivation</h1> <p> </p> <p>The use of depth parameter in <code>deactivate</code> call from the <a href="rollback_and_cache.htm">previous example</a> directly affects performance: the less is the depth the less objects will need to be re-read from the database and the better the performance will be. Ideally we only want to deactivate the objects that were changed in the rolled-back transaction. This can be done by providing a special class for db4o configuration. This class should implement <code>RollbackStrategy/IRollbackStrategy</code> interface and is configured as part of Transparent Persistence support:</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: RollbackDeactivateStrategy private class RollbackDeactivateStrategy : IRollbackStrategy { public void Rollback(IObjectContainer container, Object obj) { container.Ext().Deactivate(obj); } }</pre> <pre class="prettyprint" xml:space="preserve">TPRollback.cs: ConfigureTPForRollback private static IConfiguration ConfigureTPForRollback() { IConfiguration configuration = Db4oFactory.NewConfiguration(); // add <span class="MCTextPopup"><a href="javascript:void(0);" class="MCTextPopupSpot" onclick="FMCTextPopup( event, this ); return false;">TP<img style="border: none;margin-left: 5px;" src="../../../../../SkinSupport/ExpandingClosed.gif" MadCap:altsrc="../../../../../SkinSupport/ExpandingOpen.gif" class="MCExpandingIcon" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../../../../SkinSupport/ExpandingOpen.gif' ); }" /></a><span class="MCTextPopupBody" style="display: none; ">Transparent Persistence</span></span> support and rollback strategy configuration.Add(new TransparentPersistenceSupport( new RollbackDeactivateStrategy())); return configuration; }</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: RollbackDeactivateStrategy Private Class RollbackDeactivateStrategy Implements IRollbackStrategy Public Sub Rollback(ByVal container As IObjectContainer, _ ByVal obj As Object) _ Implements IRollbackStrategy.Rollback container.Ext().Deactivate(obj) End Sub End Class</pre> <pre class="prettyprint lang-vb" xml:space="preserve">TPRollback.vb: ConfigureTPForRollback Private Shared Function ConfigureTPForRollback() As IConfiguration Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() ' add TP support and rollback strategy configuration.Add(New TransparentPersistenceSupport _ (New RollbackDeactivateStrategy())) Return configuration End Function</pre> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>RollbackDeactivateStrategy#rollback method will be automatically called <b>once</b> per each <b>modified</b> object after the rollback. Thus you do not have to worry about deactivate depth anymore - all necessary deactivation will happen transparently preserving the best performance possible. </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: ModifyWithRollbackStrategy private static void ModifyWithRollbackStrategy() { IObjectContainer container = Database(ConfigureTPForRollback()); 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(); 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: ModifyWithRollbackStrategy Private Shared Sub ModifyWithRollbackStrategy() Dim container As IObjectContainer = Database(ConfigureTPForRollback()) 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() 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>Note, that RollbackDeactivateStrategy <b>only works for activatable</b> objects. To see the different you can comment out Activatable implementation in Id class (id value will be preserved in the cache).</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>