1var complete = 0;
2
3function checkCompletion()
4{
5    // The test should end after two transactions
6    if (++complete == 2 && window.layoutTestController)
7        layoutTestController.notifyDone();
8}
9
10// Opens the database used in this test case
11function openTestDatabase()
12{
13    return openDatabaseWithSuffix("MultipleTransactionsOnDifferentHandlesTest",
14                                  "1.0",
15                                  "Test to make sure that queueing multiple transactions on different DB handles does not result in a deadlock.",
16                                  32768);
17}
18
19function statementSuccessCallback(dbName, statementType)
20{
21    log(dbName + " " + statementType + " statement succeeded");
22}
23
24function statementErrorCallback(dbName, statementType, error)
25{
26    log(dbName + " " + statementType + " statement failed: " + error.message);
27}
28
29// Runs a transaction on the given database
30function runTransaction(db, dbName, val)
31{
32    db.transaction(function(tx) {
33       // Execute a read-only statement
34       tx.executeSql("SELECT COUNT(*) FROM Test;", [],
35                     function(result) { statementSuccessCallback(dbName, "read"); },
36                     function(tx, error) { statementErrorCallback(dbName, "read", error); });
37
38       // Execute a write statement to make sure SQLite tries to acquire an exclusive lock on the DB file
39       tx.executeSql("INSERT INTO Test VALUES (?);", [val],
40                     function(result) { statementSuccessCallback(dbName, "write"); },
41                     function(tx, error) { statementErrorCallback(dbName, "write", error); });
42       }, function(error) {
43           // Transaction failure callback
44           log(dbName + " transaction failed: " + error.message);
45           checkCompletion();
46       }, function() {
47           // Transaction success callback
48           log(dbName + " transaction succeeded");
49           checkCompletion();
50       });
51}
52
53// We need to guarantee that the Test table exists before we run our test.
54// Therefore, the test code is in the successCallback of the transaction that creates the table.
55function runTest() {
56    try {
57        var db = openTestDatabase();
58        db.transaction(function(tx) {
59            // Create the Test table if it does not exist
60            tx.executeSql("CREATE TABLE IF NOT EXISTS Test (Foo int);", [],
61                          function(result) {}, function(tx, error) {});
62            }, function(error) {
63                log("Creating the Test table failed: " + error.message);
64            }, function() {
65                // The Test table was created successfully
66                var db1 = openTestDatabase();
67                var db2 = openTestDatabase();
68                if (db1 == db2)
69                    log("failure: db1 == db2");
70                else {
71                    runTransaction(db1, "db1", 1);
72                    runTransaction(db2, "db2", 2);
73                }
74            });
75    } catch(err) {}
76}
77
78
79