3

I am developing a UWP app in C#.

I need a SQLite database to store some information, and I place this file in the LocalState folder, which I am sure to have access to.

Everything usually works fine, except in few cases in which some users experience an error while accessing the database when the app starts (please also notice that the users are able to normally run the app - sometimes - while other times the app cannot start because of this file access problem).

The error is thrown by the SQLite connector and says

database is locked

The users also confirm that they haven't touched the database file (actually they didn't even know it exists...)

Now, I cannot understand:

  1. What is the root cause of the problem, since the file should be accessible by the UWP app, it is in the private space of the user, so no other user of the machine should have access to it, and the UWP app is basically a singleton (you cannot run two instances of the App at the same time)
  2. Why it is happening to a small portion of the users, just sporadically

Thank you very much!

Cristiano Ghersi
  • 1,944
  • 1
  • 20
  • 46
  • @YanGu-MSFT the error is thrown at the very first interaction with the database, so it is the first read operation on the database file. – Cristiano Ghersi Feb 11 '21 at 20:28
  • Usually, the error ‘database is locked’ is because multiple links or operations to database at the same time. I can’t reproduce the scenario you mentioned, therefore I couldn’t check what is the possible reason. It is better if you could provide us a stable sample by OneDrive or GitHub. You could try to create a singleton class which is used to maintain the connection to database and implement various operations to database. – YanGu Feb 12 '21 at 07:24
  • Hi @YanGu-MSFT, unfortunately, I am not able to reproduce the problem as well, it has just been reported by few of our users in their own machines. Moreover, I do have a singleton class that manages the connection to the DB. Last, this problem happens at the very start of the application, when we are creating the first connection to the DB, and there are no other background threads yet active, so the only active thread is the initial Main thread that sets up the connection to our DB locally. Any other thought? – Cristiano Ghersi Feb 12 '21 at 23:49
  • It’s hard to locate the possible reason for the error without a sample for testing. The error occurs in a small portion of your users, so the code should be no problem. The error may be related to the machines of your users or the databases. You could try to add a copy of the database file to resolve the error if the size of the database is not large. – YanGu Feb 15 '21 at 07:47
  • @YanGu-MSFT I agree it's hard to repro the problem;unfortunately,my users have confidential information that we cannot share, so there is no chance to have such a repro.I also agree that the code is probably not responsible, but what I want to achieve is to understand what is the cause of the problem so maybe we will be able to detect such problematic configurations and provide some workarounds (maybe not automatic, but just saying to the user what to do in case it happens). I am failing to understand what do you mean by "add a copy of the database file to resolve the error",what do you mean? – Cristiano Ghersi Feb 15 '21 at 18:11
  • You could try to refer to the relative [case](https://stackoverflow.com/questions/151026/how-do-i-unlock-a-sqlite-database?page=1&tab=votes#tab-top). My workaround is to from one of these answers. Copy your database file to some other location. Replace the database with the copied database which will dereference all processes which were accessing your database file. – YanGu Feb 16 '21 at 08:08

0 Answers0