MySQL connector issue

Chris Angelico rosuav at gmail.com
Sun Oct 23 12:53:12 EDT 2016


On Mon, Oct 24, 2016 at 3:46 AM, Joseph L. Casale
<jcasale at activenetwerx.com> wrote:
> It really is that simple which is why I am baffled. Given the throughput is so
> low, if I close the cursor and connection at the end of loop and instantiate them
> both at the start of the loop, it works as expected but that's obviously not optimal.

Interesting. Generally, I allocate cursors exactly at the same time as
I open transactions; not sure if this works with the mysql connector,
but with psycopg2 (PostgreSQL), my code looks like this:

with conn, conn.cursor() as cur:
    cur.execute(...)
    ... = cur.fetchall()

The 'with' block guarantees that (a) the cursor will be closed and all
resources freed, and (b) the transaction will be committed or rolled
back (on exception, roll back, otherwise commit), at the unindent.
It's a nice, clean way to operate. Never had problems with it.

ChrisA



More information about the Python-list mailing list