ADO.NET – simple test of connection pooling
Introduction
By default, ado.net uses a connection pool – to reuse open database connections.
To see how dramatic the consequences of the connection pool are,
I wrote a simple test – showing the total time spent in opening and closing DB connections – both with pooling and without.
Skip to the ending - No pooling causes 4 times the overhead of pooled ado.net connections.
1: class Program
2: {
3: static void Main(string[] args)
4: {
5: SqlConnection pooledTestConnection = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=TEST;Integrated Security=SSPI;");
6: long startTicks = DateTime.Now.Ticks;
7: for (int i = 1; i <= 100; i++)
8: {
9: pooledTestConnection.Open();
10: pooledTestConnection.Close();
11: }
12: long endTicks = DateTime.Now.Ticks;
13: Console.WriteLine("Time taken POOLED: " + (endTicks - startTicks) + " ticks.");
14: pooledTestConnection.Dispose();
15:
16: SqlConnection unpooledTestConnection = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=TEST;Integrated Security=SSPI;Pooling=false;");
17: startTicks = DateTime.Now.Ticks;
18: for (int i = 1; i <= 100; i++)
19: {
20: unpooledTestConnection.Open();
21: unpooledTestConnection.Close();
22: }
23: endTicks = DateTime.Now.Ticks;
24: Console.WriteLine("Time taken NO POOLED: " + (endTicks - startTicks) + " ticks.");
25: unpooledTestConnection.Dispose();
26: }
27: }
Summary
The result of the comparison is shown below – pooled connections clearly outperform non-pooled.
Leave a Reply