[英]Getting Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. .exception
[英]Unhandled Exception: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool
我在.netcore中托管了服务,该服务运行几个功能,这些功能每秒执行一次SQL查询。 当应用运行一段时间后崩溃时,我知道它耗尽所有连接池的问题,并且我没有正确处理连接。 我知道我需要打开和关闭连接。
dbcontext未处理的异常:System.InvalidOperationException:超时已过期。 从池中获取连接之前已经过超时时间。 这可能是因为所有池化连接都在使用中,并且达到了最大池大小。
我面临的问题是我正在使用DBContext
,它会自动为我获取并打开数据库连接,而我并没有明确要求打开连接,但是该如何关闭连接呢? 对于每个Scope
?
public class DBChangeService : DelegatingHandler, IHostedService
{
private Timer _timer;
public SqlDependencyEx _sqlDependency;
private readonly IHubContext<SignalServer> _hubcontext;
private readonly IServiceScopeFactory _scopeFactory;
public DBChangeService(IHubContext<SignalServer> hubcontext, IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
_hubcontext = hubcontext;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_timer = new Timer(Heartbeat, null, 0, 1000);
return Task.CompletedTask;
}
public async void Heartbeat(object state)
{
await _hubcontext.Clients.All.SendAsync("SBSystemBrodcasting", SBSystemApps());
await _hubcontext.Clients.All.SendAsync("SBUserBrodcasting", SBNonSystemApps());
}
public List<SomeModel> SBSystemApps()
{
using (var scope = _scopeFactory.CreateScope())
{
var _DBcontext = scope.ServiceProvider.GetRequiredService<PlatformStatusContexts>();
var result = _DBcontext.SomeModels.FromSql((
@"SELECT
a.app_guid as AppGuid,
a.name as AppName,
a.state as AppState,
a.created_at as AppCreatedAt,
a.updated_at as AppUpdatedAt,
a.foundation as AppFoundation,
s.name as SpaceName,
o.name as OrgName
FROM
apps as a
INNER JOIN
spaces as s ON a.space_guid = s.space_guid
INNER JOIN
organizations as o ON s.org_guid = o.org_guid
where s.name = 'system' and o.name = 'system' and a.foundation = 2")).ToList();
return result;
}
}
public List<SomeModel> SBNonSystemApps()
{
using (var scope = _scopeFactory.CreateScope())
{
var _DBcontext = scope.ServiceProvider.GetRequiredService<PlatformStatusContexts>();
var result = _DBcontext.SomeModels.FromSql((
@"SELECT
a.app_guid as AppGuid,
a.name as AppName,
a.state as AppState,
a.created_at as AppCreatedAt,
a.updated_at as AppUpdatedAt,
a.foundation as AppFoundation,
s.name as SpaceName,
o.name as OrgName
FROM
apps as a
INNER JOIN
spaces as s ON a.space_guid = s.space_guid
INNER JOIN
organizations as o ON s.org_guid = o.org_guid
where s.name != 'system' and o.name != 'system' and a.foundation = 2")).ToList();
return result;
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
//Timer does not have a stop.
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
}
启动:
services.AddDbContext<PCFStatusContexts>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
连接字符串:
"DefaultConnection": "Server=address,port;Database=dbname;User Id=uname;Password=password;Trusted_Connection=false;MultipleActiveResultSets=true;"
更新:
DBContext:
public partial class PlatformStatusContexts : DbContext
{
public PlatformStatusContexts()
{
}
public PlatformStatusContexts(DbContextOptions<PlatformStatusContexts> options): base(options)
{
}
public virtual DbSet<Apps> Apps { get; set; }
public virtual DbSet<Organizations> Organizations { get; set; }
public virtual DbSet<Spaces> Spaces { get; set; }
public DbQuery<SomeModel> SomeModels { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//model builder for each table
}
}
您可以尝试如下使用with上下文吗
using(var _DBcontext = scope.ServiceProvider.GetRequiredService<PlatformStatusContexts>())
{
var result = _DBcontext.SomeModels.FromSql((
@"SELECT
a.app_guid as AppGuid,
a.name as AppName,
a.state as AppState,
a.created_at as AppCreatedAt,
a.updated_at as AppUpdatedAt,
a.foundation as AppFoundation,
s.name as SpaceName,
o.name as OrgName
FROM
apps as a
INNER JOIN
spaces as s ON a.space_guid = s.space_guid
INNER JOIN
organizations as o ON s.org_guid = o.org_guid
where s.name = 'system' and o.name = 'system' and a.foundation = 2")).ToList();
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.