In-page navigation
- 1. How to open a connection pool?
- 2. The connection pool that is relevant and what is it?
- 3. How to use the same connection pool to access different databases?
- 4. The default maximum and minimum connection pool database
'On the database connection pool we have heard about or used, but really understand how much of it?
- How Data Connection pooling is enabled? What are the main parameters?
- Why use connection pooling?
- How close the connection pool?
- How does not change the current open database connection pool under the new situation?
- Lifecycle connection pool?
- What happens when the database server is forced to close the connection?
===========================================================================
First, explain the test environment:
Database version: SQL SERVER 12.0.2269.0 [Microsoft SQL Server 2014 Enterprise (64-bit)]
C # version: Microsoft Visual C # 2015 14.0.25431
Client Version: System.Data 4.0.0
===========================================================================
First create a database access class, easy to test:
public class DbAccepter { /// <Summary> /// database connection /// </ Summary> Private the SqlConnection _conn = new new the SqlConnection (); /// <Summary> /// connection string /// </ Summary > Private string _connectionString = "" ; /// <Summary> /// connection string /// </ Summary> public string the ConnectionString { GET { return _connectionString; } set { _connectionString = value; _conn.ConnectionString = _connectionString; } } public DataTable GetData(string sql) { string sss = _conn.State.ToString(); using (SqlCommand cmd = new SqlCommand(sql, _conn)) { using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { DataSet ds = new DataSet(); _conn.Open(); adapter.Fill(ds); _conn.Close(); return ds.Tables[0]; } } } }
1. How to open a connection pool?
Only the connection string can be added to the options:
private static String _connectionString = "pooling=true;connection lifetime=5;min pool size = 2;max pool size=4;
Data Source = 127.0.0.1; Initial Catalog = tempdb; User ID = test; Password=Sm5lAXQiZ10L";
Note inside and parameters related to the connection pool:
pooling = true; - represents a connection pool open (open by default)
min pool size = 2 - minimum connection pool size: that nothing is executed when the initial connection is established first n connections and database services
max pool size = 4 - maximum connection pool size: The maximum number of connection establishment, is established when needed.
Exemplified: min pool size = 2; max pool size = 4;
Call the following database script execution button is clicked.
try { info.Clear(); for (int i = 0; i < num.Value; i++) { Thread th = new Thread(GetCurrentDbName); th.Start(); } } catch (Exception ex) { listboxAdd(ex.Message); }
GetCurrentDbName here is to create a database connection and then the implementation of a SQL script that gets the current name of the database and then delay 3S, so SQL time of about 3 seconds each execution
SELECT db_name (); WAITFOR Delay ' 00:00:03 ' ; - (3 seconds)
Here we first concurrent set to 1, will create two connections when a connection is established for the first time.
You can be viewed in the database:
select * from sysprocesses where hostname='xxx' and loginame='test'
2. The connection pool that is relevant and what is it?
In the same process, and only about the connection string, the connection string as long as it will use the same connection pool.
Here we will connect the string to FIG (only modify the minimum data pool):
After the execution of instructions, and then look at the database connection information:
We found that will be more of a connection information, you can suspect that the same connection pool is not in use. You can change the number of concurrent 4.
Can be seen, when the number of connections becomes 6, before the connection string occupies two, modified occupies four. Because the size of the connection pool is limited to 4, and thus the description is indeed the use of two connection pool.
When we ask the number of concurrent connection pool is greater than the maximum number of what would happen? Here we modify the program code before, beginning and ending time of recording the calling thread execution method
Performing deadline threads 9 can be seen the first four substantially identical deadline, about the middle of the four previous four nights 3S. The last four intermediate later than 3S.
Description Start with four threads database request to obtain connection pool resources, others wait.
After four complete execution threads, won another four threads of connection pool resources, and finally to release a thread waiting for true connection pool again.
Then we look at the number of connections to the database is still four. DESCRIPTION plurality of requests in parallel, the connection pool exceeds the portion will wait. How long it will wait for a timeout? ? ?
The conclusion is if you want to use the connection pool, you must use the same connection string, exactly as written (I did not fully tested).
3. How to use the same connection pool to access different databases?
In the actual development, we will access multiple databases in the same server, but do not want to create too many connections. If you visit different libraries use different connection string, it will produce multiple connection pool.
Such as this:
pooling=true;connection lifetime=10;min pool size = 2;max pool size=4; Data Source = 127.0.0.1; Initial Catalog = tempdb; User ID = test; Password=Sm5lAXQiZ10L pooling=true;connection lifetime=10;min pool size = 2;max pool size=4; Data Source = 127.0.0.1; Initial Catalog = master; User ID = test; Password=Sm5lAXQiZ10L
这时有两个办法:
1.在SQL语句中指定数据库名称(不太靠谱)
2.不要重新创建连接,而是使用的相同的连接字符串创建连接后再切换数据库。实现代码如下:
using (SqlCommand cmd = new SqlCommand(sql, _conn)) { using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { DataSet ds = new DataSet(); _conn.Open(); //这时指向的是tempdb _conn.ChangeDatabase("master"); //切换数据库 adapter.Fill(ds); _conn.Close(); return ds.Tables[0]; } }
页内导航
- 1.如何开启连接池?
- 2. 那连接池是和什么有关呢?
- 3.如何使用相同的连接池访问不同的数据库?
- 4.数据库连接池的默认最大和最小值
‘关于数据库连接池大家都听说过或者用过,但真正的了解有多少呢?
- 数据连接池如何启用?有哪些主要的参数?
- 为什么要使用连接池?
- 如何关闭连接池?
- 如何在不开启新的连接池情况下切换当前数据库?
- 连接池的生命周期?
- 当数据库服务器强制关闭连接时会怎么样?
===========================================================================
首先说明一下测试环境:
数据库版本:SQL SERVER 12.0.2269.0 [Microsoft SQL Server 2014 Enterprise (64-bit)]
C#版本: Microsoft Visual C# 2015 14.0.25431
客户端版本:System.Data 4.0.0
===========================================================================
首先创建一个数据库访问类,便于测试:
public class DbAccepter { /// <summary> /// 数据库连接 /// </summary> private SqlConnection _conn = new SqlConnection(); /// <summary> /// 数据库连接字符串 /// </summary> private String _connectionString = ""; /// <summary> /// 数据库连接字符串 /// </summary> public string ConnectionString { get { return _connectionString; } set { _connectionString = value; _conn.ConnectionString = _connectionString; } } public DataTable GetData(string sql) { string sss = _conn.State.ToString(); using (SqlCommand cmd = new SqlCommand(sql, _conn)) { using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { DataSet ds = new DataSet(); _conn.Open(); adapter.Fill(ds); _conn.Close(); return ds.Tables[0]; } } } }
1.如何开启连接池?
只需要连接字符串中加入对应的选项即可:
private static String _connectionString = "pooling=true;connection lifetime=5;min pool size = 2;max pool size=4;
Data Source = 127.0.0.1; Initial Catalog = tempdb; User ID = test; Password=Sm5lAXQiZ10L";
注意里面和连接池有关的参数:
pooling=true; --表示开启连接池(默认为开启)
min pool size = 2 --最小连接池大小:即什么也没执行初次连接的时候先和数据库服务建立n个连接
max pool size=4 --最大连接池大小:允许建立的最大连接数,是在需要的时候建立。
举例说明:min pool size = 2;max pool size=4 ;
点击按钮时调用以下代码执行数据库脚本。
try { info.Clear(); for (int i = 0; i < num.Value; i++) { Thread th = new Thread(GetCurrentDbName); th.Start(); } } catch (Exception ex) { listboxAdd(ex.Message); }
这里的GetCurrentDbName方法是创建数据库连接然后执行一段SQL脚本,获取当前数据库的名称然后延时3S,所以每次执行SQL的时间都约为3秒
select db_name(); waitfor delay '00:00:03'; --延迟3秒
这里我们先将并发数设为1,在初次建立连接时会创建2个连接。
可以在数据库中进行查看:
select * from sysprocesses where hostname='xxx' and loginame='test'
2. 那连接池是和什么有关呢?
在同一个进程中,只和连接字符串有关,只要连接字符串一样就会使用同一个连接池。
这里我们将连接字符串改为下图(只修改了最小数据池):
执行指令后,再观察一下数据库的连接信息:
我们发现会多出一个连接信息,可以会怀疑是不是使用的同个连接池。你可以将并发数改为4.
可以看到,这时的连接数变为6,之前的连接字符串占用了两个,修改后的占用了4个。因为连接池的大小限制为4,所以说明确实是使用了两个连接池。
当我们的并发请数大于最大连接池数会怎么样?这里我们修改一下之前的程序代码,记录线程调用方法执行的起止时间
9个线程的执行截止时间,可以看出前四的截止时间基本相同,中间的四个大约比前4个晚3S。最后一个比中间四个晚3S。
说明开始有4个线程的数据库请求获取到连接池资源,其它线程等待。
这4个线程执行完成后,另4个线程获的连接池资源,最后一个线程等待真到再次释放连接池。
那我们再看一下数据库的连接数仍然为4。说明有多个并行请求时,超过连接池的部分将等待。等待多久会超时呢???
结论是如果想使用连接池,必须使用相同的连接字符串,必须一字不差(我并没有全部测试)。
3.如何使用相同的连接池访问不同的数据库?
在实际开发中我们会访问同一个服务器中的多个数据库,但又不想创建过多的连接。如果访问不同的库使用不同的连接字符串,那就会产生多个连接池。
如这样:
pooling=true;connection lifetime=10;min pool size = 2;max pool size=4; Data Source = 127.0.0.1; Initial Catalog = tempdb; User ID = test; Password=Sm5lAXQiZ10L pooling=true;connection lifetime=10;min pool size = 2;max pool size=4; Data Source = 127.0.0.1; Initial Catalog = master; User ID = test; Password=Sm5lAXQiZ10L
这时有两个办法:
1.在SQL语句中指定数据库名称(不太靠谱)
2.不要重新创建连接,而是使用的相同的连接字符串创建连接后再切换数据库。实现代码如下:
using (SqlCommand cmd = new SqlCommand(sql, _conn)) { using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { DataSet ds = new DataSet(); _conn.Open(); //这时指向的是tempdb _conn.ChangeDatabase("master"); //切换数据库 adapter.Fill(ds); _conn.Close(); return ds.Tables[0]; } }