0

I have a an app composed of multiple classes. In most of these classes I need to access the data located in an SQLite3 DB of words that contains 60,000 rows (Note: each row is composed of a word, it's POS tag, and frequency).

I don't know whether it is better to populate a list of tuples and pass it to my classes(as a parameter or a global) or just pass the connection to them, knowing that I will be doing very little filtering like: (SELCT * WHERE pos = 'N' ORDER BY frequency DESC). Which one would be more efficient as my app uses this data frequently.

If I use the DB connection: Is not closing a DB connection and reopening it frequently, resource consuming? or I should keep the connection open? which leads to the problem of knowing where to close the connection. could somebody please suggest a proper place to close the DB connection? or a connection gets closed when the app terminates.

PS: It is apparent from my question that I am a total newbie, so excuse me if I am asking a lot of question.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
K.H.A.
  • 376
  • 3
  • 12
  • Just keep the SQLite connection open for the lifetime of the application (or until *all* connection resources need to be guaranteed to be released earlier - such as "at request end" - which does not sound applicable here). SQLite automatically locks/unlocks the database file as appropriate. The key is to *scope/use transactions correctly*. – user2246674 Jul 21 '13 at 01:54
  • (I've heard arguments to let the SQLite connection be "closed automatically" when the process ends as the file is an OS-controlled resource, but I prefer to be explicit and consistent so that the library - whatever it is - is given an appropriate chance to cleanup and finalize itself.) – user2246674 Jul 21 '13 at 01:57
  • Thank you very much. I will just keep it open and leave it to get closed automatically. **Is there anything like a global destructor to do some cleaning up?** – K.H.A. Jul 21 '13 at 14:57
  • You mean an "at program exit hook" in Python? – user2246674 Jul 21 '13 at 21:13
  • Yes, where is the program exit hook? Will not it be closed as the interpreter will be terminated? – K.H.A. Jul 21 '13 at 21:26
  • There is "atexit", however, do not use it here. A program should should - except in exceptional cases - have a clean exit point. In the case of SQLite, while I recommend closing the connection explicitly at the appropriate scope (which may be *less* than the program), *as long as the transactions are complete it is 100% OK to not close the connection when the program terminates*. This is because SQLite ACID is based around transactions, not connections. – user2246674 Jul 21 '13 at 21:28

0 Answers0