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:
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: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
WaitOnewith a timeout is important:It would also be possible to wrap that up in
IDisposableto make usage more convenient.