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

Sqlite databse corruption #643

Closed
karmac2015 opened this issue Mar 8, 2017 · 5 comments
Closed

Sqlite databse corruption #643

karmac2015 opened this issue Mar 8, 2017 · 5 comments

Comments

@karmac2015
Copy link

Hello,

I use your plugin in my ionic application on android platform , and I have a plugin that uses the SQLiteDatabase class to work with the database created by your plugin.
But when my java code access the database in the same time when I access the database from javascript using your plugin, the app crashes and the database is corrupted.
Please help me to solve this problem.

@brodybits
Copy link
Contributor

As discussed in #626:

From http://ericsink.com/entries/sqlite_android_n.html: in case someone android.database.sqlite from another plugin or application-specific Java code to access the same database as this plugin there is potential for database corruption ref:

Note that the first part about "SQLite and Android N" is NOT a problem with this plugin since it uses its own build of SQLite.

In case anyone encounters this issue the workaround is to open the database with the androidDatabaseImplementation: 2 and possibly androidLockWorkaround: 1 settings as documented.

Will be documented soon.

@karmac2015
Copy link
Author

karmac2015 commented Mar 8, 2017

In my java plugin I use the following code:

SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, 0);
ContentValues values = new ContentValues(); 
values.put("Name", queryValues.get("name"));
db.insert("student", null, values);

If there are multiple threads that access the database in the same time, is your solution solve the problem ?
What about using db.enableWriteAheadLogging() in this situation ?
How can I get the connection opened by your plugin in my java plugin ?

@brodybits
Copy link
Contributor

values.put("Name", queryValues.get("name"));

What is queryValues?

If there are multiple threads that access the database in the same time, is your solution solve the problem ?

Yes, assuming that there is not a bug in the builtin android.database implementation. This may depend on which Android version and maybe which system build is installed on the device.

What about using db.enableWriteAheadLogging() in this situation ?

I have not looked at this one. In general I do not really favor WAL (write ahead logging ref: https://www.sqlite.org/wal.html) since it would require the overhead of checkpointing at certain intervals. I also just discovered an interesting issue at http://stackoverflow.com/questions/39149065/sqlite-write-ahead-logging-wal-journal-mode-with-attached-database.

How can I get the connection opened by your plugin in my java plugin ?

I recommend that you look in the dbmap at https://github.com/litehelpers/Cordova-sqlite-storage/blob/storage-master/src/android/io/sqlc/SQLitePlugin.java#L39 and follow the objects until you get the SQLiteDatabase connection.

Please report if these answers help solve your problem or not.

@brodybits brodybits reopened this Mar 9, 2017
@brodybits
Copy link
Contributor

Another solution may be to use an Android content provider to store and retrieve data as discussed in storesafe/cordova-sqlite-storage-help#9:

it may be possible to use a content provider with https://github.com/phearme/cordova-ContentProviderPlugin to store and access data from both JavaScript and custom Java code. I found a couple nice looking tutorials at:

@brodybits
Copy link
Contributor

Closing as invalid with info needed due to lack of response. Please feel free to ask here or in a new issue if you have any more questions.

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

2 participants