SQL CE 3.5 with LINQ to SQL Revisited

A few days ago I made a post about using SQL CE 3.5 with LINQ to SQL.   I described a way to use connection pooling with SQL CE. A gracious blog reader (Mike Brown)  pointed out a way I could make my solution much simpler by using the [ThreadStatic] attribute.  I never heard of this attribute but it is really nifty.  You mark a field with it and then each thread that accesses that field will be accessing a unique instance of it!

 

Continue reading →

Comments

  • Anonymous
    September 29, 2008
    Anytime Matt! Like I said, I've read that there are caveats to using that attribute, but I can't find the original post that mentioned it.

  • Anonymous
    December 20, 2008
    Your post Mike is amazing! I am just wondering, isn't it supposed for the [ThreadStatic] attribute to be applied on a static property? Thanks!

  • Anonymous
    July 31, 2009
    Your second version has a one side issue. I can't close this connections once then I open them.   In my app SCL CE used as custom doc  format and in this case it is possible situation then user start a new document by rewriting old.

  • Anonymous
    July 31, 2009
    I post updated variant into http://paste.pocoo.org/show/131885/ I simply add a trivial method for closing connection.

  • Anonymous
    June 03, 2010
    [ThreadStatic] attribute works only on static fields, documentation says: Indicates that the value of a static field is unique for each thread. For your solution to work, you have to modify this statement: private IDbConnection threadConnection; to this: private static IDbConnection threadConnection;