sql-serverdockercirclecisingle-user

SQL Server database keeps setting SINGLE_USER on in tests


I have 2 docker containers that host their own server. I also have a 3rd docker container that hosts a SQL Server database. Each server accesses this database.

My DB docker container is configured as so in the docker-compose.yml:

my-db:
     image: microsoft/mssql-server-linux:latest
     environment:
       - ACCEPT_EULA=Y
       - SA_PASSWORD=******
     ports:
       - "1433:1433"

Each servers connection string (They are .NET servers) are

ConnectionStrings__Default=data source=my-db;initial catalog=TestDb;persist security info=True;user id=SA;password=******;

When I run my tests, I get a log from the docker container for the DB that says it's setting SINGLE_USER mode on. This obviously breaks my tests because both servers have to access it.

I tried using a DB browser to set MULTI_USER on which worked... until the tests started... Then the db or something in the test suite switched it back to SINGLE_USER.

How do I configure this through the docker compose, dockerfiles or anything else to NOT set SINGLE_USER mode on or specifically set MULTI_USER mode on?

In case it matters: I'm also using Selenium for tests and circleci for test automation.


Solution

  • After scouring the code base I found it actually had nothing to do with configuration. I am using Entity Framework and the unit tests deleted and remigrated the database after each test. Calling the migrate command put the database into SINGLE_USER mode. I found this out when I removed the migrate call out of curiosity and lo and behold, no SINGLE_USER mode was set.

    I assume the migrate call (myDbContext.Database.Migrate()) sets the mode so that it kicks other listeners out so as not to cause conflicts in the middle of migrations. But that's just a guess. I couldn't find the true answer as to why online.

    So in short, the solution to this is that the code base was setting the SINGLE_USER mode, I just had to find the call.