Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database malformation #703

Closed
kanoshin opened this issue Jun 22, 2017 · 10 comments
Closed

Database malformation #703

kanoshin opened this issue Jun 22, 2017 · 10 comments

Comments

@kanoshin
Copy link

Hello,

in logs I received from iOS clients I found such error:
{"result":{"code":0,"sqliteCode":11,"message":"database disk image is malformed"}}

Percentage of clients who had this error in logs is really small (~0.01%), but I suspect that it might be higher because in some cases our logging depends on database.
Also we have complaints that sometimes it is required to reinstall app to make it work, database malformation might perfectly explain such reports.

Do you know what might cause database malformation?
We use only yours plugin to communicate with database, so 3rd party shouldn't be the case. Our app has multiple pages. Also periodically we close and reopen database because previously we had issues with hanging transactions.

@kanoshin
Copy link
Author

I'm thinking about deleting and recreating database in case of corruption. Is it always possible to remove database file or it might be locked?

@brodycj
Copy link
Contributor

brodycj commented Jun 22, 2017

Hi @kanoshin,

This is the first report of corruption with the plugin. I saw quite a bit of possible corruption on the sqlite forum.

In case of corruption it would probably be best to retrieve and then delete/remove the database file, if possible. Unfortunately I have very limited experience with file locking issues. I think sqlite(3) provides some tooling that can recover the data from a corrupted databases in certain cases.

I can also say that there have been some issues with using the standard transaction mechanism together with multi-page applications ref: #666. It should be fixed in the latest version but it would probably still be safer to use the following calls instead:

  • db.executeSql() to read data and make single-statement modifications
  • db.sqlBatch() to execute multiple changes in an atomic manner (single transaction)

In fact I also raised #690 to discuss the possibility of deprecating the old standard transaction mechanism.

I hope this is somewhat helpful. I hope to address your issue in #702 ("Database might be busy when close is called") within the next few weeks or so.

@brodycj
Copy link
Contributor

brodycj commented Oct 30, 2017

I received a similar report of corruption in a very small percentage of users of an iOS app by email. In that case the application is a single-page app and does not use the now deprecated Web SQL transaction calls.

I can think of a couple possible causes:

  1. An iOS sqlite database may be accessed from multiple threads from a thread pool while sqlite3.c is compiled with -DSQLITE_THREADSAFE=1. This is not very likely to cause data corruption since the use of a lock prevents concurrent usage on the same database handle but it would be better to compile with SQLITE_THREADSAFE=2. For reference:
  1. iOS threading issue with openDatabase? #716 (comment):

openDBs is an NSMutableDictionary, which isn't designed for thread safety...

Excellent workaround / solution is given in #716.

For the future it would be better to either run all sqlite operations in a single background thread or one background thread per db, just like the implementation for Android. Possible reference:

brodycj pushed a commit to brodycj/Cordova-sqlite-storage-common-dev that referenced this issue Nov 3, 2017
(SQLITE_THREADSAFE=1 on Android/Windows) to avoid possible
database malformation due to multithreaded access
ref: storesafe#703
brodycj pushed a commit to brodycj/Cordova-sqlite-storage-common-dev that referenced this issue Nov 3, 2017
- SQLITE_THREADSAFE=2 on iOS/macOS (SQLITE_THREADSAFE=1 on Android/Windows) to
  avoid possible malformed database due to multithreaded access
  ref: storesafe#703
- Drop -DSQLITE_OMIT_BUILTIN_TEST (renamed & not recommended)
- Certain LOCAL_CFLAGS added from
  https://www.sqlite.org/compile.html#recommended_compile_time_options
- Minor reordering
brodycj pushed a commit to brodycj/Cordova-sqlite-storage-common-dev that referenced this issue Nov 3, 2017
- SQLITE_THREADSAFE=2 on iOS/macOS (SQLITE_THREADSAFE=1 on Android/Windows) to
  avoid possible malformed database due to multithreaded access
  ref: storesafe#703
- Drop -DSQLITE_OMIT_BUILTIN_TEST (renamed & not recommended)
- Certain LOCAL_CFLAGS added from
  https://www.sqlite.org/compile.html#recommended_compile_time_options
- Minor reordering
@brodycj
Copy link
Contributor

brodycj commented Nov 6, 2017

Please try the latest cordova-sqlite-ext version, it has fixed build flag and workaround solution for #716.

@brodycj
Copy link
Contributor

brodycj commented Nov 8, 2017

Build flag fix and workaround solution for #716 are now published for this plugin version, will be included in the other sqlite plugin versions in the near future.

@brodycj brodycj closed this as completed Nov 8, 2017
brodycj pushed a commit to brodycj/cordova-sqlite-ext that referenced this issue Dec 21, 2017
NOTE: cordova-sqlite-ext 0.10.6 has incorrect -DSQLITE_THREADSAFE=1
build flag on iOS due to a mistake in the merge. This will be changed
to SQLITE_THREADSAFE=2 for iOS in the next release.

ADDITIONAL NOTE: plugin.xml has duplicate macOS ("osx") platform
section, also due to a mistake in the merge. The duplicate macOS
platform section will be removed from the next release.

ref: storesafe/cordova-sqlite-storage#703
brodycj pushed a commit to brodycj/cordova-sqlite-ext that referenced this issue Dec 21, 2017
- SQLITE_THREADSAFE=2 on iOS as well as macOS (SQLITE_THREADSAFE=1 on
  Android/Windows) to avoid possible malformed database due to
  multithreaded access ref: storesafe/cordova-sqlite-storage#703
- Suppress warnings for sqlite3.c on iOS/macOS
  Thanks again to @cjpearson for the suggestion in
  storesafe/cordova-sqlite-storage#569
- Some additional iOS build flag updates in this plugin version
brodycj pushed a commit to brodycj/cordova-sqlite-evplus-legacy-attach-detach-free that referenced this issue Jan 2, 2018
…fore merge

    - SQLITE_THREADSAFE=2 on iOS & macOS (SQLITE_THREADSAFE=1 on
      Windows) to avoid possible malformed database due to multithreaded
      access ref: storesafe/cordova-sqlite-storage#703
    - Suppress warnings for sqlite3.c on iOS/macOS
      Thanks again to @cjpearson for the suggestion in
      storesafe/cordova-sqlite-storage#569
    - Some additional iOS/macOS/Windows build flag updates
      in this plugin version
    - Document sqlite (3.8.10.2) build settings
brodycj pushed a commit to brodycj/Cordova-sqlite-evcore-extbuild-free-dev that referenced this issue Jan 8, 2018
Merge branch 'evcore-common-master'

cordova-sqlite-evcore-common-free compile-time option fixes for iOS/macOS:
- SQLITE_THREADSAFE=2 on iOS/macOS to avoid possible malformed database due
  to multithreaded access ref: storesafe/cordova-sqlite-storage#703
- Suppress warnings when building sqlite3.c (iOS/macOS)
- Remove unwanted SQLITE_OMIT_BUILTIN_TEST option (iOS/macOS)

additional doc fixes
brodycj pushed a commit to brodycj/Cordova-sqlite-evcore-extbuild-free-dev that referenced this issue Jan 8, 2018
Merge branch 'evcore-common-master'

cordova-sqlite-evcore-common-free compile-time option fixes for iOS/macOS:
- SQLITE_THREADSAFE=2 on iOS/macOS to avoid possible malformed database due
  to multithreaded access ref: storesafe/cordova-sqlite-storage#703
- Suppress warnings when building sqlite3.c (iOS/macOS)
- Remove unwanted SQLITE_OMIT_BUILTIN_TEST option (iOS/macOS)

Additional doc fixes
@NileshSystematix
Copy link

@brodybits : I am also facing the same issue of "database disk image is malformed" in android. I have installed whti plugin near to Aug 2017. So can you please me that this issue is resolved in this plugin version or I have to use another plugin(please share the new plugin url if required)?

Please help me asap I am very much stucked.

@brodycj
Copy link
Contributor

brodycj commented Feb 12, 2018

@NileshSystematix please see storesafe/cordova-sqlite-storage-help#34 (comment) for the answer to your question.

@NileshSystematix
Copy link

@brodybits : Thanks a lot, I have read the issue but can you please confirm me more so that I can save my time.

@storesafe storesafe locked and limited conversation to collaborators Feb 12, 2018
@brodycj
Copy link
Contributor

brodycj commented Feb 12, 2018

can you please confirm me more so that I can save my time.

See storesafe/cordova-sqlite-storage-help#34 (comment)

If you have any more questions please ask in storesafe/cordova-sqlite-storage-help#34 or raise a new issue.

@brodycj
Copy link
Contributor

brodycj commented Feb 15, 2018

As discussed in #754 I got the SQLITE_THREADSAFE compile-time setting wrong, will fix in the next patch release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants