Question: 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

Answer: 
You have a valid underlying concern here, however it's also important to understand how sqlite operates too:

```
1. connection open
    2. transaction started
        3. statement executes
    4. transaction done
5. connection closed

```

in terms of **data correctness**, you only need to worry about transactions and not open handles. sqlite only holds a lock on a database inside a transaction(\*) or statement execution.

however in terms of **resource management**, e.g. if you plan to remove sqlite file or use so many connections you might run out of file descriptors, you do care about open out-of-transaction connections too.

there are two ways a connection is closed: either you call `.close()` explicitly after which you still have a handle but can't use it, or you let the connection go out of scope and get garbage-collected.

if you **must close a connection**, close it explicitly, according to Python's motto "**explicit is better than implicit**."

if you are only checking code for side-effects, letting a last variable holding reference to connection go out of scope may be acceptable, but keep in mind that exceptions capture the stack, and thus references in that stack. if you pass exceptions around, connection lifetime may be extended arbitrarily.

**caveat programmator**, sqlite uses "deferred" transactions by default, that is the transaction only starts when you execute a statement. In the example above, transaction runs from 3 to 4, rather than from 2 to 4.
Your version leaves conn in scope after connection usage.

EXAMPLE:

**your version**

```
    conn = sqlite3.connect(db_filename) #DECLARE CONNECTION OUT OF WITH BLOCK

    with conn:                          #USE CONNECTION IN WITH BLOCK
        cur = conn.cursor()
        cur.execute( ... )

   #conn variable is still in scope, so you can use it again

```

**new version** 

```
    with sqlite3.connect(db_filename) as conn:  #DECLARE CONNECTION AT START OF WITH BLOCK
        cur = conn.cursor()
        cur.execute( ... )   

   #conn variable is out of scope, so connection is closed 
   # MIGHT BE IT IS NOT CLOSED BUT WHAT  Avaris SAID!
   #(I believe auto close goes for with block)

```