In ADO.net, if every time we perform a query, we create a new connection. Like the following:
//connection1
Sqlconnection sqlcon=new Sqlconnction();
sqlcon.ConnectionString = constring + "database = DB1;Integrated Security=true";
sqlcon.Open();
sqlcon.Close();
//connetion2
Sqlconnection sqlcon=new Sqlconnction();
sqlcon.ConnectionString = constring + "database = DB2;Integrated Security=true";
sqlcon.Open();
sqlcon.Close();
//connection1
Sqlconnection sqlcon=new Sqlconnction();
sqlcon.ConnectionString = constring + "database = DB1;Integrated Security=true";
sqlcon.Open();
sqlcon.Close();
//connection1
Sqlconnection sqlcon=new Sqlconnction();
sqlcon.ConnectionString = constring + "database = DB1;Uid=sa ;Pwd=password";
sqlcon.Open();
sqlcon.Close();
//connection2
Sqlconnection sqlcon=new Sqlconnction();
sqlcon.ConnectionString = constring + "database = DB2;Integrated Security=true";
sqlcon.Open();
sqlcon.Close();
If for every query or transation there create a new connection, this will waste the resources, as create new connection will take time, which will cause the query slow for users.
ADO.net has the connection pool mechanism. For the created connections, they are placed in the connection pool, the connection object can be used from the pool in stead of initialize a new connection object. After a specific of time period, if a connection is not used, it can be disposed.
The connection pool can be created by the sqlconnection string, pretty much like:
SqlConnection mySqlConnection =
new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;" +
"max pool size=10;min pool size=5"
);
No comments:
Post a Comment