title: Is __enter__ and __exit__ behaviour for connection objects specified in the Python database API?
body: <h1>Background</h1>
  
  <p>I recently discovered the Python <code>with</code> keyword and started seeing its potential usefulness for more prettily handling some scenarios where I'd previously have used <code>try: ... finally: ...</code> constructs. I immediately decided to try it out on the MySQLdb connection object in some code I was writing.</p>
  
  <p>I didn't bother reading up on how <code>__enter__</code> and <code>__exit__</code> behave in implementors of the Python database API, and naively expected the behaviour to be like that of file objects - all I was expecting was for exit to call <code>connection.close()</code>.</p>
  
  <p>Imagine my confusion, then, at this behaviour:</p>
  
  <pre><code>&gt;&gt;&gt; with util.get_db_connection() as conn:
  ...     print conn
  ... 
  &lt;MySQLdb.cursors.Cursor object at 0xb6ca8b4c&gt;
  </code></pre>
  
  <p><code>get_db_connection()</code> returns a MySQLdb connection object, but the <code>__enter__</code> method of that connection object returns a cursor object, not the connection object itself like I was expecting given how <code>__enter__</code> and <code>__exit__</code> work for file objects. I guess I ought to be doing <code>with util.get_db_connection() as cursor:</code>, or else not using <code>with</code> at all.</p>
  
  <h1>Questions</h1>
  
  <p>Immediately this discovery makes me wonder a few things:</p>
  
  <ol>
  <li>What else do the <code>__enter__</code> and <code>__exit__</code> methods of MySQLdb connection objects do? Is <code>__exit__</code> going to magically commit or rollback changes for me without me explicitly asking for that to happen? Is there anything else non-obvious that I should know?</li>
  <li>Is this behaviour the same in other implementers of the Python database API (like sqlite3, django, or psycopg2)?</li>
  <li>Is this behaviour formally specced anywhere? <code>ctrl-f</code>ing the latest spec (<a href="http://www.python.org/dev/peps/pep-0249/" rel="noreferrer">PEP 249 -- Python Database API Specification v2.0</a>) for 'enter', 'exit' and 'context manager' doesn't throw up anything.</li>
  </ol>