.Net EntityFramework连接Mysql连接池配置
Posted シ゛甜虾
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.Net EntityFramework连接Mysql连接池配置相关的知识,希望对你有一定的参考价值。
微软的Ado.Net支持连接池,.EntityFrameworkCore.mysql同样支持连接池,并且默认也是开启连接池的,EntityFramework的使用教程请查看我的另一篇文章
sql设置
启用连接池
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";
mysql设置
启用连接池
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
以上是关于.Net EntityFramework连接Mysql连接池配置的主要内容,如果未能解决你的问题,请参考以下文章
U2 Toolkit for .NET - 实体框架示例“EntityFramework_CodeFirst2”未连接
EntityFramework 6 Alpha 2 & MySQL 连接器/NET 6.6.4
关于EntityFramework连接Oracle的详细教程