0

I have written a c# multithread function that reads data from sqlserver and for each row of data starts a separate thread.In each thread I have codes that need to work with db. this is what I do in each thread:

  • Create a new sql connection
  • Connect to it and open it
  • Do work
  • Close connection (while it is not necessary)

I should note that I use Using() command and I have read that this statement manages sql connection and closes it automatically after command is executed.

this function should be called periodically (say every 1 minute) and this is the problem i get into:

after first call number of active sql connections increases to number of rows (Threads that created per each row).in the second call this number increases again. for example after 5 calls with 100 rows we have 500 active sql connections! but I have read that connection pooling manages connections and uses created connections again. what should i do to solve this problem? did I miss any statement or is there something to do that I forgot?

3
  • Please provide some context to your question. What are you trying to achive? Is this a load testing application? Commented Jun 20, 2016 at 9:34
  • its a monitoring service and i insert devices that should be monitored to db and start a new thread for monitoring each device Commented Jun 20, 2016 at 9:38
  • Have a look if this has any relevance to your case: stackoverflow.com/questions/268982/… Commented Jun 20, 2016 at 10:06

1 Answer 1

0

Using just is a try - finally, nothing more, it calls Dispose and that's all.

in a "using" block is a SqlConnection closed on return or exception?

Sign up to request clarification or add additional context in comments.

6 Comments

ok if it calls dispose why connections stay open and do not close after dispose command?
It might remain opened because of the connection pool - msdn.microsoft.com/en-us/library/8xx3tyca(v=vs.110).aspx . You can limit the max number of the connections in the pool in the connection string: Max Pool Size=xxx . Without a code snippet, I cannot help more.
Yes Its Because of the Connection Pool,How Can I limit The Time Connection Remains? Or is it a bad way to disable pooling?
In the connections string: Max Pool Size=xxx
what if I totally Disable It?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.