asdfg
asdfg

Reputation: 2631

How do we know the lock status of sqlite DB?

1)Is there a pragma or any way to know the current lock state of sqlite db?.
2)Also, Is there a way to know if any other process is using the DB?.

Upvotes: 6

Views: 9058

Answers (3)

Glenn Maynard
Glenn Maynard

Reputation: 57464

It probably didn't exist in 2011, but sqlite3_txn_state will do it today: https://www.sqlite.org/c3ref/txn_state.html

Unfortunately there doesn't seem to be a pragma, which is a problem because most sqlite language bindings don't give access to it.

Upvotes: 0

nobody
nobody

Reputation: 20163

Regarding #1: No, because the answer you got would be immediately stale (that is if you got an answer of "no the database isn't locked", someone else could come along and immediately lock it, leaving you with bad info).

The correct approach is to simply try your operation (optionally with a timeout) and see if it succeeds.

Upvotes: 2

MPelletier
MPelletier

Reputation: 16657

No pragma, but the FAQ states:

When SQLite tries to access a file that is locked by another process, the default behavior is to return SQLITE_BUSY.

However, that only means the database is locked for writing, not reading.

Upvotes: 2

Related Questions