简体   繁体   中英

How to minimize concurrent database connections?

My hosting company blocked my website for using more than 15 concurrent database connections. But in my code I closed each and every connection that I opened. But still they are saying that there are too many concurrent connections. And suggested me the solution that I should change the source code of my website. So please tell me the solution about this? And my website is dynamic, so would making it static simple HTML old days type will make a difference or not?

Also note that I tried this when no solution I can think of, before every con.open() , I added con.Close() , So that any other connection opened will be closed.

The first thing to do is to check when you open connections - see if you can minimise that. For example, and you doing "n+1" on different connections?

If you have a single server , the technical solution here is a semaphore - for example, something like:

someSemaphore.TakeOne();
try {
    using(var conn = GetConnection()) {
        ...
    }
} finally {
    someSemaphore.Release();
}

which will (assuming someSemaphore is shared, for example static ) ensure that you can only get into that block "n" times at once. In your case, you would create the semaphore with 15 spaces:

static readonly Semaphore someSemaphore = new Semaphore(15,15);

However! Caution is recommended: in some cases you could get a deadlock: imagine 2 threads poorly written each need 9 connections - thread A takes 7 and thread B takes 8. They both need more - and neither will ever get them. Thus, using WaitOne with a timeout is important:

static void TakeConnection() {
     if(!someSemaphore.TakeOne(3000)) {
         throw new TimeoutException("Unable to reserve connection");
     }
}
static void ReleaseConnection() {
     someSemaphore.Release();
}
...
TakeConnection();
try {
    using(var conn = GetConnection()) {
        ...
    }
} finally {
    ReleaseConnection();
}

It would also be possible to wrap that up in IDisposable to make usage more convenient.

Change Hosting Company.

Seriously.

Unless you run a pathetic Little home blog.

You can easily have more than 15 pages / requests being handled at the same time. I am always wary of "run away Connections" but I would not consider 15 Connections to even be something worth mentioning. This is like a car rental Company complaining you drive more than 15km - this simply is a REALLY low Limit.

On a busy Website you can have 50, 100, even 200 open Connections just because you ahve that many requests at the same time.

This is something not so obvious, but even if you care about opening and closing your connections properly, you have to look at something particular.

If you make the smallest change on the text you use to build a connection string, .net will create a whole new connection instead of using one already opened (even if the connection uses MARS), so just in case, look for your code if you are creating connection strings on the fly instead of using a single one from your web config.

I believe SQL Connections are pooled. When you close one, you actually just return it to connection pool.

You can use SqlConnection.ClearPool(connection) or SqlConnection.ClearAllPools to actually close the connection, but it will affect the performance of your site.

Also, you can disable pooling by using connection string parameter Pooling=false. There are also Max Pool Size (default 100), you may want to set it to a lower number.

This all might work, but i would also suggest you to switch providers ....

If you only fetch data from database then it is not very difficult to create some sort of cache. But if there full CRUD then the better solution is to change hosting provider.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM