简体   繁体   English

如何限制.Net Web应用程序的ODBC连接数?

[英]How to limit number of ODBC connections from a .Net web application?

I have created a .Net 3.5 web service in C# that communicates to a Progress OpenEdge database through ODBC. 我在C#中创建了一个.Net 3.5 Web服务,它通过ODBC与Progress OpenEdge数据库通信。

This database has been set up to only allow 10 simultaneous connections on the user account the web service is using (third party restriction). 此数据库已设置为仅允许Web服务正在使用的用户帐户上的10个同时连接(第三方限制)。

I am running into a huge problem when the web service gets a lot of simultaneous requests and tries to connect to the database for each of these requests. 当Web服务获得大量并发请求并尝试连接到数据库以获取每个请求时,我遇到了一个大问题。 When the connection limit is exceeded, the database will reject new incoming connections for a couple of minutes. 超过连接限制时,数据库将拒绝新的传入连接几分钟。

The web service is running on windows server 2008. Connection pooling is enabled for the ODBC driver. Web服务正在Windows Server 2008上运行。为ODBC驱动程序启用了连接池。

I somehow will have to restrict the number of connections that my web service is trying to make, but I don't know how. 我不得不限制我的网络服务试图建立的连接数,但我不知道如何。 The Progress OpenEdge ODBC driver that I am using, does not support a maximum pool size argument. 我正在使用的Progress OpenEdge ODBC驱动程序不支持最大池大小参数。

I've been reading a lot of documentation from microsoft on the subject, but all I have been able to conclude is: 我一直在阅读微软关于这个主题的大量文档,但我能得出的结论是:

  • Connection pooling is enabled by default 默认情况下启用连接池
  • Connection pooling cannot be configured from my application 无法从我的应用程序配置连接池
  • Default maximum pool size is 100 默认最大池大小为100
  • There is no way to change the maximum pool size unless the driver you are using supports it 除非您使用的驱动程序支持它,否则无法更改最大池大小

Can someone confirm this, and/or suggest a method to limit the number of connections in this situation? 有人可以确认这一点,和/或建议一种方法来限制这种情况下的连接数量吗? Thank you very much :) 非常感谢你 :)

You could consider configuring the WCF service to only allow 10 simultaneous requests. 您可以考虑将WCF服务配置为仅允许10个同时发出的请求。 This will only work if each request uses no more than 1 db connection, of course, any may or may not make sense for your particular service. 这仅在每个请求使用不超过1分贝的连接时才有效,当然,任何可能对您的特定服务有意义或可能没有意义。

You can try the WCF approach as mentioned or you could implement the connection pooling programmatically. 您可以尝试上面提到的WCF方法,也可以通过编程方式实现连接池。

public interface IService
{
    void Execute(Action<IDbConnection> command);
    T Execute<T>(Func<IDbConnection, T> command);
}

public sealed class ConnectionManager : IService
{
    public const int MaxConnections = 10;

    private readonly string _connectionString;
    private readonly SemaphoreSlim _workers = new SemaphoreSlim(0, MaxConnections);

    public ConnectionManager(string connectionString)
    {
        _connectionString = connectionString;
    }

    public void Execute(Action<IDbConnection> command)
    {
        lock(_workers)
        {
            using(var connection = new OdbcConnection(_connectionString))
            {
                connection.Open();
                command(connection);
            }
        }
    }

    public T Execute<T>(Func<IDbConnection, T> command)
    {
        lock(_workers)
        {
            using(var connection = new OdbcConnection(_connectionString))
            {
                connection.Open();
                return command(connection);
            }
        }
    }
}

The SemaphoreSlim will prevent more than 10 connections from opening assuming the code that is called doesn't try anything sneaky. SemaphoreSlim将阻止超过10个连接打开,假设被调用的代码没有尝试任何偷偷摸摸的东西。

Sounds to me like the architecture of your application should be reconsidered. 听起来像应用程序的体系结构应该重新考虑。 I would deal with this situation by having a separate thread (or threads ) that connects to the database in your web service. 我会通过一个连接到Web服务中的数据库的单独线程(或多个线程)来处理这种情况。 The web service could then manage the thread(s) that connects to the database. 然后,Web服务可以管理连接到数据库的线程。

For simplicity assume only one connection to the database. 为简单起见,假设只有一个与数据库的连接。

When you get a request for the web service the the service will put the request into the queue of the shared database thread. 当您收到Web服务请求时,该服务会将请求放入共享数据库线程的队列中。

The database thread will just sit there until there is something in the queue to process. 数据库线程将坐在那里,直到队列中有东西要处理。 When there is it will take it off the queue and send the request to the server. 如果有,它将把它从队列中取出并将请求发送到服务器。

It is likely that you will have a bit of extra work here in picking up the returned data too because the data will be returned on a different thread to the thread it was requested on. 在这里你可能会有一些额外的工作来获取返回的数据,因为数据将在不同的线程上返回到它所请求的线程。

The same principle could be used having multiple threads ( one per allowed connection ) You would then write a class to manage the threads so the each thread just took the next item from the queue. 可以使用具有多个线程(每个允许连接一个)的相同原则。然后,您将编写一个类来管理线程,以便每个线程只从队列中获取下一个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以限制.NET中数据库连接的总数? - Is it possible to limit the total number of database connections in .NET? 如何为ASP.NET应用程序监视与远程计算机的连接数? - How to monitor number of connections to a remote computer for ASP.NET application? 如何限制.net应用程序执行特定次数 - how can I limit a .net application to be executed for specific number of times 有没有办法在 ASP.NET Web API 应用程序中全局限制并行任务的数量? - Is there a way to limit the number of parallel Tasks globally in an ASP.NET Web API application? 如何调试ASP.NET 2.0 Web服务应用程序中的泄漏连接? - How to debug leaking connections in an ASP.NET 2.0 Web Services application? 如何允许远程连接到我的.net应用程序? - How to allow remote connections to my .net application? .NET ODBC + SQL Server:如何将错误从存储过程中调用应用程序? - .NET ODBC + SQL Server: How to throw error up to calling application from stored proc? 如何将 ODBC 连接与 Visual Studio 2019 和 .NET Core 3.0 结合使用? - How can I use ODBC connections with Visual Studio 2019 and .NET Core 3.0? 如何限制 ASP.Net Web Forms 中一天保存的记录数? - How to limit the number of records saved in a day in ASP.Net Web Forms? 如何将文件从Web下载到ASP.NET Web应用程序中? - How to download files from web into an ASP.NET Web Application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM