File sharing violation. SQL Server CE 3.5 SP2

I get this random problem in my .NET CF application that uses SQL Server CE 3.5 SP2 database.

When the CF application starts, it performs database maintenance to validate the .SDF database .SDF for validation 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 checked correctly, I copy the .SDF to the 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."); } 

After the backup is complete, the application starts and the first connection that the application tries to make with .SDF results in this exception:

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

Here is the log file information:

 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 studying the causes of this problem for several weeks now and I can’t find anything. Any guidance or help would be greatly appreciated.

Based on my efforts so far, I can confirm these things

  • The problem occurs randomly. The startup procedure is performed correctly several times, and then it will accidentally throw this error.
  • There are currently no other connections to the database, given that the application is only in the process of launching.
  • Each time an application opens a connection, it disposes of it as soon as it ends. This is done using using statements. An open connection does not remain open in the event of an unexpected shutdown / restart.
  • SqlCeEngine is removed using the using statement. Is there a possible delay between when an object is located, when its connection is closed?
  • There are currently no other processes. The device is locked to allow my application to run. The job manager shows only ActiveSync and my application. The device sometimes interacts with another desktop application using the OpenNETCF.Desktop.Communication Library via USB. Active Sync must be enabled for this to work correctly. the desktop computer he communicates with is Win XP.
  • I saw on some other forums that ActiveSync might support lock the file, which might cause the problem. In ActiveSync, nothing is selected in the device section for synchronization with the desktop. The "Files" checkbox is not selected. If anyone else has suggestions on how I can guarantee that this file is excluded from ActiveSync, it would also be very helpful.

  • UPDATE I used dotPeek to view the SqlCeEngine Dispose method. I think everything seems to be in order, and should my connection be set up correctly?

     private void Dispose(bool disposing) { if (!disposing) return; this.connStr = (string) null; this.connTokens = (Hashtable) null; NativeMethods.DllRelease(); this.isDisposed = true; } 
  • UPDATE . I tried to run this test to see if I could reproduce the error, and I did not come up with anything. The code was executed correctly, and the application started as it should, without problems with file sharing.

     for (int i = 0; i < 50; i++) { 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("Verified" + i); } } if (File.Exists(Resources.BackupDatabaseFileLocation)) { File.Delete(Resources.BackupDatabaseFileLocation); } File.Copy(Resources.DatabaseFileLocation, Resources.BackupDatabaseFileLocation); Log.Info("File Copied " + i); GetStartupData(); } 

Thank you for any information you can provide.

+4
source share
1 answer

I found that not all classes implement IDisposable , as intended. For example, the Microsoft OLE JET database engine does not close the database connection when called from a using statement.

You may have these problems.

First, I would suggest explicitly calling Close() for your connections. If this does not solve the problem, you can add an explicit call to Dispose() .

It may also happen that the garbage collector (GC) was not able to process all of the IDisposable code before your application tries to access it again.

+1
source

Source: https://habr.com/ru/post/1481073/


All Articles