﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Alois Kraus commented on Find the bug: Accidental code reviews</title><description>I did not much tinker with databases but I would ask the question why the CurrentContext has no set property? 
  
If I ever choose to open another DB I would like to use a new context. Since it is a static class I think that there is currently no way to inject a fresh context to the underlying object chain.
  
  
Just my non educated guess.
  
  
Yours,   
  
    Alois Kraus
  
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment29</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment29</guid><pubDate>Wed, 28 Jul 2010 05:55:22 GMT</pubDate></item><item><title>Scott commented on Find the bug: Accidental code reviews</title><description>Potential Problems:
  
  
1. Bad implementation of a singleton, if indeed that was the intention.
  
2. Has potential to cause problems (data integrity and possible race condition) if the same session uses multiple threads.
  
3. Static implementation will stay in scope and not garbage collect until the thread shuts down.
  
4. Readonly property will not allow a graceful recovery if an exception happens.
  
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment28</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment28</guid><pubDate>Tue, 27 Jul 2010 12:55:25 GMT</pubDate></item><item><title>Gian Maria Ricci commented on Find the bug: Accidental code reviews</title><description>It will use the same EntityObjectContext for the whole application, this can cause problem if multiple thread will access the context. But the error is because this is like using the same session in all the program, what happens if during a flush we have an exception? After an exception in a flush / update we need to change the context, because it is no more usable.
  
  
alk.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment27</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment27</guid><pubDate>Mon, 26 Jul 2010 16:00:50 GMT</pubDate></item><item><title>Giuliano commented on Find the bug: Accidental code reviews</title><description>Ok, All context objects will stay in the cache after the first access, after some time we'll have all database objects in cache(objectcontexts objects). Am I right? I just want to know the right way to do this with objectcontext. On the L2Sql i putted the DataContext in HttpContext.item, it was hardly stressed, more then 5000 updates, inserts per second, 10 users sim and never lost a transaction. Now i wanna port linq 2 sql to EF. Wich one will be the right way or better way?
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment26</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment26</guid><pubDate>Mon, 26 Jul 2010 13:21:01 GMT</pubDate></item><item><title>Gil commented on Find the bug: Accidental code reviews</title><description>With a static context we will get a memory leak since the context is acting as a cache for entities that were returned from the database. Also, it creates a state entry for each entity and additional objects to track entity relationships which of course in the long run will fill the memeory and crush the application.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment25</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment25</guid><pubDate>Mon, 26 Jul 2010 06:48:30 GMT</pubDate></item><item><title>Kelly Stuard commented on Find the bug: Accidental code reviews</title><description>Side note: It's odd that I don't see anything related to the newly-released NHibernate 3.0.0Alpha1.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment24</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment24</guid><pubDate>Mon, 26 Jul 2010 05:32:12 GMT</pubDate></item><item><title>Mike Minutillo commented on Find the bug: Accidental code reviews</title><description>With EF4 the context performs change tracking by keeping a reference to each object it produces so over time you'll end up with every entity in your database in memory.
  
  
Potentially unrelated parts of the app can mess with each other by calling SaveChanges() at inappropriate (unexpected) times persisting changes which might be incomplete. Transactions would be an absolute nightmare.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment23</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment23</guid><pubDate>Mon, 26 Jul 2010 03:17:21 GMT</pubDate></item><item><title>Steve Py commented on Find the bug: Accidental code reviews</title><description>I certainly hope this wasn't an ASP.Net application.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment22</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment22</guid><pubDate>Mon, 26 Jul 2010 03:12:54 GMT</pubDate></item><item><title>Steve Py commented on Find the bug: Accidental code reviews</title><description>So it is....  That just looks so wrong. :)
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment21</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment21</guid><pubDate>Mon, 26 Jul 2010 02:53:33 GMT</pubDate></item><item><title>Joe Campbell commented on Find the bug: Accidental code reviews</title><description>Which implies a double checked locking issue?
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment20</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment20</guid><pubDate>Mon, 26 Jul 2010 02:08:24 GMT</pubDate></item><item><title>Ayende Rahien commented on Find the bug: Accidental code reviews</title><description>Steve
  
  
return _context ?? (_context = new Acme.Entities.EntitiesObjectContext())
  
  
Is equal to:
  
if(_context == null) 
  
 _context = new Acme.Entities.EntitiesObjectContext();
  
return _context;
  
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment19</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment19</guid><pubDate>Sun, 25 Jul 2010 23:38:34 GMT</pubDate></item><item><title>Steve Py commented on Find the bug: Accidental code reviews</title><description>As mentioned above, If their context implementation is an object context then it should not be treated as a Singleton instance. 
  
  
Still I'm pretty sure that modified code doesn't work as they intended (aside from the missing brace) It's probably supposed to be something like: [Namespaces stripped out for clarity]
  
  
public static EntitiesObjectContext CurrentContext
  
{
  
    get 
  
    { 
  
        _context = _context ?? new EntitiesObjectContext();
  
        return _context;
  
     }
  
}
  
  
Additional little unpleasant smells: (Not context specific) Returning concrete types. Locks implementation details in. I'd define an interface for the context and have a factory create a specific concrete instance. Better yet I'd configure an IOC container to resolve it as a dependency of whatever class needed to avoid hand-writing factories.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment18</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment18</guid><pubDate>Sun, 25 Jul 2010 23:36:44 GMT</pubDate></item><item><title>Ayende Rahien commented on Find the bug: Accidental code reviews</title><description>Thilak &amp; gunteman,
  
Precisely!
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment17</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment17</guid><pubDate>Sun, 25 Jul 2010 23:35:37 GMT</pubDate></item><item><title>Ayende Rahien commented on Find the bug: Accidental code reviews</title><description>Shlomo,
  
Fixed, thanks
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment16</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment16</guid><pubDate>Sun, 25 Jul 2010 23:33:05 GMT</pubDate></item><item><title>Ayende Rahien commented on Find the bug: Accidental code reviews</title><description>Greg,
  
Yes, it does.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment15</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment15</guid><pubDate>Sun, 25 Jul 2010 23:32:31 GMT</pubDate></item><item><title>Ayende Rahien commented on Find the bug: Accidental code reviews</title><description>Alberto,
  
You are trying to find a bug in the dark, when it is sitting under the lamp and chuckling.
  
In other words, don't try to find non obvious / complex ones, this one has _glaring_ issues.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment14</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment14</guid><pubDate>Sun, 25 Jul 2010 23:32:14 GMT</pubDate></item><item><title>Ayende Rahien commented on Find the bug: Accidental code reviews</title><description>Configurator,
  
Thread Safety is a problem, sure. 
  
But let us assume that we are talking about single threaded code, it is still wrong, why?
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment13</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment13</guid><pubDate>Sun, 25 Jul 2010 23:31:03 GMT</pubDate></item><item><title>gunteman commented on Find the bug: Accidental code reviews</title><description>The ObjectContext is a Unit of Work, and it's completely bonkers to make it static. Any thread safety or race condition issues are irrelevant in comparison 
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment12</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment12</guid><pubDate>Sun, 25 Jul 2010 21:57:36 GMT</pubDate></item><item><title>Mistertom commented on Find the bug: Accidental code reviews</title><description>&gt; nothing wrong, except that CurrentContext created when app starts
  
  
No, the initialization is lazy : the static constructor is called when a reference to the class is found by the JIT.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment11</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment11</guid><pubDate>Sun, 25 Jul 2010 21:07:22 GMT</pubDate></item><item><title>kostikkv  commented on Find the bug: Accidental code reviews</title><description>Yeah, probably EntitiesObjectContext:System.Data.Objects.ObjectContext and they decided to reuse its instance, but there was some threading problem, ‘cause the app was an ASP.NET app or something.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment10</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment10</guid><pubDate>Sun, 25 Jul 2010 20:08:00 GMT</pubDate></item><item><title>OmariO commented on Find the bug: Accidental code reviews</title><description>Thread unsafe work with shared variable . In such situation it's possible that concurrent clients get diffrent instances of the context class.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment9</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment9</guid><pubDate>Sun, 25 Jul 2010 19:46:03 GMT</pubDate></item><item><title>evereq commented on Find the bug: Accidental code reviews</title><description>Nice :) 
  
a) My # Re to 'configurator':
  
 With code like
  
 public static class ContextHelper {
  
       public static readonly EntitiesObjectContext CurrentContext = new  EntitiesObjectContext();
  
} 
  
nothing wrong, except that CurrentContext created when app starts  instead of "lazy" way from original code with C# "getter"... While sure it's sometimes maybe make sense to create objects right after App start, usually better to put such logic into bootstrappers.. and I never see that contexts (entities contexts) somebody create such a way, i.e. ones after app starts :D and keep forever... but sure as always "it depends" :)
  
b) with original code from famous post author issue that I see, like in most of real world situations, is that programmer probably want CurrentContext to be actually "current", not the one that was created when you first time use CurrentContext in blog post code... :)
  
c) Sure no thread safety at all..  100% agree! but I was think that it's normal to see "warn" sign when you see static fields and no "lock" in the moment when you fill them :) 
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment8</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment8</guid><pubDate>Sun, 25 Jul 2010 19:37:40 GMT</pubDate></item><item><title>kostikkv commented on Find the bug: Accidental code reviews</title><description>It’s not even a singleton.  This code shows that EntitiesObjectContext may have internal/ public constructor.
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment7</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment7</guid><pubDate>Sun, 25 Jul 2010 19:24:59 GMT</pubDate></item><item><title>Wile E. Coyote commented on Find the bug: Accidental code reviews</title><description>this is why I never catch that @#$@ bird
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment6</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment6</guid><pubDate>Sun, 25 Jul 2010 18:26:07 GMT</pubDate></item><item><title>Thilak Nathen commented on Find the bug: Accidental code reviews</title><description>Object contexts are reasonably volatile resources... like NH sessions. Not a good idea to singleton them. They are meant for short lived usages.  
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment5</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment5</guid><pubDate>Sun, 25 Jul 2010 17:24:53 GMT</pubDate></item><item><title>Shlomo commented on Find the bug: Accidental code reviews</title><description>There's a missing closing curly brace, though I assume that's a typo
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment4</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment4</guid><pubDate>Sun, 25 Jul 2010 16:31:53 GMT</pubDate></item><item><title>greg commented on Find the bug: Accidental code reviews</title><description>Does the 2nd half of the ?? Operator even return the correct type?
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment3</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment3</guid><pubDate>Sun, 25 Jul 2010 16:13:11 GMT</pubDate></item><item><title>alberto commented on Find the bug: Accidental code reviews</title><description>Could it be a race condition where more than one context gets created?
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment2</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment2</guid><pubDate>Sun, 25 Jul 2010 15:56:43 GMT</pubDate></item><item><title>configurator commented on Find the bug: Accidental code reviews</title><description>Thread safety, first of all.
  
Second, there are many points against singletons which I'm too lazy to iterate, especially since there are cases where singletons are exactly the right thing.
  
  
What's wrong with
  
public static class ContextHelper {
  
    public static readonly EntitiesObjectContext CurrentContext = new EntitiesObjectContext();
  
} ?
</description><link>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment1</link><guid>http://ayende.com/4561/find-the-bug-accidental-code-reviews#comment1</guid><pubDate>Sun, 25 Jul 2010 15:49:42 GMT</pubDate></item></channel></rss>