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 (5)

  1. Ivolved says:

    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.

  2. George Mavritsakis says:

    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!

  3. Stoune says:

    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.

  4. Stoune says:

    I post updated variant into http://paste.pocoo.org/show/131885/

    I simply add a trivial method for closing connection.

  5. [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;