Compare commits
2 Commits
9014363d6d
...
51b7372d81
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
51b7372d81 | ||
|
|
8d7e62f8af |
@ -8,8 +8,8 @@ public static class DatabaseExtensions
|
|||||||
{
|
{
|
||||||
public static IServiceCollection AddDatabase(this IServiceCollection services)
|
public static IServiceCollection AddDatabase(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
// 使用 Singleton 注册 SqlSugarScope(线程安全,内部使用 AsyncLocal)
|
// SqlSugarScope 是线程安全的,内部使用 AsyncLocal 隔离上下文
|
||||||
// SqlSugarScope 设计上支持并发,会自动管理连接池
|
// 使用 Singleton 注册,整个应用共享一个实例
|
||||||
services.AddSingleton<ISqlSugarClient>(s =>
|
services.AddSingleton<ISqlSugarClient>(s =>
|
||||||
{
|
{
|
||||||
return SqlSugarConfig.GetSqlSugarClient();
|
return SqlSugarConfig.GetSqlSugarClient();
|
||||||
|
|||||||
@ -27,6 +27,31 @@ public static class SqlSugarConfig
|
|||||||
?? _configuration.GetConnectionString("Default")
|
?? _configuration.GetConnectionString("Default")
|
||||||
?? throw new InvalidOperationException("Connection string 'Default' not found in environment variables or configuration.");
|
?? throw new InvalidOperationException("Connection string 'Default' not found in environment variables or configuration.");
|
||||||
|
|
||||||
|
// MySQL 连接池优化配置
|
||||||
|
// 添加连接池参数提高稳定性
|
||||||
|
if (!connectionString.Contains("Pooling=", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
connectionString += ";Pooling=true";
|
||||||
|
}
|
||||||
|
if (!connectionString.Contains("MaximumPoolSize=", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
connectionString += ";MaximumPoolSize=100";
|
||||||
|
}
|
||||||
|
if (!connectionString.Contains("MinimumPoolSize=", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
connectionString += ";MinimumPoolSize=5";
|
||||||
|
}
|
||||||
|
// 连接超时和生命周期
|
||||||
|
if (!connectionString.Contains("ConnectionTimeout=", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
connectionString += ";ConnectionTimeout=30";
|
||||||
|
}
|
||||||
|
// 连接空闲超时,避免使用长时间空闲的连接
|
||||||
|
if (!connectionString.Contains("ConnectionIdleTimeout=", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
connectionString += ";ConnectionIdleTimeout=180";
|
||||||
|
}
|
||||||
|
|
||||||
return new SqlSugarScope(new ConnectionConfig()
|
return new SqlSugarScope(new ConnectionConfig()
|
||||||
{
|
{
|
||||||
ConnectionString = connectionString,
|
ConnectionString = connectionString,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user