In my service with multiple Windows threads, I open connections to db in each thread, after which I delete these connections, although the problem is that some of them remain closed on db when I execute a query in the sys.sysprocesses table.
I conducted two unit tests and saw some strange behavior, first I started a cycle with 100 tasks, and in each of them I opened a new connection. After they are finished (I see through WaitAll ()), I see that some connections are still hanging in db. In the second unit test, when I run several open / dispose without parallel execution, it uses them perfectly and there are no connections in my db.
The problem is that in my Windows service these dangling connections add up and, in the end, there is no more room for new connections, and db becomes unusable for the user.
Here are the codes, at first parallel, and the second not:
[TestMethod] public void TestMultiThreadedAccessToExplicitObjectContext() { int taskSize = 100; List<Task> taskList = new List<Task>(); int goodSources = 0; for (int i = 0; i < taskSize; i++) { Task newTask = Task.Factory.StartNew(() => { System.Data.Objects.ObjectContext objectContext = new PersoniteEntities(); objectContext.Connection.Open(); objectContext.Dispose(); Thread.Sleep(1200); }); taskList.Add(newTask); } Task.WaitAll(taskList.ToArray()); GC.Collect(); total += goodSources; } [TestMethod] public void TestMultiThreadedAccessToExplicitObjectContextInline() { System.Data.Objects.ObjectContext objectContext1 = new PersoniteEntities(); objectContext1.Connection.Open(); objectContext1.Dispose(); System.Data.Objects.ObjectContext objectContext2 = new PersoniteEntities(); objectContext2.Connection.Open(); objectContext2.Dispose(); System.Data.Objects.ObjectContext objectContext3 = new PersoniteEntities(); objectContext3.Connection.Open(); objectContext3.Dispose(); System.Data.Objects.ObjectContext objectContext4 = new PersoniteEntities(); objectContext4.Connection.Open(); objectContext4.Dispose(); System.Data.Objects.ObjectContext objectContext5 = new PersoniteEntities(); objectContext5.Connection.Open(); objectContext5.Dispose(); }
thanks
source share