SQLSugar 连接池默认大小 | 详解配置与代码设置
SQLSugar 默认的连接池数量是 20。但是可以通过配置文件或代码设置连接池的大小。
配置文件设置:
在 appsettings.json 文件中添加以下配置:
"ConnectionStrings": {
"Default": "Server=localhost;Database=MyDatabase;User ID=sa;Password=yourpassword;"
},
"SQLSugar": {
"ConnectionStringName": "Default",
"DbType": "SqlServer",
"InitKeyType": "Attribute",
"IsAutoCloseConnection": true,
"AopEvents": "Default",
"ConfigureExternalServices": {
"DataInfoCacheServer": "MemoryCache",
"CacheType": "MemoryCache",
"CacheTimeSpan": 3600
},
"ConnectionPool": {
"InitialPoolSize": 50,
"MaxPoolSize": 100,
"MinPoolSize": 10,
"AcquireTimeout": 120,
"RemoveAbandonedTimeout": 120,
"RemoveAbandoned": true
}
}
代码设置:
// 创建数据库连接对象
var db = new SqlSugarClient(new ConnectionConfig
{
ConnectionString = "Server=localhost;Database=MyDatabase;User ID=sa;Password=yourpassword;",
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
AopEvents = "Default",
ConfigureExternalServices = new ConfigureExternalServices
{
DataInfoCacheServer = "MemoryCache",
CacheType = "MemoryCache",
CacheTimeSpan = 3600
},
ConnectionPool = new ConnectionPoolConfig
{
InitialPoolSize = 50,
MaxPoolSize = 100,
MinPoolSize = 10,
AcquireTimeout = 120,
RemoveAbandonedTimeout = 120,
RemoveAbandoned = true
}
});
参数说明:
InitialPoolSize: 连接池的初始大小。MaxPoolSize: 连接池的最大大小。MinPoolSize: 连接池的最小大小。AcquireTimeout: 获取连接的超时时间(秒)。RemoveAbandonedTimeout: 移除闲置连接的超时时间(秒)。RemoveAbandoned: 是否移除闲置连接。
建议:
- 根据应用程序的负载和数据库服务器的性能来调整连接池的大小。
- 如果应用程序负载很高,则需要增加连接池的大小。
- 如果数据库服务器性能较差,则需要减小连接池的大小。
- 定期监控连接池的使用情况,并根据需要调整连接池的大小。
原文地址: https://www.cveoy.top/t/topic/lEew 著作权归作者所有。请勿转载和采集!