Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

ADO.NET: Connection Pooling



This sample illustrates how to construct a pool of connections to a datasource. You will want to do this to deploy high-performance applications. In this example the pool is established in the connection string and managed automatically by the SqlConnection.

VB ConnectionPooling.aspx
Run Sample View Source


In this example we specify the pooling characteristics in the connection string when the SqlConnection is constructed, as shown in the following code sample. Please note: Pooling is implicit, you automatically get it unless you disable it. Therefore, "true" is the default for the pooling keyword (pooling=true).
		
String connString;

// Specification in the connection string:
// Please note: Pooling is implicit, you automatically get it unless you disable it. 
//              Therefore, "true" is the default for the pooling keyword (pooling=true).   
// Connection Reset:    False
// Connection Lifetime: 5
// Enlist:              true
// Min Pool Size:       1
// Max Pool Size:       50

connString = "server=(local)\\SQLExpress;Integrated Security=SSPI;database=northwind;" +
             "connection reset=false;" +
             "connection lifetime=5;" +
             "min pool size=1;" +
             "max pool size=50";

SqlConnection myConnection1 = new SqlConnection(connString);
SqlConnection myConnection2 = new SqlConnection(connString);
SqlConnection myConnection3 = new SqlConnection(connString);
C#


Now we have code to use several of the connections in our pool. First, open two connections and return both of the connections to the pool. Then, open three connections from the pool and return all three connections to the pool.
		
// Open two connections. One is from the pool (see min pool size), the other is created.
Console.WriteLine ("Open two connections.");
myConnection1.Open();
myConnection2.Open();

// Now there are two connections in the pool that matches the connection string.
// Return the both connections to the pool. 
Console.WriteLine ("Return both of the connections to the pool.");
myConnection1.Close();
myConnection2.Close();

// Get a connection out of the pool.
Console.WriteLine ("Open a connection from the pool.");
myConnection1.Open();

// Get a second connection out of the pool.
Console.WriteLine ("Open a second connection from the pool.");
myConnection2.Open();

// Open a third connection.
Console.WriteLine ("Open a third connection.");
myConnection3.Open();

// Return the all connections to the pool.  
Console.WriteLine ("Return all three connections to the pool.");
myConnection1.Close();
myConnection2.Close();
myConnection3.Close();
C#


The model for pooled connections is the similar to non-pooled connections. However, it is especially important to call Close when you finish with a pooled connection to release it back into the pool.




Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.