简体   繁体   English

JavaScript WebSQL线程锁定安全

[英]JavaScript WebSQL Thread Locking Safety

So I have a Javascript Class that wraps and manages access to the WebSQL Database system. 因此,我有一个Javascript类,用于包装和管理对WebSQL数据库系统的访问。

the code for the class can be found: https://jsfiddle.net/dsct89kv/ 可以找到该类的代码: https : //jsfiddle.net/dsct89kv/

now to test it I'm using 现在测试我正在使用

function(){
    var test = new Database();
    test.open("test");
    test.query("CREATE TABLE `logs` (id INTEGER PRIMARY KEY, value VARCHAR)");
    test.query("SELECT * FROM `logs`"); 
    test.waitForBlockLift(); 
    console.log(test.fetchRows());
  }

if I run all of these line by line one after another in the console it works perfectly but if I run the group it becomes thread locked on test.waitForBlockLift(); 如果我在控制台中一行接一行地运行所有这些功能,则效果很好,但是如果运行该组,则该线程将锁定在test.waitForBlockLift();

so that is defined as 因此定义为

this.isBlocked = function(){ return blocking; };

this.waitForBlockLift = function(){
    var test = this.isBlocked();
    while(test){
        test = this.isBlocked();
    }
    return true;
}

the initial value of blocking = false when a query is called test.query it is set to true and once the transaction has completed and called the callback then set it back to false but for some reason when I call in a single line from the Console eg test.query("SELECT * FROM logs"); test.waitForBlockLift(); console.log(test.fetchRows()); 当查询被称为test.query时, blocking的初始值= false设置为true,一旦事务完成并调用了回调,然后将其设置回false,但是由于某种原因,当我从控制台中单行调用时例如test.query("SELECT * FROM logs"); test.waitForBlockLift(); console.log(test.fetchRows()); test.query("SELECT * FROM logs"); test.waitForBlockLift(); console.log(test.fetchRows()); that does not happen the javascript engine works as I can still use the console. javascript引擎无法正常运行,因为我仍然可以使用控制台。 however, I can't access test and the tread appears to lock the whole point was to enable it to wait for the thread to unlock. 但是,我无法访问测试,胎面似乎锁定了整个点,是为了使其能够等待线程解锁。

I must be doing something wrong but can't work out what 我一定在做错事,但无法解决

In the main thread of Javascript, this construct: 在Javascript主线程中,此构造:

this.isBlocked = function(){ return blocking; };

while(test){
    test = this.isBlocked();
}

is an infinite loop. 是一个无限循环。 That's because the main execution path in Javascript is single threaded. 这是因为Javascript中的主要执行路径是单线程。 So, while you're looping there in that single thread, nothing else can ever run. 因此,当您在那个单线程中循环时,其他任何事情都无法运行。 Therefore the blocking variable can never be changed, thus your loop runs forever. 因此, blocking变量永远无法更改,因此循环将永远运行。 You can't do a wait loop like this in Javascript. 您无法在Javascript中进行这样的等待循环。

Instead, you have to use completion callbacks to know when asynchronous operations are done. 相反,您必须使用完成回调来了解异步操作何时完成。 You don't show any of your actual database code, but all database operations should have callbacks that will tell you when they are done. 您没有显示任何实际的数据库代码,但是所有数据库操作都应具有回调,这些回调将告诉您何时完成操作。 You will need to use those to know when operations are complete. 您将需要使用它们来了解操作何时完成。 You pass a callback and it will call you when things are done. 您传递一个回调,完成后它将调用您。 This then allows other code to run while you are waiting for the callback function to be called. 然后,这将在您等待调用回调函数时运行其他代码。


Per your comments, in Javascript, you would not do this: 根据您的评论,在Javascript中,您不会这样做:

test.query("SELECT * FROM logs");
console.log(test.fetchRows());

Instead, you would use a completion callback to do something like this: 相反,您将使用完成回调来执行以下操作:

test.query("SELECT * FROM logs", function(result) {
    console.log(test.fetchRows());
});

Or, if you returned a promise from test.query() , you could code it like this: 或者,如果您从test.query()返回了promise,则可以这样编码:

test.query("SELECT * FROM logs").then(function(result) {
    console.log(test.fetchRows());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM