Bug 6500 - Sqlite throwing exception when opening DB file
Summary: Sqlite throwing exception when opening DB file
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-08-10 10:08 UTC by René Ruppert
Modified: 2013-12-05 18:36 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description René Ruppert 2012-08-10 10:08:03 UTC
MT 5.2.13, Simulator 5.1

I'm getting the exception below sometimes. The DB file is there, it has content and it can be opened using any DB utility. So nothing obviuosly wrong. What is in line 136 and line 879 that can cause this exception?

[ERROR] FATAL UNHANDLED EXCEPTION: Mono.Data.Sqlite.SqliteException: Unable to open the database file
  at Mono.Data.Sqlite.SQLite3.Open (System.String strFilename, SQLiteOpenFlagsEnum flags, Int32 maxPoolSize, Boolean usePool) [0x0007e] in /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLite3.cs:136 
  at Mono.Data.Sqlite.SqliteConnection.Open () [0x0021a] in /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteConnection.cs:879
Comment 1 Sebastien Pouliot 2012-08-10 10:37:23 UTC
That error (CantOpen, 14) directly comes from native sqlite3 library. MonoTouch relays the error code into an exception. The most common explanation is that the file is not available (but that does not fit your description).

*Wild guess* : Is the file copied just before being opened ? maybe there's a race where sqlite tries to open the file before it's being closed by the copy call ?

There's quite a few (mostly unanswered) question on SO for "CantOpen 14" so there could be other conditions where this occur. 

> What is in line 136 and line 879 that can cause this exception?

Note: MonoTouch ships with it's class library source code. A large part of it is also open source, including Mono.Data.Sqlite.dll, and viewable from github. E.g.

Comment 2 René Ruppert 2012-08-10 14:22:38 UTC
Is there any C# version of Sqlite you can recommend that I can use instead?
Is Sqlite3 integral part of iOS? Or is MT just binding to "some" native lib?
Comment 3 René Ruppert 2012-08-10 14:24:33 UTC
Oh, I forgot somehting: I noticed that I have have different amounts of SqliteConnection.Open()) and .Close() calls. So I might be missing 4 closes. Could that be related? I will fix this now but I assume that this issue would rather lead to some "out of connections" problem.
Comment 4 Sebastien Pouliot 2012-08-10 15:22:56 UTC
> Is there any C# version of Sqlite you can recommend that I can use instead?

There is an implementation of SQLite written 100% in C#. I have not used it myself but it was used for a Xamarin sample app [1] for the Winphone version (since this platform does not allow native code nor ships with sqlite).

[1] http://blog.xamarin.com/2012/02/24/mwc_2012/

> Is Sqlite3 integral part of iOS?

Sqlite3 is shipped as part of iOS (not MonoTouch).

> I noticed that I have have different amounts of SqliteConnection.Open()) 

What are the option (i.e. SQLiteOpenFlagsEnum) used to open the database ? (e.g. sharing / exclusive)
Comment 5 René Ruppert 2012-08-10 15:43:50 UTC
I do not set any specific options. But I tried "Threaded" and it did not change anything.

Meanwhile I have made the following changes:

* I'm accessing multiple DB files
* The class that encapsulates Sqlite is used as a singleton per DB file. Before, new instances of the wrapper classes were created each time DB access was required.
* Each instance has now exactly one SqliteConnection object and that object is kept open throughout the lifetime of the app/DB.

This seems to get me rid of the problem and saves millions of .Open() and .Close() calls.
I can imagine that one connection blocked another although I'd not be aware of any race conditions.

I had a look at the C# version of Sqlite. Interesting project. But I suspect the C version outperforms it by factors.

I'll keep on testing if the issue happens again and will try to find a pattern if so.

Thanks for your hints!
Comment 6 PJ 2013-11-19 17:05:34 UTC
This bug has been in the NEEDINFO state with no changes for the last 90 days. Can we put this back into the NEW or CONFIRMED state, or are we still awaiting response?

If there is no change in the status of this bug over the next two weeks, this bug will be marked as NORESPONSE.
Comment 7 PJ 2013-12-05 18:36:00 UTC
This bug has not been changed from the NEEDINFO state since my previous comment, marking as RESOLVED NORESPONSE.

Please feel free to REOPEN this bug at any time if you are still experiencing the issue. Please add the requested information and set the bug back to the NEW (or CONFIRMED) state.