HTML 5 Web SQL Database Transaction commit or rollback on page refresh

As written in the Safari Client-Side Storage and stand-alone application programming guide , the HTML 5 Web SQL Database transaction is rolled back by returning true in the callback function provided as an error callback for the transaction of the executeSql method:

Callback error handling callback is pretty simple. If the callback returns true, the entire transaction is rolled back. If the callback returns false, the transaction continues, as if nothing went wrong. Thus, if you are executing a request that is optional, if the failure of this particular request should not cause a fail transaction, you must pass a callback that returns false. If the request must cause the entire transaction to fail, you must pass in a callback that returns true.

For example, if I have the following transaction (suppose that the "users" table has a UNIQUE constraint in the "username" field, and the username "test" already exists, which I am trying to insert again, which should lead to a restriction error):

 database.transaction(function(transaction) { transaction.executeSql( "INSERT INTO users (username) VALUES('test')", null, dataCallback, errorCallback ); }); function errorCallback() { return true; //this causes the rollback } 

I have two questions:

  • If I need to enable many operations inside a transaction (for example, I have to send some data using ajax to the server and wait for a response, etc.), and the user will reload the page before the answer arrives (this means that errorCallback will not be called ), will the transaction be completed or it will not work?

  • Does anyone know how to roll back a Web SQL transaction manually? For example, if I want to cancel a transaction based on the result of an ajax call, how can this be done? Should I run a query containing an error to make sure the error callback is called?

Thanks.

+6
javascript database html5 transactions web-sql
source share
2 answers
  • The transaction will be completed.
  • Yes, for rollback explicitly, you must explicitly call an invalid request. This is recommended for a workaround, since there is no simple and convenient abort API.

For AJAX, prepare all the data before starting a write transaction. You will not have problems as you described. Use a database constraint (UNIQUE, FOREIGNKEY) as much as possible.

+2
source share

Did you find a way to make AJAX calls during a transaction? I have not finished reading the entire specification, but so far it is like once your SQLTransactionCallback or SQLTransactionSyncCallback back, you cannot add more things to the transaction - or can you? Maybe from a results callback?

Edit: Now, when I look again, the spec (which contains far fewer errors than the Apple document you linked to but not so easy to read) says this :

  • If the [ executeSql ] method was not called during SQLTransactionCallback , SQLStatementCallback or SQLStatementErrorCallback , then raise the INVALID_STATE_ERR exception.

So I think that means that there is no way to do this.

Further editing: No, wait! While the SQLStatementCallback takes some time to receive the call, you can wait for select 3 + 4 repeatedly, repeating each time from the callback of the previous select 3 + 4 , until your AJAX call sets the flag somewhere, has the necessary data. This is scary programming (it will take a lot of processor for no good reason, it may block tasks with lower priority, such as redrawing the page), but I think this is probably the only way to keep the transaction open for an arbitrary period of time. Too bad, you cannot select 3 + 4, sleep(1) in SQLite.

In general, SQLite (the primary storage engine here) rolls back transactions in progress. I have not tested the page reload error case you are asking about yet. I would be very surprised if this were done.

By the way, many thanks for posting this question. I tried to figure out how to cancel a transaction, even if it was carefully documented in the original specification.

+1
source share

All Articles