SQL SERVER connection pool

Original: SQL SERVER connection pool

Connection Pool What is it?
Every time when the program needs to read and write to the database. Connection.Open () will use the ConnectionString to connect to the database, the database will establish a connection for the program, and remains open, after which the program can use T-SQL statements to query / update the database. When performing the Connection.Close (), the database will close the current connection. Well, everything looks are so methodical.

But if my program need not timed to open and close connections (such as ASP.Net or Web Service), for example, when Http Request sent to the server,, we need to open the Connection and then use the Select * from Table returns a DataTable / DataSet to the client / browser, and then close the current Connection. That every Open / Close Connection so frequent operation for the entire system will undoubtedly become a waste.

ADO.Net Team will give a better solution methods. The previous saved Connection, next time will need to open connections when the previous Connection to the next connection. This is the Connection Pool.

 

How Connection Pool work?
First, when a program is executed Connection.open (), ADO.net need to judge, this connection supports Connection Pool (Pooling default is True), if you specify False, ADO.net is created between the database and a connection (for to avoid confusion, all connected to the database, use the "connect" description), then returns to the program.

If you specify True, ADO.net will be based ConnectString create a Connection Pool, and then fill in Connection to Connection Pool (all connections .net program, use the "Connection" description). Connection to fill the number determined by the Min Pool Size (default is 0) properties. For example, if 5 is specified, the time between ADO.net open SQL database 5 is connected, then the Connection 4, stored in the Connection Pool, a Connection returning.


When the program is executed to Connection.close () of. Pooling Connection Pool is put If True, ADO.net current Connection put and held between the connection database.

While also determining Connection Lifetime (default is 0) properties, 0 represents infinity, if there is time exceeds Connection Connection LifeTime, ADO.net Connection will be closed while disconnected from the connection to the database, rather than save it back to Connection Pool in.
(This is primarily used in the SQL database clustering, load balancing purposes). If Pooling designated False, the disconnected directly between database.


Then the next time Connection.Open () implementation of the time, ADO.Net Connection will determine whether the new ConnectionString connectionString previously stored in the Connection Pool is consistent.

(ADO.Net ConnectionString will turn into a binary stream, so to say, the new ConnectionString must exactly match the Connection's ConnectionString saved in Connection Pool in even added an extra spaces, or modify certain attributes in the Connection String the order will make ADO.Net think this is a new connection, and re-create a new connection, so if you are using a UserID, Password authentication method, modify the Password will lead to a connection, if you are using the SQL integrated authentication, you need to save two connections using the same).
ADO.net then necessary to determine whether the current Connection Pool Connection there may be used (not being used by another program), if not, it is necessary to determine ADO.net Max Pool Size ConnectionString setting (default is 100), if the Connection All connection Pool does not reach Max Pool Size, ADO.net will connect to the database again to create a connection, and then will return to the connection program.
If you have reached MaxPoolSize, ADO.net again will not create any new connections, but wait Connection Pool are occupied by other programs Connection release, this waiting time by SqlConnection.ConnectionTimeout (default is 15 seconds) restrictions, also that is, if more than 15 seconds, SqlConnection will throw out error (so sometimes if SqlConnection.open () method throws a timeout error, one possible reason is that there is no timely Connnection previous close, while the number of Connection Pool reached the MaxPoolSize.)
If one is available Connection, removed from the Connection Pool Connection is not directly returned to the program, ADO.net also need to check the ConnectionReset ConnectionString property (the default is True) the need for the most time Connection reset. This is because, before returning from the program Connection may have been modified, for example, use SqlConnection.ChangeDatabase method to modify the current connection, then returned Connection may have been connected to the current Connection String is not specified in the Initial Catalog database. It is necessary to reset a current connection. However, due to all the extra checks will increase ADO.net Connection Pool overhead on the system.
 
View number of application pools occupancy:
[Code = sql] select * from sysprocesses where dbid = db_id ( 'database name') [/ code]
Max Pool Size: If the default is not set 100, the theoretical maximum of 32,767. The maximum number of connections is the maximum number of connection pools can apply, if the database connection requests exceeding this number, the database connection request to be added later to the waiting queue, which will affect subsequent database operations. In the queue, the default time to wait for a connection to the server 15 seconds.
Chinese error:
Timeout expired. Timeout expired, but obtaining a connection from the pool. This situation may be because all pooled connections were in use and max pool size was reached.
English error:
Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached. 
Description of the problem: we obtain as generating abnormal connection exceeds the maximum connection pool. Connection Pooling usually a maximum of 100. When we get a connection exceeds the maximum, ADO.NET connection pool waiting for the return connection timeout, which will be thrown as an exception
Solution: The first thing is that after we close the connection using the connection immediately. If you do not close the connection then the connection will be saved to the connection pool knows GC to destroy. In this case you thought the connection pool does not actually reach the maximum connection pool has reached maximum Secondly, we can connect the string Max Pool Size = N; connected to dynamically expand the maximum number of connections in the pool.

Description: 
That is, if the value of connectionString max pool size is not specified, then the max pool size = 100, when the number of simultaneously accessing person to connect to the database 101 people, the wait time provided SqlConnection.ConnectionTimeout (default 15 seconds), Connection will be available or not the above error.
When we set:
"Server = (local); Integrated Security = SSPI; Database = Northwind; Max Pool Size = 512; Min Pool Size = 5" when. After the number of simultaneous connections to access personnel database is 513, set the wait time SqlConnection.ConnectionTimeout (default is 15 seconds), or not available Connection will be above the error.

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11229038.html