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

How to make an asynchronous transaction? #343

Closed
screendriver opened this issue Sep 11, 2015 · 10 comments
Closed

How to make an asynchronous transaction? #343

screendriver opened this issue Sep 11, 2015 · 10 comments

Comments

@screendriver
Copy link

I have a really large blob of JSON data that can't be handled in memory at once. So I use Oboe.js to process this JSON data as a stream. Unfortunately your library handles transaction in one big transaction function:

db.transaction(function(tx) {
  tx.executeSql('DROP TABLE IF EXISTS test_table');
  // How to handle asynchronous / AJAX code here?
});

Is there a way to make a database transaction that can manage asynchronous code? I thought something like that:

var tx = db.beginTransaction();
// some asynchronous code that use promises for example
service.fetchData().then(function(data) {
  tx.executeSql(data);
  tx.end();
});
@brodycj
Copy link

brodycj commented Sep 11, 2015

This idea is in consideration for the future. Here is a workaround that you can try, to take some extra control over the transaction mechanism.

Make the following change to SQLitePlugin.js:

--- a/www/SQLitePlugin.js
+++ b/www/SQLitePlugin.js
@@ -110,15 +110,15 @@
   };

   SQLitePlugin.prototype.transaction = function(fn, error, success) {
     if (!this.openDBs[this.dbname]) {
       error(newSQLError('database not open'));
       return;
     }
-    this.addTransaction(new SQLitePluginTransaction(this, fn, error, success, true, false));
+    this.addTransaction(new SQLitePluginTransaction(this, fn, error, success, false, false));
   };

   SQLitePlugin.prototype.readTransaction = function(fn, error, success) {
     if (!this.openDBs[this.dbname]) {
       error(newSQLError('database not open'));
       return;
     }

This will cause the plugin to stop using BEGIN/COMMIT/ROLLBACK. Then you have to do something like db.executeSql('BEGIN') at the beginning, db.executeSql('COMMIT') to commit, and db.executeSql('ROLLBACK)` to rollback the changes since BEGIN.

Please let me know if this works for you.

@screendriver
Copy link
Author

Thank you for the workaround. I will try it out.
Is there a roadmap or something when this feature will be available in the official release?

@brodycj
Copy link

brodycj commented Sep 11, 2015

Unfortunately I really cannot promise anything for free right now, due to some changing priorities.

@screendriver
Copy link
Author

All right. Should we close this issue or leave it open until this feature is implemented?

@brodycj
Copy link

brodycj commented Sep 11, 2015

Let's leave it open. I do want to add this when I get a chance.

@screendriver
Copy link
Author

👍

@Yerkon
Copy link

Yerkon commented May 23, 2018

@brodybits hi. You wrote:

This idea is in consideration for the future

Is any progress on it? Thanks

@brodycj
Copy link

brodycj commented May 23, 2018

Will not be done in this plugin version. Similar functionality may be possible in case of a redesign (#548).

@Yerkon
Copy link

Yerkon commented May 23, 2018

@brodybits, I have written solution for So question https://stackoverflow.com/a/50486135/5677886. This plugin used in Ionic 3. What do you think about it? It’s ok?

@brodycj
Copy link

brodycj commented May 23, 2018

Please ask in https://github.com/litehelpers/Cordova-sqlite-help/issues.

I would like to suggest you consider using https://github.com/brodybits/sql-promise-helper which provides a clean Promise-based API and can be used on browser platform as discussed in brodycj/sql-promise-helper#4.

Private support is also available, please contact sales@litehelpers.net for more details.

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