<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Matt Manela's Blog : SQL CE</title><link>http://blogs.msdn.com/matt/archive/tags/SQL+CE/default.aspx</link><description>Tags: SQL CE</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>SQL CE 3.5 with LINQ to SQL Revisited</title><link>http://blogs.msdn.com/matt/archive/2008/09/26/sql-ce-3-5-with-linq-to-sql-revisited.aspx</link><pubDate>Fri, 26 Sep 2008 18:26:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8966589</guid><dc:creator>MattManela</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/matt/comments/8966589.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=8966589</wfw:commentRss><description>&lt;p&gt;A few days ago I &lt;a href="http://blogs.msdn.com/matt/archive/2008/09/09/sql-ce-3-5-with-linq-to-sql.aspx"&gt;made a post&lt;/a&gt; about using &lt;a href="http://blogs.msdn.com/matt/archive/2008/09/09/sql-ce-3-5-with-linq-to-sql.aspx"&gt;SQL CE 3.5 with LINQ to SQL&lt;/a&gt;.&amp;#160;&amp;#160; I described a way to use connection pooling with SQL CE. A gracious blog reader (Mike Brown)&amp;#160; pointed out a way I could make my solution much simpler by using the [&lt;a href="http://msdn.microsoft.com/en-us/library/system.threadstaticattribute.aspx"&gt;ThreadStatic&lt;/a&gt;] attribute.&amp;#160; I never heard of this attribute but it is really nifty.&amp;#160; You mark a field with it and then each thread that accesses that field will be accessing a unique instance of it!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Using this attribute my code for connection pooling went from this:&lt;/p&gt;  &lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; height: 246px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;   &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color: #008000"&gt;/// Manages open connections on a per-thread basis&lt;/span&gt;
&lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ConnectionPool
{
    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, IDbConnection&amp;gt; threadConnectionMap;
 
    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// Gets the connection string.&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;value&amp;gt;The connection string.&amp;lt;/value&amp;gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; ConnectionString
    {
        get;
        &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; set;
    }
 
    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// Gets a connection.&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;returns&amp;gt;An open connection&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IDbConnection Connection
    {
        get
        {
            &lt;span style="color: #0000ff"&gt;lock&lt;/span&gt; (threadConnectionMap)
            {
                &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; threadId = Threading.Thread.CurrentThread.ManagedThreadId;
 
                IDbConnection connection = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (threadConnectionMap.ContainsKey(threadId))
                {
                    connection = threadConnectionMap[threadId];
                }
                &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;
                {
                    connection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SqlCeConnection(ConnectionString);
                    connection.Open();
                    threadConnectionMap.Add(threadId, connection);
                }
 
                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; connection;
            }
        }
    }
 
    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// Initializes a new instance of the &amp;lt;see cref=&amp;quot;ConnectionPool&amp;quot;/&amp;gt; class.&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;connectionString&amp;quot;&amp;gt;The connection string.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ConnectionPool(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; connectionString)
    {
        threadConnectionMap = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, IDbConnection&amp;gt;();
        ConnectionString = connectionString;
    }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;to this:&lt;/p&gt;

&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;
  &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// Manages open connections on a per-thread basis&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ConnectionPool
    {
        [ThreadStatic]
        &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; IDbConnection threadConnection;

        &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color: #008000"&gt;/// Gets the connection string.&lt;/span&gt;
        &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color: #008000"&gt;/// &amp;lt;value&amp;gt;The connection string.&amp;lt;/value&amp;gt;&lt;/span&gt;
        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; ConnectionString
        {
            get;
            &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; set;
        }

        &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color: #008000"&gt;/// Gets a connection.&lt;/span&gt;
        &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color: #008000"&gt;/// &amp;lt;returns&amp;gt;An open connection&amp;lt;/returns&amp;gt;&lt;/span&gt;
        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IDbConnection Connection
        {
            get
            {
                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (threadConnection == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
                {
                    threadConnection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SqlCeConnection(ConnectionString);
                    threadConnection.Open();
                }

                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; threadConnection;
            }
        }

        &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color: #008000"&gt;/// Initializes a new instance of the &amp;lt;see cref=&amp;quot;ConnectionPool&amp;quot;/&amp;gt; class.&lt;/span&gt;
        &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;connectionString&amp;quot;&amp;gt;The connection string.&amp;lt;/param&amp;gt;&lt;/span&gt;
        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ConnectionPool(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; connectionString)
        {
            ConnectionString = connectionString;
        }
    }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Very nice and clean.&amp;#160; &lt;/p&gt;

&lt;p&gt;Thanks Mike!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8966589" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/matt/archive/tags/SQL+CE/default.aspx">SQL CE</category></item><item><title>SQL CE 3.5 with LINQ to SQL</title><link>http://blogs.msdn.com/matt/archive/2008/09/09/sql-ce-3-5-with-linq-to-sql.aspx</link><pubDate>Wed, 10 Sep 2008 00:04:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8937427</guid><dc:creator>MattManela</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/matt/comments/8937427.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=8937427</wfw:commentRss><description>&lt;p&gt;Using LINQ to SQL with SQL CE 3.5 can be a bit of a challenge.&amp;#160; First off, the LINQ to SQL Visual Studio designer doesn't support SQL CE so you need to run &lt;a href="http://msdn.microsoft.com/en-us/library/bb386987.aspx"&gt;sqlmetal&lt;/a&gt; from the command line to create the object model (or write it by hand).&amp;#160; Once you get past this point then you can use LINQ to SQL the same way you would for SQL Sever BUT there is a catch.&lt;/p&gt;  &lt;p&gt;The way LINQ to SQL is built makes it work well with SQL Server and its connection pooling ability.&amp;#160; If you look at this &lt;a href="http://msdn.microsoft.com/en-us/library/bb386929.aspx"&gt;FAQ&lt;/a&gt; under the &amp;quot;Database Connection: Open How Long?&amp;quot; section it says:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;A connection typically remains open until you consume the query results.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Therefore given the following code:&lt;/p&gt;  &lt;div&gt;   &lt;div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;     &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;using&lt;/span&gt;(Northwind db = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Northwind(MyConnectionString))&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;     (from p &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; db.Products).ToList();&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt;     (from p &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; db.Customers).ToList();&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;On line 3 and 4 in the above code will open a database connection (which will be pulled from the open connections in connection pool), execute the query, and then close the connection after the operation is completed (which will return the connection to the pool).&amp;#160; &lt;/p&gt;

&lt;p&gt;This works great with connection pooling but when you move to SQL CE you don't have that luxury.&amp;#160; What happens now is that for each query a new SQL CE connection will be opened, the query will be executed and then the connection is closed.&amp;#160; Each query is incurring the cost of opening a new connection.&amp;#160; To make matters even worse, in SQL CE the first open connection&amp;#160; brings the database engine into memory, and once you have no open connections anymore it removes the engine from memory.&amp;#160; What this means is that each time we close the only open connection and then re-open we are incurring a HUGE cost.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;One way to get around this is to pass into the DataContext an open SQLCeConnection object.&amp;#160; LINQ to SQL will only automatically close a connection if it opens it.&amp;#160; Therefore, if you pass it an open connection then you won't incur this cost over and over again. This will work fine in a single threaded application but once you move to a mutlithreaded app where you are performing database operations from different threads you encounter a problem: The SQLCeConnection object is not thread safe.&amp;#160; You need to have a different connection object per thread in order to make this work.&amp;#160; What you want is to be able to request a connection from any thread and get an already opened one for that thread.&amp;#160; This sounds a lot like a simple connection pooler, which could look something like this:&lt;/p&gt;

&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; height: 246px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;
  &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span style="color: #008000"&gt;/// Manages open connections on a per-thread basis&lt;/span&gt;
&lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ConnectionPool
{
    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, IDbConnection&amp;gt; threadConnectionMap;
 
    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// Gets the connection string.&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;value&amp;gt;The connection string.&amp;lt;/value&amp;gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; ConnectionString
    {
        get;
        &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; set;
    }
 
    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// Gets a connection.&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;returns&amp;gt;An open connection&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IDbConnection Connection
    {
        get
        {
            &lt;span style="color: #0000ff"&gt;lock&lt;/span&gt; (threadConnectionMap)
            {
                &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; threadId = Threading.Thread.CurrentThread.ManagedThreadId;
 
                IDbConnection connection = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (threadConnectionMap.ContainsKey(threadId))
                {
                    connection = threadConnectionMap[threadId];
                }
                &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;
                {
                    connection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SqlCeConnection(ConnectionString);
                    connection.Open();
                    threadConnectionMap.Add(threadId, connection);
                }
 
                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; connection;
            }
        }
    }
 
    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// Initializes a new instance of the &amp;lt;see cref=&amp;quot;ConnectionPool&amp;quot;/&amp;gt; class.&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;connectionString&amp;quot;&amp;gt;The connection string.&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ConnectionPool(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; connectionString)
    {
        threadConnectionMap = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, IDbConnection&amp;gt;();
        ConnectionString = connectionString;
    }&lt;/pre&gt;
&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;p&gt;With this you create a DataContext like this:&lt;/p&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;
  &lt;div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;
    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   1:&lt;/span&gt; &lt;span style="color: #008000"&gt;// Defined somewhere&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   2:&lt;/span&gt; ConnectionPool connectionPool = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ConnectionPool(MyConnectionString);&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   3:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   4:&lt;/span&gt; &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   5:&lt;/span&gt; &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   7:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;using&lt;/span&gt;(Northwind db = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Northwind(connectionPool.Connection))   &lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   8:&lt;/span&gt; {   &lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;   9:&lt;/span&gt;     (from p &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; db.Products).ToList();  &lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  10:&lt;/span&gt;     (from p &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; db.Customers).ToList();&lt;/pre&gt;

    &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060"&gt;  11:&lt;/span&gt; }&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Now each thread will have its own open connection which will minimize the cost of opening connections. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8937427" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/matt/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://blogs.msdn.com/matt/archive/tags/SQL+CE/default.aspx">SQL CE</category></item></channel></rss>