Friday, August 24th, 2007

Adobe AIR: Can we have synchronous queries, please?

Category: Adobe, Database

Justin Palmer has an interesting post about the dealing with the asynchronous nature of Adobe AIR’s SQLConnection class and the challenges it poses when dealing with the DBMS’ response. Justin shows some creative ways of working around this through the use of Responder objects and event listeners:

var connection = new air.SQLConnection();'development.sqlite'));

var statement = new air.SQLStatement();
statement.sqlConnection = connection;
statement.text = "SELECT * FROM contacts";

The code above, albeit a little long winded, is the bare necessities for executing a query. While it looks fairly straightforward on the surface, we’ve already run into our first problem. If the execute operation is asynchronous, how do we know when we can begin to retrieve and manipulate the data returned? We can do it one of two ways, register an event listener or pass a Responder object to the execute method.

From the looks of this thread it appears this is on the minds of several other folks and Adobe’s working on developing a method for synchronous SQL calls is in the works.

Posted by Rey Bango at 6:00 am

4.1 rating from 19 votes


Comments feed TrackBack URI

the only reason that GearsORM(a orm on top of Google Gears) doesn`t work in AIR is that i didn`t want to switch into a async api.
if adobe will support sync queries i will add support for AIR in GearsORM(and maybe change the name ;) )

Comment by Uriel Katz — August 24, 2007

It would not be so bad if you could have multiple outstanding async database requestes going at one time, but apparently according to the article you must wait for each database request to finish before executing another one.

Comment by Hank — August 24, 2007

This looks a good use case for using continuations/coroutines.
@Hank- I don’t think that is what the article is saying. He just wants to show how to do them one after another because they are dependent on each other.

Comment by Kris Zyp — August 24, 2007


No, having to wait for one asynchronous operation to complete before starting another one is not true but a side effect of how he has his code set up. If you try and reuse the same statement object for multiple operations then trying to change the statement.text property while its still executing on the previous statement will throw an error.

If you don’t care about knowing when all operations are done something like:

for (var i = 0; i

I was dealing with this problem myself and posted what I found to the AIR forums.

Comment by Matthew Reinbold — August 24, 2007

Let’s try that blockquote again:

for (var i = 0; i

Comment by Matthew Reinbold — August 24, 2007

One more time (I apologize – I thought the code tag would rewrite the < and >):

for (var i = 0; i < numToInsert; i++) {
var stmt = new air.SQLStatement();
stmt.sqlConnection = db;
stmt.text = aTextToRun[i];
stmt.addEventListener(air.SQLEvent.RESULT, synchResultHandler);
stmt.addEventListener(air.SQLErrorEvent.ERROR, synchErrorHandler);

Comment by Matthew Reinbold — August 24, 2007

If you are duel deploying your AJAX apps w/ AIR then you should take a serious look at creating a Service Facade. Allowing the main application to make calls to the Service layer w/o having to know whether its connecting to a remote server or local DB. Since the remote connection will always require async connection, you’re local requests should too in order to maintain a standard interface. So AIR’s async database connection becomes a non-issue.

FYI – Just got back from the Boston AIR Bus Tour Event. If you’re on the fence about going, definitely do it. Adobe has a great group of guys presenting.

Comment by Todd Cullen — August 24, 2007

Sounds like that could all be wrapped up using Futures ( quite nicely.

Comment by Adam Sanderson — August 25, 2007

As per my blog post (see website), a feature request to Adobe late last night resulted in a confirmation from their Product Manager that synchronous queries will be available in the next beta release.

Very impressed that they got back to me so quickly. Especially with a positive reply.

Comment by bcg — September 19, 2007

Leave a comment

You must be logged in to post a comment.