Ado. NET Review Summary (2)--Connection pool

Source: Internet
Author: User

1.

2.

3. Example: in 100 cycles, perform the open and close of the database connection and use stopwatch to view the time spent.

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;usingSystem.Diagnostics;namespacewindowsformsapplication1{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {Stopwatch Watch=NewStopwatch (); Watch.            Start ();  for(inti =0; I < -; i++) {SqlConnection conn=NewSqlConnection ("server=.; Database=dbtest;uid=sa;pwd=123"); Conn.                Open (); Conn.                Close (); Conn.             Dispose (); } watch.            Stop (); Label1. Text=Watch.        Elapsedmilliseconds.tostring (); }        Private voidButton2_Click (Objectsender, EventArgs e) {Stopwatch Watch=NewStopwatch (); Watch.            Start ();  for(inti =0; I < -; i++) {SqlConnection conn=NewSqlConnection ("server=.; database=dbtest;uid=sa;pwd=123; Pooling=false"); Conn.                Open (); Conn.                Close (); Conn.            Dispose (); } watch.            Stop (); Label2. Text=Watch.        Elapsedmilliseconds.tostring (); }    }}
View Code

Conclusion:

1. Each application in ADO maintains a pool of connection objects, which is not created each time it is used, but is first fetched from the pool and then used directly.

New if not, reduces the time of each new build

2. Create a collection space in memory to store idle objects, if each time an object is needed, first obtained from the collection if it is not obtained in the new.

3. Object pooling: Improve execution efficiency

4 Description:

(1) Two connections The string used must be the same

(2) Open use, close () after the use of the connection object is placed in the connection pool, when using the Connection object, go to the connection pool to find, available to use.

(3) If a connection object is not closed in use and a connection object is required, a new connection object is recreated because there is no idle connection object at this time.

(4) The number of alternate connection objects can be set, set in the connection string.

(5) Close does not really close the connection, but instead puts the connection object into the connection pool.

ADO. NET Review Summary (2)--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.