赞
踩
微软的Ado.Net支持连接池,.EntityFrameworkCore.MySql同样支持连接池,并且默认也是开启连接池的,EntityFramework的使用教程请查看我的另一篇文章
启用连接池
string connStringUsePool = "server=.;database=master;uid=sa;pwd=123;pooling=true;connection lifetime=0;min pool size = 1;max pool size=500";
不启用连接池
string connStringUnUsePool = "server=.;database=master;uid=sa;pwd=123;pooling=false";
启用连接池
string connStringUsePool = "server=127.0.0.1;uid=root;pwd=xxx;database=databasetest;pooling=true;connectionlifetime=5;minpoolsize=1;maxpoolsize=500;"
不启用连接池
"server=127.0.0.1;uid=root;pwd=xxx;database=databasetest;pooling=false;"
mysql 查询连接数
show processlist
关闭线程池
开启连接池后
如果连接池不够用了,会支持抛出异常Too many connections太多的连接
- SelectThread:4,Exception:System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySql' call.
- ---> MySqlConnector.MySqlException (0x80004005): Too many connections
- at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, Int32 startTickCount, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ServerSession.cs:line 425
- at MySqlConnector.MySqlConnection.CreateSessionAsync(ConnectionPool pool, Int32 startTickCount, Nullable`1 ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlConnection.cs:line 884
- at MySqlConnector.MySqlConnection.OpenAsync(Nullable`1 ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlConnection.cs:line 414
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。