C # ADO Database connection pool

Source: Internet
Author: User
Tags connection pooling

A burst of development of a system, the same group of colleagues to provide a database connectivity components, is his own encapsulation, using a custom connection pool, with a very uncomfortable, and often because of the program is not rigorous reasons, resulting in connection pool connection is exhausted, but also caused other errors, so I want to study the ADO Connection pool.

Actually a long time ago, I have contacted the connection pool, but never actually used, in my impression, a connection pool should be similar to sqlconnection,mysqlconnection and so on, are implemented IDbConnection interface, so the program in use, There is no code intrusion, just in the new one connection, from the connection pool, the colleague's connection pool really does not accord with my aesthetic.

But when I began to search for information from the Internet, but found that, in fact, the bottom of the network has supported the connection pool, do not need our own development!!!

In ADO, the behavior of a connection pool can be controlled by a connection string , consisting mainly of four important attributes:

    • Connection Timeout: connection request Wait time-out. The default is 15 seconds, in seconds.
    • Max Pool Size: The maximum number of connections in the connection pool. The default is 100.
    • min Pool Size: The minimum number of connections in the connection pool. The default is 0.
    • Pooling: Whether connection pooling is enabled. ADO. NET is enabled for connection pooling by default, so you need to set Pooling=false manually to disable connection pooling.

I discovered the following article:

Http://www.cnblogs.com/liuhaorain/archive/2012/02/19/2353110.html

In addition, because we used the MySQL database, the class library is not provided by Microsoft, so I worry that MySQL did not implement the connection pool, so I tested it, the result is obvious, MySQL is also supported.

Test code and results are attached:

 classProgram {Static voidMain (string[] args) {Mysqlconnection conn=NewMysqlconnection (configurationmanager.connectionstrings["MySQL"].                       ConnectionString); DateTime StartTime=DateTime.Now;            Console.WriteLine (Starttime.tolongtimestring ());  for(inti =0; I < -; i++) {Conn.                Open (); Conn.            Close (); } DateTime endTime=DateTime.Now;            Console.WriteLine (Endtime.tolongtimestring ()); Console.WriteLine ("Connection 100 times, use Time (ms):"+ (endtime-startTime).            ToString ());        Console.ReadLine (); }    }

Configuration file:

Enable connection pooling: (default)

<add name= "MySQL" connectionstring= "server = 192.168.2.101; User ID = root; Password = password; Database = Test; Pooling=true; " />

Do not enable connection pooling:

<add name= "MySQL" connectionstring= "server = 192.168.2.101; User ID = root; Password = password; Database = Test; Pooling=false; " />

I have run three times for each configuration:

When using connection pooling, execute 100 times for 0.477 seconds

When the connection pool is not used, the time is 6.56 seconds and the difference is great.

In short, in the ADO, do not need to implement the connection pool, as long as the parameters can be set.

C # ADO Database connection pool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.