开发者

sqlite database locking error

开发者 https://www.devze.com 2023-03-02 14:30 出处:网络
I am developing c# application with a backend sqlite db. In my application mutithreaded concepts are being used. All the threads will call the below mentioned code. then it will throw error as databas

I am developing c# application with a backend sqlite db. In my application mutithreaded concepts are being used. All the threads will call the below mentioned code. then it will throw error as database is locked.

lock (localLockHandle)
        {

            SQLiteCommand cmd = conn.CreateCommand();
            cmd.CommandText = sqlExpr;
            int ireturn = cmd.ExecuteNonQuery();
            return ireturn;
        }

Is there any way to get rid from this database lock error. i am opening and closing the connection after each process. even sometimes it throws this lock error. please give me a solution as it is very critical for开发者_如何学Go me.

Thanks


If I understand your code snippet correctly, you are using lock on an object in the local scope. This would mean that other threads would lock on a different object.

Try defining a global lock, like this:

static readonly object DatabaseLock = new object()

and then using lock(DatabaseLock) wherever you access the database.

0

精彩评论

暂无评论...
验证码 换一张
取 消