Access to the same website database from the web worker and main browser branch

I searched a little Google and cannot find out if I can access the same websql database from the main ui stream and web worker.

I use async api, because I believe that this is the only API that has been implemented for both the web worker and the main ui thread.

Basically, I get problems when both threads execute a transaction simultaneously with the same database. SQLite supports multiple threads accessing db in a controlled manner, so this should be possible.

Has anyone done this?

+4
source share
3 answers

Employers on the Internet are quite limited in their functions. You cannot access websql or localStorage database. The only solution is to send messages to the main window, which processes updates.

EDIT:

Here is a link to the available webmaster features:

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

+1
source

Considering the frequency of SQLite, there is a question SQLite threadsafe? , which refers to the SQLITE_THREADSAFE compile-time parameter. See Compilation Options for SQLite .

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

The sqlite_compileoption_get () SQL function is a wrapper around the C / C ++ sqlite3_compileoption_get () function. This procedure returns the Nth compile-time parameter used to build SQLite or NULL if N is missing an assortment. See also pragma compile_options.

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

0
source

In Chrome (tested on version 29) you can now access webSql, just don't use window

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

use instead

 var db=openDatabase(....); 

Also, when calling executeSql be sure to write the error message in the second callback function. This is useful 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)}); }); 
0
source

Source: https://habr.com/ru/post/1396259/


All Articles