Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 2e9c43658e374d290a2de15d25134ac8 > files > 953

db4o-doc-8.0-1.fc15.i686.rpm

<?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="Tuning|Utility Methods" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>PeekPersisted	</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#tuning/utility_methods/peekpersisted.htm" style="">Open topic with navigation</a>
        </p>
        <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../tuning.htm">Tuning</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="../utility_methods.htm">Utility Methods</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">peekpersisted</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>PeekPersisted</h1>
        <p>Db4o loads each object into reference cache only once in the session, thus ensuring that independently of the way of retrieving, you will always get a reference to the same object. This concept certainly makes things clearer, but in some cases you will want to operate on the copy of an object.
</p>
        <p>Typical usecases can be:</p>
        <ul>
            <li value="1">comparing object's changes in a running transaction with the original object in a database;</li>
            <li value="2">safely changing an object without making changes to the database;</li>
            <li value="3">modifying an object in several threads independently, writing the changes to the database after conflict resolution.</li>
        </ul>
        <p>Db4o helps you with these tasks providing the following method:</p>
        <p MadCap:conditions="Global.Primary:java" />
        <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages">
            <p>c#:   <code>IExtObjectContainer.PeekPersisted(object, depth, committed)</code></p>
        </p>
        <p MadCap:conditions="Global.Primary:cs" />
        <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages">
            <p>VB:   <code>IExtObjectContainer.PeekPersisted(object, depth, committed)</code></p>
        </p>
        <p MadCap:conditions="Global.Primary:vb" />
        <p>This method creates a copy of a database object in memory instantiating its members up to depth parameter value. The object has no connection to the database.</p>
        <p>Committed parameter defines whether committed or set values are to be returned. 
Let's see how you can use it.</p>
        <p>We will use 2 threads measuring temperature independently in different parts of the car: somewhere in the cabin (getCabinTemperature) and on the conditioner unit (getConditionerTemperature).After some period of time the average measured value will be written to the database.</p>
        <p MadCap:conditions="Global.Primary:java" />
        <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages">
            <pre class="prettyprint" xml:space="preserve">PeekPersistedExample.cs: MeasureCarTemperature
private static void MeasureCarTemperature()
     {
      SetObjects();
      IObjectContainer db = Db4oFactory.OpenFile(Db4oFileName);
      try 
       {
        IObjectSet result = db.QueryByExample(typeof(Car));
        if (result.Size() &gt; 0)
         {
          Car car = (Car)result[0];
          Car car1  = (Car)db.Ext().PeekPersisted(car, 5, true);
          Change1 ch1 = new Change1();
          ch1.Init(car1);
          Car car2  = (Car)db.Ext().PeekPersisted(car, 5, true);
          Change2 ch2 = new Change2();
          ch2.Init(car2);
          Thread.Sleep(300);
          // We can work on the database object at the same time
          car.Model = "BMW M3Coupe";
          db.Store(car);
          ch1.Stop();
          ch2.Stop();
          System.Console.WriteLine("car1 saved to the database: " 
+ db.Ext().IsStored(car1));
          System.Console.WriteLine("car2 saved to the database: " 
+ db.Ext().IsStored(car1));
          int temperature = (int)((car1.Temperature + car2.Temperature)/2);
          car.Temperature = temperature;
          db.Store(car);
        }
      } 
      finally 
       {
        db.Close();
      }
      �heckCar();
    }</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">PeekPersistedExample.vb: MeasureCarTemperature
Private Shared Sub MeasureCarTemperature()
            SetObjects()
            Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName)
            Try
                Dim result As IObjectSet = db.QueryByExample(GetType(Car))
                If result.Size() &gt; 0 Then
                    Dim car As Car = CType(result(0), Car)
                    Dim car1 As Car = CType(db.Ext().PeekPersisted(car, 5, True), Car)
                    Dim ch1 As Change1 = New Change1()
                    ch1.Init(car1)
                    Dim car2 As Car = CType(db.Ext().PeekPersisted(car, 5, True), Car)
                    Dim ch2 As Change2 = New Change2()
                    ch2.Init(car2)
                    Thread.Sleep(300)
                    ' We can work on the database object at the same time
                    car.Model = "BMW M3Coupe"
                    db.Store(car)
                    ch1.Kill()
                    ch2.Kill()
                    System.Console.WriteLine("car1 saved to the database: " _ 
+ db.Ext().IsStored(car1).ToString())
                    System.Console.WriteLine("car2 saved to the database: " _ 
+ db.Ext().IsStored(car1).ToString())
                    Dim temperature As Integer = CType(((car1.Temperature _ 
+ car2.Temperature) / 2), Integer)
                    car.Temperature = temperature
                    db.Store(car)
                End If
            Finally
                db.Close()
            End Try
            CheckCar()
        End Sub</pre>
        </p>
        <p MadCap:conditions="Global.Primary:vb" />
        <p>peekPersisted method gives you an easy way to work with database objects' clones. Remember that these clones are totally disconnected from the database. If you will try to save such object you will get a new object in the database.</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="persistvb.zip">VB.NET </a>
            </MadCap:conditionalText>
            <MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><a href="persistcs.zip">c# </a>
            </MadCap:conditionalText>
        </p>
        <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>