cursort.execute/cursor.callproc returns no error but nothing is executed

4k views Asked by At

When i run this SQL query via psql client it runs for several seconds (~90 seconds, that's normal since its a huge table) and it return, then i can check that my line is successfully inserted.

SELECT merge_data('898989', '111111111', '10000')

It is a stored procedure that runs an UPDATE or INSERT, the procedure is ran without errors and i get my entry in the table.

When trying to do the same from a python program, the query take 2 seconds and returns no errors, i get nothing in my table; for information, the statement is successfully executed on postgresqk backed (i can see it in pgsql logs), here's my code snippet:

conn = psycopg2.connect("...")
cursor = conn.cursor()

try:
    cursor.callproc("merge_data", ['898989', '111111111', '10000'])
except:
    print "ERROR !"

cursor.close()
2

There are 2 answers

0
zfou On BEST ANSWER

Resolved !

By default, psycopg2 doesnt autocommit on each executed statement, so once i close the connection (at the end) all pending transactions are discarded as if a ROLLBACK was performed.

One may call conn.commit() after each cursor.execute() or .callproc, or just set it at a global scope using

conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
0
DR-MATTH On

conn.commit() should be used instead changing the isolation level, unless you realy need the autocommit feature.

Python DB2 API