Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What if I don't close the database connection in Python SQLite

I am doing something like this...

conn = sqlite3.connect(db_filename)  with conn:     cur = conn.cursor()     cur.execute( ... ) 

with automatically commits the changes. But the docs say nothing about closing the connection.

Actually I can use conn in later statements (which I have tested). Hence it seems that the context manager is not closing the connection.

Do I have to manually close the connection. What if I leave it open?

EDIT

My findings:

  • The connection is not closed in the context manager, I have tested and confirmed it. Upon __exit__, the context manager only commits the changes by doing conn.commit()
  • with conn and with sqlite3.connect(db_filename) as conn are same, so using either will still keep the connection alive
  • with statement does not create a new scope, hence all the variables created inside the suite of with will be accessible outside it
  • Finally, you should close the connection manually
like image 698
treecoder Avatar asked Mar 05 '12 04:03

treecoder


People also ask

Do I need to close connection in Python?

Connections are automatically closed when they are deleted (typically when they go out of scope) so you should not normally need to call [ conn. close() ], but you can explicitly close the connection if you wish. and similarly for cursors (my emphasis):

Should I close connection to database?

If you do not close your database connections, many problems can occur like web pages hanging, slow page loads, and more. Think of it as going through a door to your house. Maybe the door will shut by itself, but maybe it won't. If it doesn't shut, who knows what will happen.

Should I close DB connection after query?

For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.

How do I close a SQLite database connection?

close( conn ) closes the SQLite connection by using the MATLAB® interface to SQLite. The SQLite connection object remains open until you close it using the close function. Always close this object when you finish using it.


1 Answers

In answer to the specific question of what happens if you do not close a SQLite database, the answer is quite simple and applies to using SQLite in any programming language. When the connection is closed explicitly by code or implicitly by program exit then any outstanding transaction is rolled back. (The rollback is actually done by the next program to open the database.) If there is no outstanding transaction open then nothing happens.

This means you do not need to worry too much about always closing the database before process exit, and that you should pay attention to transactions making sure to start them and commit at appropriate points.

like image 190
Roger Binns Avatar answered Sep 30 '22 22:09

Roger Binns