Issue 20: add context manager support to connection to automatically close
Status:  WontFix
Owner: ----
Closed:  Jan 2009
Reported by brian.br...@gmail.com, Jan 2, 2009
Would like to write code like this:

with pyodbc.connection(conn_string) as conn:
    #do cool things here



Jan 2, 2009
Project Member #1 mkleehammer
I have considered doing this, only because the context manager documentation made
such a big deal out of it, but I first have to ask why.

You can already do this:

def x():
   cnxn = pyodbc.connect(...)
   # do something
   return row.count

Python's C implementation uses reference counting, so as long as you didn't stash a
reference to the connection anywhere, it goes out of scope and is closed
automatically.  Same for a cursor.  This works when exceptions are thrown or if you
exit normally.

Obviously you aren't going to be using pyodbc with Jython, so that can't be an issue.

I guess I can see wanting to close a connection in the middle of a function, but you
can just put cnxn=None and your done.  Generally you don't have to worry about
exceptions if they escape the function since that will close your connection too.

The only scenario I've come up with so far is:

def func():
   try:
      cnxn = ...
   except:
      ...

   morecodehere()

If you want the connection closed before morecodehere and you don't want to put in
two cnxn.close() or cnxn = None statements.  I've written a lot of code with pyodbc
and I haven't hit this yet.

I'm not opposed to adding this feature, but can you provide a use case?

Labels: -Type-Defect Type-Enhancement
Jan 2, 2009
#2 brian.br...@gmail.com
I agree with you that this isn't necessarily needed.  My thought was that it would be
nice to have some consistency across APIs when working with external resources.  I
think it would be nice as a developer to know that when working with files (a.k.a.
the "open" function) and database connections, I could use the "with - as" syntax and
guarantee closure of these resources.  I think this would be easier for new Python
devs to grasp and would lead to potentially better code.  New Python devs may not be
aware of reference counting... just a thought.  I could be totally wrong. :)
Jan 2, 2009
Project Member #3 mkleehammer
I do like consistency, but I would think it should go the other direction.  I
personally never use the with statement because it has been unnecessary for me.  In
fact, I'm concerned it was requested by developers who don't understand how the
reference counting works.

As for new developers, not having to put try/finally everywhere is one of the
attractive features I've Python, IMHO.  I almost don't want new developers to
discover the with statement until they have a solid understanding of the way Python
works.

Let me continue to hold this for a while.  Perhaps we should try starting a
discussion on the groups?
Labels: -Priority-Medium Priority-Low
Jan 2, 2009
Project Member #4 mkleehammer
typo above: I've Python --> of Python
Jan 23, 2009
Project Member #5 mkleehammer
(No comment was entered for this change.)
Status: WontFix