2

在遵循 Microsoft 的一些示例之后,我实现了 DbContext 的 Elastic 版本,它从 Elastic ShardMapManager 获取其连接字符串,具体取决于当前用户的租户(或客户)ID。

这在技术上可行,我已将其部署到我的 Azure 帐户。我现在关心的是连接池管理,因为我已经覆盖了默认的上下文连接创建机制。此外,我不确定shardMap.OpenConnectionForKey每次请求时如何管理连接(请参阅下面的我的 ninject 设置)。

昨天经过一些简单的测试后,我的网络应用程序失败并显示以下消息:

超时已过。在从池中获取连接之前超时时间已过。这可能是因为所有池连接都在使用中并且达到了最大池大小。`

这可能是一次性的,因为我今天无法重新创建它,我想确保有效地使用连接池,因为我最不想看到的是当真正的用户开始敲击系统时发生这种情况。

它失败的代码在我的 ElasticScaleContext 的完整代码中注释如下:

public class ElasticScaleContext<T> : DbContext
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="shardMapManager">This is injected - only one of these exists per Application (Singleton)</param>
    /// <param name="customerId">The Shard Key is the Customer Id</param>
    /// <param name="shardConnectionString">The connection string for the Shard - this should only have the credentials and NOT any datasource. The correct datasource and initial catalog are returned by the Shard Map</param>
    /// <param name="metadataWorkSpaceConnectionString">Metadata required by EF model first cannot be passed in the shard connection string, it is passed here and used to return the MetadataWorkspace - no actual connection is created</param>
    public ElasticScaleContext(ShardMapManager shardMapManager, IPrincipal user, string shardConnectionString, string metadataWorkSpaceConnectionString)
      : base(CreateEntityConnection(shardMapManager, user, shardConnectionString, metadataWorkSpaceConnectionString), true)
    {
    }

    private static DbConnection CreateEntityConnection(ShardMapManager shardMapManager, IPrincipal user, string shardConnectionString, string metadataWorkSpaceConnectionString)
    {

        int shardKey = 0; // Default just to get a valid connection string on login page (it's never actually used)

        if (user != null && user.Identity.IsAuthenticated)
        {
            shardKey = user.Identity.GetCustomerId();
        }

        // Loads the Shard Map from the Shard Manager
        // This has the details of which shards are located on which databases
        ListShardMap<T> shardMap = shardMapManager.GetListShardMap<T>(AppConfig.ShardMapName);

        // No initialization
        Database.SetInitializer<ElasticScaleContext<T>>(null);

        // Create Elastic Scale SqlConnection
        // ******* FAILED HERE *********
        var shardConnection = shardMap.OpenConnectionForKey(shardKey, shardConnectionString, ConnectionOptions.None);

        // Allocate metadata workspace via an EF connection 
        var efConnection = new EntityConnection(metadataWorkSpaceConnectionString);

        // Create Entity connection that holds the sharded SqlConnection and metadata workspace
        var workspace = efConnection.GetMetadataWorkspace();
        EntityConnection entcon = new EntityConnection(workspace, shardConnection);

        return entcon;

    }

}

我正在使用 Ninject 和 ShardMapManager 作为单例注入:

// Only ever create one Shard Map Manager
kernel.Bind<ShardMapManager>().ToMethod(context =>
{
   return ShardMapManagerFactory.GetSqlShardMapManager(AppConfig.ConnectionString, ShardMapManagerLoadPolicy.Lazy);
}).InSingletonScope();

上下文是根据请求创建的:

kernel.Bind<DbContext>().ToMethod(ctx =>
{
   return new ElasticScaleContext<int>(kernel.Get<ShardMapManager>(), kernel.Get<IPrincipal>(), AppConfig.BaseConnectionString, AppConfig.SCSMetaDataConnectionString);
}}).InRequestScope();

所以有几个问题:

  1. 我在上下文中创建的连接是否应该正常处理?

  2. 有谁知道如何ShardMapManger管理连接?

  3. 下面的代码(由于我的Model First EF 方法是必需的)是否可以打开然后不关闭连接?

更新- 在 @Evk 的建议(见评论)之后,我修改了对 EntityConnection 的构造函数调用以传递 true ,entityConnectionOwnsStoreConnection这应该允许它在使用后正确关闭存储连接,见下文。我真的需要一种监控连接池的方法,看看这是否有任何影响:

 var efConnection = new EntityConnection(metadataWorkSpaceConnectionString);

  // Create Entity connection that holds the sharded SqlConnection and metadata workspace
  var workspace = efConnection.GetMetadataWorkspace();
  EntityConnection entcon = new EntityConnection(workspace, shardConnection. true);

最后,我有什么方法可以监控和查看 Azure 弹性 SQL 池上连接的当前状态吗?

我意识到这是很多问题,但我正在寻找有关该领域的任何信息 - 网络上确实没有那么多现有信息。

附加信息:该解决方案使用EF6、SQL Server 2014、MVC 5.0

4

1 回答 1

1

正如评论中所指出的,在CreateEntityConnection方法中,您从底层连接创建 EntityConnection,但不要将重要参数“entityConnectionOwnsStoreConnection”设置为 true。这意味着实体连接不负责管理您的商店连接并且不会关闭它,因此您的连接会泄漏。要修复,请使用另一个EntityConnection构造函数:

var entcon = new EntityConnection(workspace, shardConnection, true);

然后,当上下文被释放时,它会释放你的EntityConnection,这反过来又会释放底层的数据库连接。

于 2016-11-24T14:39:21.187 回答