{"id":733,"date":"2009-10-15T21:00:32","date_gmt":"2009-10-15T20:00:32","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=733"},"modified":"2021-03-11T09:44:57","modified_gmt":"2021-03-11T08:44:57","slug":"db4o-single-object-container-concurrency","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2009\/10\/db4o-single-object-container-concurrency\/","title":{"rendered":"db4o: Single Object-Container Concurrency"},"content":{"rendered":"<p>Today nearly every application has some concurrent parts. In a classic desktop-application some work is done in the background to keep the application responsive. In a web-application more than one request are handled concurrently. There\u2019s no escape from the challenges of concurrent programs.\u00a0<\/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>Is a Object Container Thread Safe?<\/h3>\n<p>Short answer: Yes it is. You can throw as many threads as you like at a IObjectContainer. A big fat lock protects all operations on the IObjectContainer. It even goes further. You even can grab that lock yourself, but it&#8217;s not recommended!<\/p>\n<div id=\"attachment_734\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-synchonize-container.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-734\" class=\"size-medium wp-image-734\" title=\"db4o-synchonize-container\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-synchonize-container-300x96.png\" alt=\"synchronized object-container\" width=\"300\" height=\"96\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-synchonize-container-300x96.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-synchonize-container.png 1000w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-734\" class=\"wp-caption-text\">synchronized object-container<\/p><\/div>\n<h3>But Your Persistent-Classes Aren\u2019t!<\/h3>\n<p>So now we know that the object-container is thread safe. Hurray, so lets throw a quazillion threads at it =). Again a little demonstration. Since you cannot \u2018control\u2019 concurrent operations I just simulate some interleaving. Such a simulation never ever shows the real effects of race-conditions, but it\u2019s deterministic to demonstrate the effects.<\/p>\n<p>One thread takes the Price and Amount and calculate the sum and stores it. The other thread doubles the Amount and cuts the Price in half. So the sum should be always the same, right? The start-values are: Amount = 2, Price = 4.<\/p>\n<p>So the first interleaving:<\/p>\n<pre class=\"csharpcode\">var toUpdate = GetTheUpdateObject();\r\nInOtherThreadThisRanThrought(\r\n    () =&gt;\r\n    {\r\n        var toUpdateT2 = GetTheUpdateObject();\r\n        toUpdateT2.Amount = toUpdate.Amount * 2;\r\n        toUpdateT2.Price = toUpdate.Price \/ 2;\r\n        db.Store(toUpdateT2);\r\n    });\r\nvar sum = toUpdate.Price;\r\nsum = sum * toUpdate.Amount;\r\ntoUpdate.Sum = sum;\r\nConsole.Out.WriteLine(<span class=\"str\">\"Sum is=\"<\/span>+toUpdate.Sum+<span class=\"str\">\" Expected is 8\"<\/span>);\r\ndb.Store(toUpdate);<\/pre>\n<p>The second interleaving:<\/p>\n<pre class=\"csharpcode\">var toUpdate = GetTheUpdateObject();\r\nvar sum = toUpdate.Price;\r\nInOtherThreadThisRanThrought(\r\n    () =&gt;\r\n    {\r\n        var toUpdateT2 = GetTheUpdateObject();\r\n        toUpdateT2.Amount = toUpdate.Amount * 2;\r\n        toUpdateT2.Price = toUpdate.Price \/ 2;\r\n        db.Store(toUpdateT2);\r\n    });\r\nsum = sum * toUpdate.Amount;\r\ntoUpdate.Sum = sum;\r\nConsole.Out.WriteLine(<span class=\"str\">\"Sum is=\"<\/span> + toUpdate.Sum + <span class=\"str\">\" Expected is 8\"<\/span>);\r\ndb.Store(toUpdate);<\/pre>\n<p>The first interleaving will return the correct result. The second one will return 16, because between the reads of \u2018Price\u2019 and \u2018Amount\u2019 the \u2018Amount\u2019 was changed. (As you remember from previous posts, the object-container caches the objects and returns always the same instances)<\/p>\n<p>Well the object-container is thread-safe, but actually that doesn\u2019t do much for us.<\/p>\n<div id=\"attachment_735\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-concurrent-obj-manipulation.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-735\" class=\"size-medium wp-image-735\" title=\"db4o-concurrent-obj-manipulation\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-concurrent-obj-manipulation-300x104.png\" alt=\"non-synchronized object manipulation\" width=\"300\" height=\"104\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-concurrent-obj-manipulation-300x104.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-concurrent-obj-manipulation-1024x356.png 1024w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-concurrent-obj-manipulation.png 1200w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-735\" class=\"wp-caption-text\">non-synchronized object manipulation<\/p><\/div>\n<h3>Ok, I Just Synchronize The Persisted Classes<\/h3>\n<p>Ok, we could synchronize all properties and methods on the persisted classes. But it won\u2019t fix the problem above. Since the race-condition happens between the reads of the two properties.<\/p>\n<h3>Keep It Simple Solution, No Concurrent Operations<\/h3>\n<p>Well the simplest solution is to avoid any concurrency on the data-operations. So you never ever touch your persistent objects and database concurrently. This actually will work quite good in a simple single-user CRUD-Application.<\/p>\n<p>However, as soon as your operations on the database and persistent objects takes more time, this isn\u2019t a good solution. (the classic UI-freezes).<\/p>\n<h3>Big Fat Lock Solution<\/h3>\n<p>This another quite obvious solution. You create a Big-Fat-Lock which protects all operations on the db and persistent objects. So you can ensure that everything stays consistent:<\/p>\n<p>lock(dbLock)<\/p>\n<p>load and store persistent objects<\/p>\n<p>do work with persistent objects<\/p>\n<p>release(dbLock)<\/p>\n<p>Yes, it\u2019s still pretty non-concurrent, but it\u2019s enough for lots of applications. It works fine as long the Application is IO-bound anyway and you don\u2019t to computation-intensive stuff.<\/p>\n<p>This is the way I do it in my application. It allows to run some operations in the background without blocking the rest. Here comes the closure-style API handy:<\/p>\n<pre class=\"csharpcode\">dataBase.InTransaction(\r\n    tx=&gt;{\r\n        db.Store(<span class=\"kwrd\">new<\/span> DemoObject());\r\n    });<\/pre>\n<p>The InTransaction-Method ensures that the the transaction is either committed or rolled back and that the Big-Fat-Lock is hold. Now I do every manipulation on your persistent-object in such a closure.<\/p>\n<p>Pay attention how you use your persistent-object. For example when you use data-binding (WPF-Binding, Beans-Binding in JAVA). Such binding-mechanisms won\u2019t grab your db-lock. So If you want to just bind the persistent object to the GUI this approach won\u2019t work.<\/p>\n<div id=\"attachment_736\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-big-fat-lock.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-736\" class=\"size-medium wp-image-736\" title=\"db4o-big-fat-lock\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-big-fat-lock-300x188.png\" alt=\"big fat lock\" width=\"300\" height=\"188\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-big-fat-lock-300x188.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-big-fat-lock.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-736\" class=\"wp-caption-text\">big fat lock<\/p><\/div>\n<h3>Finer Grained Mechanisms<\/h3>\n<p>The solution above a pretty simple, but won\u2019t scale a at all. When you really need more performance you need finer grained lock mechanisms. Like per class-lock, per object-lock or whatever strategies fits your usage.<\/p>\n<h3>But What About Client-Server?<\/h3>\n<p>So far I\u2019ve talked about stuff isn\u2019t that special to db4o and I assumed that there\u2019s only one object-container around. But what about the db4o client-server-model, which has more than one object-container? I haven\u2019t lost a word about it. So that is the topic of the upcoming blog-post =)<\/p>\n<h3>100th Post<\/h3>\n<p>Just noticed that this is the 100th post on my blog. Yuppie =)<\/p>\n<div id=\"attachment_742\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/100th-post.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-742\" class=\"size-medium wp-image-742\" title=\"100th-post\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/100th-post-300x119.png\" alt=\"100th post\" width=\"300\" height=\"119\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/100th-post-300x119.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/100th-post-1024x409.png 1024w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/100th-post.png 1061w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-742\" class=\"wp-caption-text\">100th post<\/p><\/div>\n<p>Demo-Files: <a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/Program.cs\">InterleavingDemo.cs<\/a>, <a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/SimpleObject.cs\">SimpleObject.cs<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today nearly every application has some concurrent parts. In a classic desktop-application some work is done in the background to keep the application responsive. In a web-application more than one&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\/733"}],"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=733"}],"version-history":[{"count":7,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/733\/revisions"}],"predecessor-version":[{"id":3830,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/733\/revisions\/3830"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}