Page 1 of 1

How to release file handle of sqlite database?

Posted: Fri 28 May 2010 10:15
by LordFjord
Hello,
I have problems releasing file access on the sqlite database file once the database was connected via dotconnect (I havent tried other sqlite drivers yet).

What i want to achieve is to connect to a database file, do some operations on it, then release the file access so the file can be moved/processed otherwise.

I have tried to dispose the sqlite database context before doing the operations on filesystem level, but i cant solve the "The process cannot access the file because it is being used by another process." error. The Sqlite database is set to run in exclusive access mode and my application is the only one working with it at that time (in my test environment).

My context wrapper has a dispose pattern implemented (similar to the one mentioned in http://ondotnet.com/pub/a/dotnet/2002/0 ... traps.html ). I am closing any open transactions/connections, then dispose the context.

Code: Select all

...
		protected virtual void Dispose( bool disposing )
		{
			if ( !isDisposed ) // only dispose once!
			{
				if ( disposing )
				{
					if ( _sqliteContext.Transaction != null )
					{
						_sqliteContext.Transaction.Rollback();
						_sqliteContext.Transaction.Dispose();
					}
					if ( _sqliteContext.Connection != null )
					{
						_sqliteContext.Connection.Close();
						_sqliteContext.Connection.Dispose();
					}
					_sqliteContext.Dispose();
				}
			}
			this.isDisposed = true;
		}

		public void Dispose()
		{
			Dispose( true );
			GC.SuppressFinalize( this );
		}

		~SQLiteManager()
		{
			Dispose( false );
		}
What is the recommended way / best practice to ensure that the file handle is released by the database context?

Posted: Mon 31 May 2010 11:44
by StanislavK
The database file lock should be removed after the connection is closed. The most probable cause why it doesn't happen is that the connection pooling feature is on. To resolve the problem, please set the Pooling property of the connection string to false. For more information on connection pooling, please see the corresponding section of our FAQ:
http://www.devart.com/dotconnect/sqlite ... Q.html#q50

Posted: Mon 31 May 2010 13:29
by LordFjord
Thank you very much for the hint, disabling collection pooling helped :)