<?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|Activation|Transparent Activation|Transparent Activation Pitfalls" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Migrating Between Databases </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/activation_concept/transparent_activation_framework/transparent_activation/migrating_between_databases.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="../../../activation.htm">Activation</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../../transparent_activation_framework.htm">Transparent Activation</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Migrating Between Databases</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>Migrating Between Databases</h1> <p>Transparent activation and persistence functionality depends on an association between an object and an object container, which is created when an activator is bound to the object. Each object allows only one activator. Typically this limitation won't show up, however there is a valid use case for it: </p> <p>1) suppose you need to copy one or more objects from one object container to another;</p> <p>2) you will retrieve the object(s) from the first object container using any suitable query syntax;</p> <p>3) optionally you can close the first object container;</p> <p>4) you will now save the object to the second object container.</p> <p>If both object containers were using transparent activation or persistence - the 4-th step will throw an exception. Let's look at the case in more detail. Typical activatable class contains an <code>activator</code> field. When transparent activation functionality is used for the first time an object container activator will be bound to the object:</p> <p MadCap:conditions="Global.Primary:java" /> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <pre class="prettyprint" xml:space="preserve">SensorPanelTA.cs: Bind /**//*Bind the class to the specified object container, create the activator*/ public void Bind(IActivator activator) { if (_activator == activator) { return; } if (activator != null && null != _activator) { throw new System.InvalidOperationException(); } _activator = activator; }</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">SensorPanelTA.vb: Bind ' Bind the class to the specified object container, create the activator Public Sub Bind(ByVal activator As IActivator) Implements IActivatable.Bind If _activator Is activator Then Return End If If Not (activator Is Nothing Or _activator Is Nothing) Then Throw New System.InvalidOperationException() End If _activator = activator End Sub</pre> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>If <code>bind</code> method will be re-called with the same object container, activator parameter will always be the same. However, if another object container tries to bind the object (in our case with the <code>store</code> call) activator parameter will be different, which will cause an exception. (Exception will be thrown even if the first object container is already closed, as activator object still exists in the memory.) This behaviour is illustrated with the following example (<a href="../detailed_example.htm">SensorPanelTA</a> class from Transparent <span class="MCPopup"><a href="javascript:void(0);" class="MCPopupSpot" onclick="FMCPopup( event, this ); return false;" MadCap:src="../../../activation.htm">Activation<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> chapter is used):</p> <p MadCap:conditions="Global.Primary:java" /> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <pre class="prettyprint" xml:space="preserve">TAExample.cs: TestSwitchDatabases private static void TestSwitchDatabases() { StoreSensorPanel(); IObjectContainer firstDb = Db4oFactory.OpenFile(ConfigureTA(), FirstDbName); IObjectContainer secondDb = Db4oFactory.OpenFile(ConfigureTA(), SecondDbName); try { IObjectSet result = firstDb.QueryByExample(new SensorPanelTA(1)); if (result.Count > 0) { SensorPanelTA sensor = (SensorPanelTA)result[0]; firstDb.Close(); // Migrating an object from the first database // into a second database secondDb.Store(sensor); } } finally { firstDb.Close(); secondDb.Close(); } }</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">TAExample.vb: TestSwitchDatabases Private Shared Sub TestSwitchDatabases() StoreSensorPanel() Dim firstDb As IObjectContainer = _ Db4oFactory.OpenFile(ConfigureTA(), FirstDbName) Dim secondDb As IObjectContainer = _ Db4oFactory.OpenFile(ConfigureTA(), SecondDbName) Try Dim result As IObjectSet = _ firstDb.QueryByExample(New SensorPanelTA(1)) If result.Count > 0 Then Dim sensor As SensorPanelTA = _ DirectCast(result(0), SensorPanelTA) firstDb.Close() ' Migrating an object from the first database ' into a second database secondDb.Store(sensor) End If Finally firstDb.Close() secondDb.Close() End Try End Sub</pre> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>The solution to this problem is simple: activator should be unbound from the object:</p> <p MadCap:conditions="Global.Primary:java" /> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <p>c#:</p> <p><code>sensor.Bind(null);</code> </p> </p> <p MadCap:conditions="Global.Primary:cs" /> <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages"> <p>VB:</p> <p><code>sensor.Bind(Nothing)</code> </p> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>Note, that the object will quit being activatable for the first object container. The following example shows the described behaviour:</p> <p MadCap:conditions="Global.Primary:java" /> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <pre class="prettyprint" xml:space="preserve">TAExample.cs: TestSwitchDatabasesFixed private static void TestSwitchDatabasesFixed() { StoreSensorPanel(); IObjectContainer firstDb = Db4oFactory.OpenFile(ConfigureTA(), FirstDbName); IObjectContainer secondDb = Db4oFactory.OpenFile(ConfigureTA(), SecondDbName); try { IObjectSet result = firstDb.QueryByExample(new SensorPanelTA(1)); if (result.Count > 0) { SensorPanelTA sensor = (SensorPanelTA)result[0]; // Unbind the object from the first database sensor.Bind(null); // Migrating the object into the second database secondDb.Store(sensor); System.Console.WriteLine("Retrieving previous query results from " + FirstDbName + ":"); SensorPanelTA next = sensor.Next; while (next != null) { System.Console.WriteLine(next); next = next.Next; } System.Console.WriteLine("Retrieving previous query results from " + FirstDbName + " with manual activation:"); firstDb.Activate(sensor, Int32.MaxValue); next = sensor.Next; while (next != null) { System.Console.WriteLine(next); next = next.Next; } System.Console.WriteLine("Retrieving sensorPanel from " + SecondDbName + ":"); result = secondDb.QueryByExample(new SensorPanelTA(1)); next = sensor.Next; while (next != null) { System.Console.WriteLine(next); next = next.Next; } } } finally { firstDb.Close(); secondDb.Close(); } }</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">TAExample.vb: TestSwitchDatabasesFixed Private Shared Sub TestSwitchDatabasesFixed() StoreSensorPanel() Dim firstDb As IObjectContainer = _ Db4oFactory.OpenFile(ConfigureTA(), FirstDbName) Dim secondDb As IObjectContainer = _ Db4oFactory.OpenFile(ConfigureTA(), SecondDbName) Try Dim result As IObjectSet = _ firstDb.QueryByExample(New SensorPanelTA(1)) If result.Count > 0 Then Dim sensor As SensorPanelTA = _ DirectCast(result(0), SensorPanelTA) ' Unbind the object from the first database sensor.Bind(Nothing) ' Migrating the object into the second database secondDb.Store(sensor) System.Console.WriteLine( _ "Retrieving previous query results from " + FirstDbName + ":") Dim [next] As SensorPanelTA = sensor.NextSensor While [next] IsNot Nothing System.Console.WriteLine([next]) [next] = [next].NextSensor End While System.Console.WriteLine( _ "Retrieving previous query results from " + FirstDbName + _ " with manual activation:") firstDb.Activate(sensor, Int32.MaxValue) [next] = sensor.NextSensor While [next] IsNot Nothing System.Console.WriteLine([next]) [next] = [next].NextSensor End While System.Console.WriteLine( _ "Retrieving sensorPanel from " + SecondDbName + ":") result = secondDb.QueryByExample(New SensorPanelTA(1)) [next] = sensor.NextSensor While [next] IsNot Nothing System.Console.WriteLine([next]) [next] = [next].NextSensor End While End If Finally firstDb.Close() secondDb.Close() End Try End Sub</pre> </p> <p MadCap:conditions="Global.Primary:vb" /> <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="tamigratevb.zip">VB.NET </a> </MadCap:conditionalText> <MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><a href="tamigratecs.zip">c# </a> </MadCap:conditionalText> </p> <script type="text/javascript" src="../../../../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>