{"id":637,"date":"2009-09-22T22:46:50","date_gmt":"2009-09-22T21:46:50","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=637"},"modified":"2021-03-11T09:44:58","modified_gmt":"2021-03-11T08:44:58","slug":"db4o-activation-update-depth","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2009\/09\/db4o-activation-update-depth\/","title":{"rendered":"db4o: Activation- &amp; Update-Depth"},"content":{"rendered":"<p><a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/net35\/reference\/html\/reference\/object_lifecycle\/activation.html\">Activation<\/a> is a very basic concept in db4o. In this post I\u2019ll illustrate what it is, why it\u2019s there and how you handle it.<\/p>\n<p>(All posts of this series: <a href=\"?p=620\">the basics<\/a>, <a href=\"?p=637\">activation<\/a>, <a href=\"?p=654\">object-identity<\/a>, <a href=\"?p=671\">transactions<\/a>, <a href=\"?p=695\">persistent classes<\/a>, <a href=\"?p=733\">single container concurrency<\/a>, <a href=\"?p=744\">Queries in Java, C# 2.0<\/a>, <a href=\"?p=779\">client-server concurrency<\/a>, <a href=\"?p=840\">transparent persistence<\/a>, <a href=\"?p=949\">adhoc query tools<\/a>)<\/p>\n<h3>The Mysterious Null-Pointer<\/h3>\n<p>Let\u2019s start with a mysterious exception! With the knowledge of the <a href=\"?p=620\">previous post<\/a> we store some objects. This time a person has a property which refers to the person\u2019s boss. We store six instances. Each person is the boss of another one.<\/p>\n<pre class=\"csharpcode\">var ceo = <span class=\"kwrd\">new<\/span> Person(<span class=\"str\">\"Julius\"<\/span>, <span class=\"str\">\"Caesar\"<\/span>);\r\nvar firstGeneral = <span class=\"kwrd\">new<\/span> Person(<span class=\"str\">\"NoIdea\"<\/span>, <span class=\"str\">\"WhoCareus\"<\/span>, ceo);\r\nvar secondGeneral = <span class=\"kwrd\">new<\/span> Person(<span class=\"str\">\"Master\"<\/span>, <span class=\"str\">\"Chiefius\"<\/span>, firstGeneral);\r\nvar captain = <span class=\"kwrd\">new<\/span> Person(<span class=\"str\">\"Biggest\"<\/span>, <span class=\"str\">\"Halo-Geekius\"<\/span>, secondGeneral);\r\nvar officer = <span class=\"kwrd\">new<\/span> Person(<span class=\"str\">\"Plus\"<\/span>, <span class=\"str\">\"Nixus\"<\/span>, captain);\r\nvar normalPerson = <span class=\"kwrd\">new<\/span> Person(<span class=\"str\">\"Mius\"<\/span>, <span class=\"str\">\"Dius\"<\/span>, officer);\r\ncontainer.Store(normalPerson);<\/pre>\n<pre class=\"csharpcode\">\u00a0<\/pre>\n<p>Everything fine so far. Then we decide to write a little method which prints the initials \u201cMius Dius\u201d and all its superiors. It runs fine until it should print the initials of \u201cJulius Caesar\u201d. There a NullReferenceException is thrown. Somehow all the properties of that person are null. But why?<\/p>\n<pre class=\"csharpcode\">\u00a0<\/pre>\n<pre class=\"csharpcode\">var person = (from Person p <span class=\"kwrd\">in<\/span> db\r\n              <span class=\"kwrd\">where<\/span> p.FirstName.Equals(<span class=\"str\">\"Mius\"<\/span>)\r\n              select p).Single();\r\n<span class=\"kwrd\">while<\/span> (<span class=\"kwrd\">null<\/span> != person)\r\n{<\/pre>\n<pre class=\"csharpcode\">    <span class=\"rem\">\/\/ on the last person 'Julius Caesar' properties are null, hence this will fail <\/span>\r\n    Console.Out.WriteLine(\r\n        <span class=\"kwrd\">string<\/span>.Format(<span class=\"str\">\"Persons initials {0}.{1}.\"<\/span>, person.FirstName[0], person.SirName[0]));\r\n    person = person.Boss;\r\n}<\/pre>\n<h3>Activation. What is it? Why is it there?<\/h3>\n<p>As seen above, all properties of \u201cJulius Caesar\u201d are empty. db4o simply hasn&#8217;t loaded the &#8220;Julius Caesar&#8221;-object from the database into memory yet. And there\u2019s a good reason for that. db4o stores object-graphs. Assume you have stored a complex object-graph with thousands of objects. Now you load a single object from the database. Imagine db4o would ensure that all references to other objects are set properly. Then db4o would have to load your whole object-graph (this could be the whole database) into memory.<\/p>\n<div class=\"mceTemp\">\n<div id=\"attachment_639\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-without-activation.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-639\" class=\"size-medium wp-image-639\" title=\"db4o-without-activation\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-without-activation-300x85.png\" alt=\"db4o without activation\" width=\"300\" height=\"85\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-without-activation-300x85.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-without-activation-1024x293.png 1024w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-without-activation.png 1100w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-639\" class=\"wp-caption-text\">db4o without activation \ud83d\ude09<\/p><\/div>\n<p>\u00a0To avoid that, db4o introduces a concept called <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/net35\/reference\/html\/reference\/object_lifecycle\/activation.html\">Activation<\/a>. db4o loads only a part of the object-graph into memory. The fully loaded objects are \u2018activated\u2019. At the \u2018edge\u2019 of the the object-graph are object which are not activated yet. All properties of such objects are not set. To use such a object, you have to \u2018activate\u2019 it first. This will load the missing data from the database.<\/p>\n<p><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-with-activation.png\"><\/a><\/p>\n<div id=\"attachment_640\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-with-activation.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-640\" class=\"size-medium wp-image-640\" title=\"db4o-with-activation\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-with-activation-300x110.png\" alt=\"db4o with activation ;)\" width=\"300\" height=\"110\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-with-activation-300x110.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-with-activation.png 1000w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-640\" class=\"wp-caption-text\">db4o with activation \ud83d\ude09<\/p><\/div>\n<h3>Dealing With Activation<\/h3>\n<p>By default db4o has an activation-depth of 5. That means that the retrieved object and all objects which can be reached over 4 references are activated. If you want more, you can activate an object anytime. You just pass the object and the activation-depth to IObjectContainer.Activate()<\/p>\n<pre class=\"csharpcode\">db.Activate(person,5);<\/pre>\n<pre class=\"csharpcode\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-activate.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-641\" title=\"db4o-activate\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-activate.png\" alt=\"db4o-activate\" width=\"700\" height=\"264\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-activate.png 700w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-activate-300x113.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/pre>\n<p>Its also possible to change the <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/net35\/reference\/html\/reference\/object_lifecycle\/activation\/global_activation_settings.html\">global default<\/a> or change the activation-depth for a <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/net35\/reference\/html\/reference\/object_lifecycle\/activation\/object-specific_activation.html\">specific class<\/a>.<\/p>\n<p>A higher activation-depth makes it more pleasant to work. However it uses more memory and can have a negative impact on the performance. You have to experiment with it.<\/p>\n<h3>Get Rid Of The Activation-Pain<\/h3>\n<p>Well activation can be a pain in the ass. So, witty people came up with a solution: <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/net35\/reference\/html\/reference\/object_lifecycle\/activation\/transparent_activation_framework.html\">Transparent Activation<\/a>. Basically this makes persisted objects aware of their activation-state. As soon as you access a property the first time the object activates itself. You can hand-code Transparent Activation by implementing interfaces or used byte-code-enhancers. I haven\u2019t used Transparent Activation so far, so I can\u2019t share any experience.<\/p>\n<h3>The Lost Update<\/h3>\n<p>So after all this complex activation stuff we update some objects.<\/p>\n<pre class=\"csharpcode\">person.SirName = <span class=\"str\">\"Cool-NewName\"<\/span>;\r\nperson.Boss.SirName = <span class=\"str\">\"Cool-NewBoss\"<\/span>;\r\ndb.Store(person);<\/pre>\n<pre class=\"csharpcode\">\u00a0<\/pre>\n<p>Later we retrieve the same person and read the properties:<\/p>\n<p>Console.Out.WriteLine(person.SirName);<br \/>\nConsole.Out.WriteLine(person.Boss.SirName);<\/p>\n<p>And the console output is this:<\/p>\n<p>Cool-NewName<br \/>\nNixus<\/p>\n<p>Oh noes, the name-change on the boss is lost. Well you can thinks of the reason?<\/p>\n<h3>Update-Depth<\/h3>\n<p>Analogical to the activation-depth there is an <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/net35\/reference\/html\/reference\/object_lifecycle\/update_depth.html\">update-depth<\/a> in db4o. For similar reasons as the activation-depth. By default db4o simple doesn\u2019t traverse the object-graph to look for changes. It only updates objects which was updated explicitly with IObjectContainer.Store(). (This applies only for updating. New objects are always stored completely).<\/p>\n<p>Again you can change this setting globally, for a class or specify it on the Store()-call.<\/p>\n<h3>I\u2019m Really To Lazy For All This Stuff<\/h3>\n<p>Well then you should try <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/net35\/reference\/html\/reference\/usage_pitfalls\/transparent_persistence.html\">Transparent Persistance<\/a>. Basically this is a way to make your object fully database-aware. You store the object once in the database. After that, all changes you make on the object are reflected in the database \u2018by magic\u2019. Again you can implement this manually or use byte-code-enhancers. And again, I haven\u2019t tried it so far.\u00a0<\/p>\n<h3>Next time<\/h3>\n<p>You might wonder how the heck db4o identifies its objects. There are no explicit ids like you use in hibernate &amp; co. Well I\u2019ll explain that next time =)<\/p>\n<p>Example-Source-Code: <a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/Person1.cs\">Person.cs<\/a>, <a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/Program1.cs\">Program.cs<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Activation is a very basic concept in db4o. In this post I\u2019ll illustrate what it is, why it\u2019s there and how you handle it. (All posts of this series: the&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[150],"tags":[21,100],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/637"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=637"}],"version-history":[{"count":10,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/637\/revisions"}],"predecessor-version":[{"id":3838,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/637\/revisions\/3838"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}