brodybits / cordova-sqlite-ext

A Cordova/PhoneGap plugin to open and use sqlite databases on Android/iOS/macOS/Windows with REGEXP (Android/macOS/iOS) and pre-populated databases (Android/iOS/macOS/Windows)
Other
71 stars 55 forks source link

Unable to begin transaction: cannot start a transaction within a transaction. #56

Closed svranch closed 7 years ago

svranch commented 7 years ago

I am using the cordova-sqlite-ext plugin for a mobile app I am developing (iOS and Android). I am running in to a problem where I get this ERROR:

Unable to begin transaction: cannot start a transaction within a transaction.

It's really a bummer. 😞 lol So i've tried a number of things - and have found lots of doc'n in the readme. Some things I tried:

  1. Closing the dbase (see Readme > "Close a database object"). For some reason the dbase did not re-open when I switch to the page where the test button is.
  2. Used a callback to make sure the transaction completed. I had an alert pop up to verify transaction was complete. However, I still got the above error.

So it's still not working. It seems my UPDATE transaction is not committed. Sometimes it works upon first start up of the app, but then it won't commit when I go and try to repeat the update with a new value. That's when I get the above error.

I think it might be because I have kicked off two functions which run at the same time but in different directions.

User clicks a link on page 1. This kicks off two functions at the same time: Function 1 Does an UPDATE query adding to a record in the dbase. This is a transaction.executeSql inside a db.transaction. Function 2 Opens a new page and will try to populate a listview with a ... transaction.executeSql SELECT query.
This is also a transaction.executeSql inside a db.transaction.

It's strange cuz neither the built-in error nor success message shows. I only see page 2 open - showing only the header. For some reason the listview has failed to open. I have other error handlers which also fail to fire.

Then I go to a test page and click a button to verify the changed record in the database. Sometimes the record is updated, sometimes I get the above error msg. If i click OK, then click the same button again, this time it shows the record - and it will not be updated.

I am wondering if Function 2 crashes for some reason and this stops Function 1 from completing?

Is this a common problem?

brodybits commented 7 years ago

I am running in to a problem where I get this ERROR:

Unable to begin transaction: cannot start a transaction within a transaction.

It's really a bummer. 😞

From the past 4-5 years of supporting this plugin I have never seen such an error before. Given the level of testing we have done in the test suite, others (such as Nolan Lawson) have done with the PouchDB adapter, and number of users in the field I would be extremely surprised to see this as a bug in the CoffeeScript/JavaScript part.

Considering that this is a multi-page app, my theory is that this happens if the JavaScript part starts a transaction and then the app goes to another page before the transaction finishes.

In case of a multi-page app I think the safest workaround is to execute all SELECTs and changes using db.executeSql instead of using the standard transaction mechanism.

svranch commented 7 years ago

Dude. this is too wierd. I thought the answer was to use JS Promises - so I re-worked the code and the thing still happened (!?!)

frustrating.

So I ended up back here and tried the last paragraph in your response above - and I think that is the answer - so far it is working.

But why do I seem to think you have to use the standard transaction mechanism? Do you know? Is it a rule or standard for sqlite? Or what?

thanks

brodybits commented 7 years ago

I would like to continue the discussion in litehelpers/Cordova-sqlite-storage#666 which I just opened on the main cordova-sqlite-storage version (sorry for the symbolism here).