What is more preferable way of using psycog2 cursor?

Viewed 127

For handle postgresql I use python library psycog2.

To make a transactions I can choose between:

with psycopg2.connect(params) as conn:
    with conn.cursor() as cur:
        cur.execute(sql)

or

try:
    con = psycopg2.connect(params) 
    cursor = con.cursor() 
    cursor.execute(sql)        
    con.commit()
except psycopg2.DatabaseError, e:
        con.rollback()

What is way more preferable (in the terms of speed and thread safety)?

I guess the second one is some better because I can use cursor more times than in the first approach (when cursor is needed to created every time). But what about thread safety?

0 Answers
Related