Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I keep Sqlite connection open?

Should I be keeping sqlite connections open or should I close it every time I'm done with my batch queries?

Update: This is specific to iPhone/iOS if that makes a difference.

like image 874
Andrew Young Avatar asked Feb 23 '11 08:02

Andrew Young


People also ask

Do you need to close SQLite connection?

The sqlite3 module connect() ; the connection must be closed at the end of the session with the . close() command. While the connection is open, any interactions with the database require you to make a cursor object with the . cursor() command.

Should I keep a database connection open?

If you write/read every second I would definitely keep the connection open. If you only access the DB every hour I would probably close the connection. In most cases I would tend to keep the connection open as long as your application runs.

When should a SQLite database be closed?

The SQLite connection object remains open until you close it using the close function. Always close this object when you finish using it.

What is SQLite connection?

SQLite is a C library that provides a lightweight disk-based database that doesn't require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage.


1 Answers

My general recommendation given that the DB interaction is user driven, I'd say open, do your activities, close it and then return to the user. This leaves nothing up in the air. There tends to be an over-emphasis on performance. The best approach on single user applications I find is to assume that performance is going to be fine, and then prove it otherwise.

One of the ways in which to prove this to yourself is right some test code to time how long it's taking to open and close a connection. Next is to try to sets of actions, one with each strategy. This should convince you.

The downsides to leaving your connection open generally outweighs (again, in single user applications) the benefits of keeping it open.

That's my 2 cents,

like image 142
Driss Zouak Avatar answered Sep 30 '22 17:09

Driss Zouak