13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with local SQL databases in AIR<br />

Best practices for working with local SQL databases<br />

Adobe AIR 1.0 and later<br />

The following list is a set of suggested techniques you can use to improve the performance, security, and ease of<br />

maint<strong>en</strong>ance of your applications wh<strong>en</strong> working with local SQL databases.<br />

Pre-create database connections<br />

Adobe AIR 1.0 and later<br />

Ev<strong>en</strong> if your application doesn’t execute any statem<strong>en</strong>ts wh<strong>en</strong> it first loads, instantiate a SQLConnection object and<br />

call its op<strong>en</strong>() or op<strong>en</strong>Async() method ahead of time (such as after the initial application startup) to avoid delays<br />

wh<strong>en</strong> running statem<strong>en</strong>ts. See “Connecting to a database” on page 725.<br />

Reuse database connections<br />

Adobe AIR 1.0 and later<br />

If you access a certain database throughout the execution time of your application, keep a refer<strong>en</strong>ce to the<br />

SQLConnection instance, and reuse it throughout the application, rather than closing and reop<strong>en</strong>ing the connection.<br />

See “Connecting to a database” on page 725.<br />

Favor asynchronous execution mode<br />

Adobe AIR 1.0 and later<br />

Wh<strong>en</strong> writing data-access code, it can be tempting to execute operations synchronously rather than asynchronously,<br />

because using synchronous operations frequ<strong>en</strong>tly requires shorter and less complex code. However, as described in<br />

“Using synchronous and asynchronous database operations” on page 752, synchronous operations can have a<br />

performance impact that is obvious to users and detrim<strong>en</strong>tal to their experi<strong>en</strong>ce with an application. The amount of<br />

time a single operation takes varies according to the operation and particularly the amount of data it involves. For<br />

instance, a SQL INSERT statem<strong>en</strong>t that only adds a single row to the database takes less time than a SELECT statem<strong>en</strong>t<br />

that retrieves thousands of rows of data. However, wh<strong>en</strong> you’re using synchronous execution to perform multiple<br />

operations, the operations are usually strung together. Ev<strong>en</strong> if the time each single operation takes is very short, the<br />

application is froz<strong>en</strong> until all the synchronous operations finish. As a result, the cumulative time of multiple operations<br />

strung together may be <strong>en</strong>ough to stall your application.<br />

Use asynchronous operations as a standard approach, especially with operations that involve large numbers of rows.<br />

There is a technique for dividing up the processing of large sets of SELECT statem<strong>en</strong>t results, described in “Retrieving<br />

SELECT results in parts” on page 741. However, this technique can only be used in asynchronous execution mode.<br />

Only use synchronous operations wh<strong>en</strong> you can’t achieve certain functionality using asynchronous programming,<br />

wh<strong>en</strong> you’ve considered the performance trade-off that your application’s users will face, and wh<strong>en</strong> you’ve tested your<br />

application so that you know how your application’s performance is affected. Using asynchronous execution can<br />

involve more complex coding. However, remember that you only have to write the code once, but the application’s<br />

users have to use it repeatedly, fast or slow.<br />

In many cases, by using a separate SQLStatem<strong>en</strong>t instance for each SQL statem<strong>en</strong>t to be executed, multiple SQL<br />

operations can be queued up at one time, which makes asynchronous code like synchronous code in terms of how the<br />

code is writt<strong>en</strong>. For more information, see “Understanding the asynchronous execution model” on page 756.<br />

Last updated 6/6/2012<br />

775

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!