Page 1 of 1

IIS Contiunes accessing (locking) file after it is Closed.

Posted: Wed 02 Sep 2015 15:48
by Randy3W
I'm working with IIS version 8. Devart Version Standard Version 5.3.478.
I am running the following function in a service. The function runs fine however the File becomes locked on the server. I get the exception that "the file xyz is being used by another process"

I cannot rename or delete the file on the sever. After about 4 minutes, the file becomes unlocked. If I restart the web Site in IIS the file will immediately unlock.

What is causing the file to become locked at the file level and how can I correct? Is there a Connection string setting I should be using. Am I not closing properly?


Code: Select all

    string GetChangeTimeStamp(string FileName)
    {
        string res = "";
        string ConnectString = "datasource="+FileName;
        SQLiteConnection SqliteConn = new SQLiteConnection(ConnectString);
        SqliteConn.Open();
        using (SQLiteCommand cmd = SqliteConn.CreateCommand())
        {
            cmd.CommandText = "Select value from Settings where parameter = 'ChangedDTime' ";
            var r = cmd.ExecuteReader();
            if (r.Read())
            {
                res = r["Value"].ToString().Split('.')[0];
            }
            else
            {
                res = "No Date";
            }
            r.Close();
            cmd.Dispose();
        }

        SqliteConn.Close();
        return res;
    }

Re: IIS Contiunes accessing (locking) file after it is Closed.

Posted: Thu 03 Sep 2015 13:37
by Pinturiccio
SQLiteConnection has the Pooling connection string parameter. If Pooling is set to true in the connection string (this is the default value), the physical connection is placed to the pool, when a connection object is closed.

When calling the Close() method to close a connection, the SQLiteConnection object and its internal resources are freed for garbage collection, but the internal object with the physical connection to the database file is placed to a pool when 'Pooling=true'. That's why your database file stays locked.
You can specify 'Pooling=false' in the connection string in order to disable pooling; however, the latter may slow down connection opening.

A SQLiteConnection object also has two static methods ClearPool and ClearAllPools. These methods allow clearing connection pool. For more information, please refer to
https://www.devart.com/dotconnect/sqlit ... Pools.html
https://www.devart.com/dotconnect/sqlit ... rPool.html

Re: IIS Contiunes accessing (locking) file after it is Closed.

Posted: Thu 03 Sep 2015 17:16
by Randy3W
Thanks. That resolved my problem.