Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is closing a database opened with window.openDatabase necessary?

The code at the moment reads something in the order of...

DoAnything() {
  OpenTheDatabase()
  // ... Do all the things! ...
}

However, the database object is never closed. This is worrisome.

The database is opened as follows:

var db = window.openDatabase( ... paramters ... );

No .closeDatabase function exists, or the documentation is incomplete. I thought the following might suffice:

db=null;

I see that sqlite3_close(sqlite3*) and int sqlite3_close_v2(sqlite3*) exist, but I'm unsure how to apply them in this case.

How do I close the database, and is it necessary?

like image 627
Mark Avatar asked Dec 27 '22 14:12

Mark


1 Answers

Generally you only have one database connection that you open on app startup, and there is no need to close it while the app is open. It's a single threaded, single user app, so a lot of the normal rules about database connections don't apply.

When the app shuts down, you can rely on the browser to close everything - given the average quality of code on the web, browsers have to be pretty good at cleanup.

Setting db to null and letting the garbage collector do its thing will probably also work, but it is better not to create the extra objects in the first place.

like image 134
Tom Clarkson Avatar answered Feb 06 '23 11:02

Tom Clarkson