Replacing Orchard.sdf in specs execution - database file becomes corrupted randomly

Topics: Troubleshooting
Sep 6, 2013 at 8:36 AM
Edited Sep 6, 2013 at 8:37 AM
Hello all, I'm having problems with the reliability of our spec (integration) tests. At the core lies a problem with resetting the DB file to an initial state (from right after executing setup).

What we're doing: at the beginning of a test run we run setup on a clean Orchard installation and afterwards save a copy of the DB, which then at the beginning of some of our tests we restore to its original location. The problem is that we're getting anywhere from 1 to several tens of exceptions of this type:
[TestRunnerThread] Orchard.Environment.DefaultOrchardHost - A tenant could not be started: Default
System.Data.SqlServerCe.SqlCeException (0x80004005): The database file may be corrupted. Run the repair utility to check the database file. [Database name = C:\TeamCity\buildAgent\temp\buildTmp\DZ.Specs\deterministicInstance\App_Data\Sites\Default\Orchard.sdf]
   at [...]
   at Orchard.Environment.ShellBuilders.ShellContextFactory.CreateShellContext(ShellSettings settings)
The problem seems to lie in replacing the DB file. We've already tried a few things to avoid this problem:
  • add a Thread.Sleep(1000) after putting the clean DB in place - this helped a few tests
  • copy the DB file before initializing the remote AppDomain instance hosting the application that will use that DB
  • after setup (and thus creation of the initial DB file), shut down the AppDomain hosting the application before backing up the DB file
We've been down to a single instance of the exception during a whole test run, but it's simply put not predictable.

Could anybody advise on how to avoid corruption of the DB in this kind of setup?
Sep 6, 2013 at 5:27 PM
Looks like SQL CE file corruption is a common problem...

Luck would have it that I found this great post on stackoverflow: I followed advice number 2 so far, i.e. setting AutoShrink Threshold=100 inside the connection string for the SQL CE database file and my specs are (almost) all running green now! What a great switch :-)
Had to add that option to Orchard.Data.Providers.SqlCeDataServicesProvider. Now my line 40 reads:
string localConnectionString = string.Format("Data Source={0}; AutoShrink Threshold=100", _fileName);
That's it for now.
Sep 7, 2013 at 4:00 AM
Personally, I never use SQL CE in production. Doesn't inspire me the greatest confidence. It's great for dev boxes however.