Connection Pooling and Integration Tests

On my current project we recently started getting the following error while running integration tests as part of continuous integration build.

System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

I tested our application, and we did not have connection leaking problems, our connection were being disposed of after use.  So the issue was not in our code, but in unit tests.

We are using MSTest and TFS Build for our continuous integration implementation.  The errors started after we wrote sufficient number, well over a hundred of integration tests.  As I was trying to find out the reason, I came across some article that were stating that even though MS Test runs tests sequentially, but the initialization is run in parallel, one thread per test / test class.  In our initialization code we perform application login, which hits the database.  Since our configuration file does not specify connection pooling settings, we are use defaults you can see here.  So, default number of connections per pool is 100, which makes sense that the error showed up after we had over 100 tests.  The solution to the problem was very simple.  I simply added “Pooling=false” to the connection string that is used in unit test runs only. 

Hopefully this will save a few more folks a few minutes.

Thanks.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *