4

I have googled quite a bit and cannot find out whether I should be able to access the same websql database from the main ui thread and a web worker.

I am using the async api since I believe this is the only API that has been implemented for both the web worker and the main ui thread.

Basically I get issues when both threads execute a transaction concurrently against the same database. SQLite supports multiple threads accessing a db in a controlled way so it should be possible.

Has anyone done this?

paul
  • 494
  • 6
  • 17

3 Answers3

1

Webworkers are quite limit in their features. You can't access a websql database or localStorage. The only work-around would be to send messages to the main-window which handles the updates.

EDIT:

Here is a link to the available webworker functions:

https://developer.mozilla.org/en/DOM/Worker/Functions_available_to_workers

Alex
  • 2,106
  • 16
  • 15
  • This is not correct. See for example http://www.infoq.com/news/2010/02/Web-SQL-Database and the link within on the webkit implementation. Looks like sync and async api web sql database support has been added to web workers. – paul Feb 14 '12 at 22:07
  • Looks like you are right. You know this google groups entry: https://groups.google.com/a/chromium.org/group/chromium-html5/browse_thread/thread/f3b0c9340696656d/76ae45d1e9cc41e7 ? – Alex Feb 14 '12 at 22:27
  • thanks, missed that one. Problem I am having is no one seems to know what you can access safely from 2 threads. I've prooved in latest chrome browser and blackberry playbook browser that accessing the same sqlite db from the main ui thread and worker thread is okay until 2 transactions overlap. I'm not sure if this is a webkit issue or sqlite issue? What local storage is thread safe? I think I will ask as another questions. ta – paul Feb 15 '12 at 14:30
  • Asked question at: http://stackoverflow.com/questions/9295433/what-local-storage-in-html5-can-i-use-safely-in-the-browser-ui-thread-and-the-we – paul Feb 15 '12 at 14:47
0

Looking at the SQLite FAQ, there is a Is SQLite threadsafe? question that refers to a SQLITE_THREADSAFE compile time option. See Compilation Options For SQLite.

Then, in the core functions section of SQL As Understood By SQLite, there is a sqlite_compileoption_get(N) function:

The sqlite_compileoption_get() SQL function is a wrapper around the sqlite3_compileoption_get() C/C++ function. This routine returns the N-th compile-time option used to build SQLite or NULL if N is out of range. See also the compile_options pragma.

I think you could write an SQLite statement and call from your JavaScript to see how PRAGMA compile_options; was set.

Kevin Hakanson
  • 41,386
  • 23
  • 126
  • 155
0

On Chrome (tested on ver 29), you can now have access the webSql, just don't use window

var db=window.openDatabase(....);

instead use

var db=openDatabase(....);

Also when calling executeSql be sure to log the error message in the second callback function. This is helpful to see where the error is.

var db=openDatabase('myDB', '', 'my first database', 2 * 1024 * 1024);
db.transaction(function(tx){
   tx.executeSql('CREATE TABLE IF NOT EXISTS foo (id unique, text)',[],function(tx,results){
      self.postMessage("passed");
   },function(_trans,_error){self.postMessage(_error.message)});
   tx.executeSql('INSERT INTO foo (id, text) VALUES (1, "synergies")',[],function(tx,results){
      self.postMessage("passed");
   },function(_trans,_error){self.postMessage(_error.message)});
});
fredtma
  • 1,007
  • 2
  • 17
  • 27