HTML 5 Web SQL Database Transaction commit or rollback when refreshing page
- The transaction will be committed.
- Yes, to rollback explicitly, you must invoke invalid query explicitly. This is recommended workaround since the quick-and-dirty API is missing
abort
method.
Regarding AJAX, be ready all data, before you start a write transaction. You won't have any problem as you described. Use database constraint (UNIQUE, FOREIGNKEY) as much possible.
Have you found a way to do AJAX calls during the transaction? I haven't finished reading the whole spec, but so far it looks like once your SQLTransactionCallback
or SQLTransactionSyncCallback
returns, you can’t add any more stuff to the transaction — or can you? Maybe from the results callback?
Edit:Now that I look again, the spec (which contains many fewer errors than the Apple document you linked to, but is not as easy to read) says this:
- If the method [
executeSql
] was not invoked during the execution of aSQLTransactionCallback
,SQLStatementCallback
, orSQLStatementErrorCallback
then raise anINVALID_STATE_ERR
exception.
So I think that means there's no way to do it.
Further edit: No, wait! As long as the SQLStatementCallback
takes some time to get called, you could busy-wait doing select 3 + 4
over and over again, each time from the statement callback of the previous select 3 + 4
, until your AJAX call sets a flag somewhere that has the data you want. This is terrible programming (it'll eat lots of CPU for no good reason, might block lower-priority tasks like redisplaying the page) but I think it's probably the only way to keep a transaction open for an arbitrary period of time. Too bad you can't select 3 + 4, sleep(1)
in SQLite.
In general SQLite (the underlying storage engine here) rolls back uncompleted transactions. I haven’t yet tested the page-reload error case you're asking about. I would be very surprised if it was committed.
By the way, thank you very much for posting this question. I had been trying to figure out how to get the transaction to rollback, even though it is meticulously documented in the original spec.