Please help me with the issue occurring while using SQLite in sqlite3. Connect Failure: (Too many open files)
I’ll let you know the whole scenario and if you could then please help.
We use sqlite3 in a mobile apps.
I have created a SQLite connection with Serialized mode with the following configuration
1. I have set SQLite Open Flags to ReadWrite ,Create ,SharedCache ,FullMutex ,ProtectionComplete.
2. PRAGMA journal_mode = WAL
3. PRAGMA page_size = 4096
4. PRAGMA default_cache_size = 1024
5. PRAGMA cache_size = 1024
6. PRAGMA synchronous = 1
7. PRAGMA locking_mode = EXCLUSIVE
8. PRAGMA sqlite3_temp_directory = tempPath (I have set a path)
9. Whenever the app goes in background we Shut Down the SQLite connection and on Activation we Initialize the SQLite again in Serialized mode.
10. We use a single connection in the whole application which gets closed when app goes in background.
Now the issue is after using the app for some time we get the issue as
Connect Failure (too many open files)
When I found the list of files open I found that out of 256 file descriptors around 210-220 file descriptors were used by a file named SQliteDB.db3-shm
And one was SQLiteDB.db3-wal
So my question is :
1. How this can be solved or if any alternative or If have done any mistake while setting the configuration mode?
2. How to clear those temporary db3-shm files?
> 9. Whenever the app goes in background we Shut Down the SQLite connection and on Activation we Initialize the SQLite again in Serialized mode.
If the problem really is with SQLite, and not some other thing which is opening files, then my guess is that your detection of backgrounding is faulty, or that your app is being choked off before it can close the database.
What hardware are you programming for, and what library are you using to make SQLite calls ?
Do you test the code returned by sqlite_close() to make sure it is returning SQLITE_OK and not an error ?
As a temporary test, after detecting being moved to background, and closing the database, deinitialise SQLite by calling sqlite3_shutdown() or whatever equivalent your library allows. It should be impossible for SQLite to hold a file open after this call, even if it wasn't able to close all databases without errors.
the last step might return SQLITE_DONE. But the SQLite library doesn't release the resources until you call either sqlite_reset() or sqlite_finalize(). And one of the resources is the database connection.
So if you use sqlite_step() anywhere, make sure you finalize after the last step.
You must not be closing the SQLite connection when the app goes into the background. Then the file descriptors stay open, and new ones are opened (on the same file) when you reopen when the app activates.