Sophie

Sophie

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

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="Advanced Features" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>Session Containers</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#advanced_topics/session_containers.htm" style="">Open topic with navigation</a>
        </p>
        <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../advanced_topics.htm">Advanced Features</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Session Containers</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><a name="kanchor68"></a>Session Containers</h1>
        <p>In an application there are often multiple operations running at the same time. A typical example is a web application which processes multiple requests at the same time. These operations should be isolated from each other. This means that for the database we want to have multiple transactions at the same time. Each transaction does some work and other transactions shouldn't interfere .</p>
        <p>db4o supports this scenario with session containers. A session container is a lightweight object-container with its own transaction and reference cache, but shares the resources with its parent container. That means you can commit and rollback changes on such a session container without disturbing the work of other session containers. If you want to implement units of work, you might considers using a session container for each unit. You can create such a container with the open session call.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">using (IObjectContainer rootContainer = Db4oEmbedded.OpenFile(DatabaseFileName))
{
    // open the db4o-session. For example at the beginning for a web-request
    using (IObjectContainer session = rootContainer.Ext().OpenSession())
    {
        // do the operations on the session-container
        session.Store(new Person("Joe"));
    }
}</pre>
            <div class="codesnippet-meta">Db4oSessions.cs: Session object container
			<div class="codedownload"><a href="../CodeExamples/container/sessions/Example-container-sessions-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Using rootContainer As IObjectContainer = Db4oEmbedded.OpenFile(DatabaseFileName)
    ' open the db4o-session. For example at the beginning for a web-request
    Using session As IObjectContainer = rootContainer.Ext().OpenSession()
        ' do the operations on the session-container
        session.Store(New Person("Joe"))
    End Using
End Using</pre>
            <div class="codesnippet-meta">Db4oSessions.vb: Session object container
			<div class="codedownload"><a href="../CodeExamples/container/sessions/Example-container-sessions-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <h2>Transactions And Isolation</h2>
        <p>As previously mentioned session-containers are isolated from each other. Each session container has its own transaction and its own reference system. This isolation ensures that the different session container don't interfere witch each other. </p>
        <p>They don't share the objects loaded and stored with each other. That means you need to load and store the a object with the same session container. When you try to load a object form one session-container and store it with another, you well end up with two separate copies of that object.</p>
        <p>Since the transactions are isolated, changes are only visible for other session containers when you've committed. Before the commit call the changes are not visible to other session containers.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">session1.Store(new Person("Joe"));
session1.Store(new Person("Joanna"));

// the second session won't see the changes until the changes are committed
PrintAll(session2.Query&lt;Person&gt;());

session1.Commit();

// new the changes are visiable for the second session
PrintAll(session2.Query&lt;Person&gt;());</pre>
            <div class="codesnippet-meta">Db4oSessions.cs: Session are isolated from each other
			<div class="codedownload"><a href="../CodeExamples/container/sessions/Example-container-sessions-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">session1.Store(New Person("Joe"))
session1.Store(New Person("Joanna"))

' the second session won't see the changes until the changes are committed
PrintAll(session2.Query(Of Person)())

session1.Commit()

' new the changes are visiable for the second session
PrintAll(session2.Query(Of Person)())</pre>
            <div class="codesnippet-meta">Db4oSessions.vb: Session are isolated from each other
			<div class="codedownload"><a href="../CodeExamples/container/sessions/Example-container-sessions-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p>Note also that sessions also have their own reference cache. So when a object is already loaded, it wont be refreshed if another transaction updates the object. You explicitly need to refresh it.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">Person personOnSession1 = session1.Query&lt;Person&gt;()[0];
Person personOnSession2 = session2.Query&lt;Person&gt;()[0];

personOnSession1.Name = "NewName";
session1.Store(personOnSession1);
session1.Commit();


// the second session still sees the old value, because it was cached
Console.WriteLine(personOnSession2.Name);
// you can explicitly refresh it
session2.Ext().Refresh(personOnSession2,int.MaxValue);
Console.WriteLine(personOnSession2.Name);</pre>
            <div class="codesnippet-meta">Db4oSessions.cs: Each session does cache the objects
			<div class="codedownload"><a href="../CodeExamples/container/sessions/Example-container-sessions-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim personOnSession1 As Person = session1.Query(Of Person)()(0)
Dim personOnSession2 As Person = session2.Query(Of Person)()(0)

personOnSession1.Name = "NewName"
session1.Store(personOnSession1)
session1.Commit()


' the second session still sees the old value, because it was cached
Console.WriteLine(personOnSession2.Name)
' you can explicitly refresh it
session2.Ext().Refresh(personOnSession2, Integer.MaxValue)
Console.WriteLine(personOnSession2.Name)</pre>
            <div class="codesnippet-meta">Db4oSessions.vb: Each session does cache the objects
			<div class="codedownload"><a href="../CodeExamples/container/sessions/Example-container-sessions-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <script type="text/javascript" src="../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>