1

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 me.

Thanks

1
  • I believe applying a lock at that point it really won't do anything. In my opinion at some point you are doing a query that locks the database. It is the query itself, and not the SQLLiteCommand. Try to discover what is the last query you run before the database becomes locked. Commented May 4, 2011 at 13:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.