Saturday, July 12, 2008

something about closing and reopening a SQLite connection in AIR

Recently, we had a 'strange' problem in an AIR application that I am working on.

Here's the scenario:
The application already had a SQLConnection open to a SQLite database, and it was open syncronously in READ mode. I wanted to write some data into the same database, so decided to close the connection, reopen it, now in UPDATE mode. That way, I can write data, and keep the connection open so that the rest of the code can continue using the connection as well.

This is what I did:
  • I issued a conn.close();
  • I did a trace(conn.connected) //to check if the connection is closed
  • And finally, a conn = null; // just in case ;)
And then, went on to create a new SQLConnection object into the same variable. And on the connection open event handler, created a SQLStatement object with the UPDATE table SQL text and executed it. Everything looked fine, and I tested the code, only to see a runtime error stating something like "operation cannot be performed on a closed connection" :-/

Now let me get it straight. I was executing the command in the SQLConnection open event handler. And the error said the connection is closed ??

After spending a good chunk of time trying to investigate it, adding a responder to the conn.close() call, and creating a new connection object after that responder was triggered, solved the problem.

Wonder why, being a synchronous connection still had something running in the background, when my code had moved on to create the new connection, and open it as well.

Guess its one of those things...

No comments: