neverseenjack
neverseenjack

Reputation: 316

There is a file sharing violation. SQL Server CE 3.5 SP2

I've been getting this random issue in my .NET CF application that uses a SQL Server CE 3.5 SP2 database.

When the CF application starts up it performs database maintenance to verify the .SDF database file to check using the SQL Server CE Engine class:

using (SqlCeEngine engine = new SqlCeEngine(Resources.SqlCeConnectionString))
{
   Log.Info("Starting database Verification.");

   if (!engine.Verify())
   {
      Log.Warn("Database failed verification.");
      engine.Repair(null, RepairOption.RecoverAllOrFail);
      Log.Info("Database successfully repaired.");
   }
   else
   {
      Log.Info("Database Verification successful.");
   }
}

If the application is verified properly then I copy the .SDF into a backup folder:

if (File.Exists(Resources.DatabaseFileLocation))
{
   File.Delete(Resources.BackupDatabaseFileLocation);
   File.Copy(Resources.DatabaseFileLocation, Resources.BackupDatabaseFileLocation);
   Log.Info("Database backup complete.");
}
else
{
   Log.Info("Could not find Device Database.");
}

Once the backup is complete the application starts up and the first connection the application tries to make to the .SDF results in this exception:

There is a file sharing violation. A different process might be using the file.

Here is the Log file info:

2013-05-13 11:52:29,894 [INFO ] - Starting database Verification.
2013-05-13 11:52:33,832 [INFO ] - Database Verification successful.
2013-05-13 11:52:33,838 [INFO ] - Database backup starting.
2013-05-13 11:52:46,941 [INFO ] - Database backup complete.
2013-05-13 11:52:47,933 [ERROR] - There is a file sharing violation. A different process might be using the file. [ \Program Files\ApplicationName\DB.sdf ]
    at System.Data.SqlServerCe.SqlCeConnection.ProcessResults(Int32 hr)    at System.Data.SqlServerCe.SqlCeConnection.Open(Boolean silent)
    at System.Data.SqlServerCe.SqlCeConnection.Open()
    at CFApp.MainScreen.GetStartupData()
    at CFApp.MainScreen..ctor()
    at CFApp.Program.RunInReleaseMode() 
    at CFApp.Program.Main()

I've been researching the causes of this problem for weeks now and I cannot find anything. Any guidance or help would be very much appreciated.

Based on my efforts so far I can confirm these things

Thank you very much in advance for any info you can provide.

Upvotes: 4

Views: 2785

Answers (1)

user153923
user153923

Reputation:

I have found that not all classes implement IDisposable like they are supposed to. Microsoft's OLE JET database engine, for example, will not close a database connection when called from a using statement.

You could be having those issues here.

I would suggest, as a start, to explicitly call Close() on your connections. If that does not solve the issues, you could add an explicit call to Dispose().

It could also be that the Garbage Collector (GC) has not had a chance to handle all of the IDisposable code before your application attempts to access it again.

Upvotes: 1

Related Questions