与异步执行模式的代码相比,使用同步执行时用于执行和响应操作的实际代码几乎没有差异。两种方法之间的主要差异体现在以下两个方面。首先,执行一个依赖于另一个操作(如
SELECT
结果行或由
INSERT
语句添加的行的主键)的操作。第二方面的差异体现在处理错误上。
为同步操作编写代码
同步执行和异步执行的主要差异在于:在同步模式下,以单个步骤系列的形式编写代码。相反,在异步代码中,注册事件侦听器,并经常在侦听器方法之间分配操作。当在
同步执行模式
下连接数据库时,可以在单个代码块中连续执行一系列数据库操作。以下示例对此技术进行了演示:
// Include AIRAliases.js to use air.* shortcuts
var conn = new air.SQLConnection();
// The database file is in the application storage directory
var folder = File.applicationStorageDirectory;
var dbFile = folder.resolvePath("DBSample.db");
// open the database
conn.open(dbFile, air.OpenMode.UPDATE);
// start a transaction
conn.begin();
// add the customer record to the database
var insertCustomer = new air.SQLStatement();
insertCustomer.sqlConnection = conn;
insertCustomer.text =
"INSERT INTO customers (firstName, lastName) " +
"VALUES ('Bob', 'Jones')";
insertCustomer.execute();
var customerId = insertCustomer.getResult().lastInsertRowID;
// add a related phone number record for the customer
var insertPhoneNumber = new air.SQLStatement();
insertPhoneNumber.sqlConnection = conn;
insertPhoneNumber.text =
"INSERT INTO customerPhoneNumbers (customerId, number) " +
"VALUES (:customerId, '800-555-1234')";
insertPhoneNumber.parameters[":customerId"] = customerId;
insertPhoneNumber.execute();
// commit the transaction
conn.commit();
如您所看到的,不管使用的是同步执行还是异步执行,都调用相同的方法来执行数据库操作。两种方法的主要差异在于:执行一个依赖于另一个操作的操作和处理错误。
执行一个依赖于另一个操作的操作
使用同步执行模式时,无需编写侦听事件的代码来确定操作完成的时间。相反,可以假定如果一个代码行中的操作成功完成,则继续执行下一代码行。因此,要执行一个依赖于另一个操作成功的操作,只需在紧随它所依赖的操作之后编写相关代码即可。例如,要为应用程序编码以开始事务,可执行
INSERT
语句,检索已插入行的主键,将该主键插入到不同表的其他行中,最后提交事务,可以将代码全部编写为一系列语句。以下示例对这些操作进行了演示:
// Include AIRAliases.js to use air.* shortcuts
var conn = new air.SQLConnection();
// The database file is in the application storage directory
var folder = File.applicationStorageDirectory;
var dbFile = folder.resolvePath("DBSample.db");
// open the database
conn.open(dbFile, air.OpenMode.UPDATE);
// start a transaction
conn.begin();
// add the customer record to the database
var insertCustomer = new air.SQLStatement();
insertCustomer.sqlConnection = conn;
insertCustomer.text =
"INSERT INTO customers (firstName, lastName) " +
"VALUES ('Bob', 'Jones')";
insertCustomer.execute();
var customerId = insertCustomer.getResult().lastInsertRowID;
// add a related phone number record for the customer
var insertPhoneNumber = new air.SQLStatement();
insertPhoneNumber.sqlConnection = conn;
insertPhoneNumber.text =
"INSERT INTO customerPhoneNumbers (customerId, number) " +
"VALUES (:customerId, '800-555-1234')";
insertPhoneNumber.parameters[":customerId"] = customerId;
insertPhoneNumber.execute();
// commit the transaction
conn.commit();
在同步执行时处理错误
在同步执行模式下,不侦听错误事件来确定操作是否已失败。相反,将可能触发错误的任何代码括在一组
try..catch..finally
代码块中。将引发错误的代码包装在
try
块中。在单独的
catch
块中,编写响应每种类型的错误时要执行的操作。在
finally
块中放置不管成功还是失败(例如,关闭不再需要的数据库连接)都希望始终执行的任何代码。以下示例演示如何使用
try..catch..finally
块进行错误处理。它建立在前面示例的基础之上,添加了错误处理代码:
// Include AIRAliases.js to use air.* shortcuts
var conn = new air.SQLConnection();
// The database file is in the application storage directory
var folder = File.applicationStorageDirectory;
var dbFile = folder.resolvePath("DBSample.db");
// open the database
conn.open(dbFile, air.SQLMode.UPDATE);
// start a transaction
conn.begin();
try
{
// add the customer record to the database
var insertCustomer = new air.SQLStatement();
insertCustomer.sqlConnection = conn;
insertCustomer.text =
"INSERT INTO customers (firstName, lastName)" +
"VALUES ('Bob', 'Jones')";
insertCustomer.execute();
var customerId = insertCustomer.getResult().lastInsertRowID;
// add a related phone number record for the customer
var insertPhoneNumber = new air.SQLStatement();
insertPhoneNumber.sqlConnection = conn;
insertPhoneNumber.text =
"INSERT INTO customerPhoneNumbers (customerId, number)" +
"VALUES (:customerId, '800-555-1234')";
insertPhoneNumber.parameters[":customerId"] = customerId;
insertPhoneNumber.execute();
// if we've gotten to this point without errors, commit the transaction
conn.commit();
}
catch (error)
{
// rollback the transaction
conn.rollback();
}
|
|
|