Library:
Record locking bug, when PostgreSQL, MSSQL or Firebird database is used, fixed
To use record locking for items for which you defined on_apply event handler you must change. Add the connection parameter, create a cursor and use the cursor to execute sql queries. Otherwise the record locking won’t work.
For example, the code
def on_apply(item, delta, params):
tracks_sql = []
delta.update_deleted()
for d in delta:
for t in d.invoice_table:
if t.rec_inserted():
sql = "UPDATE DEMO_TRACKS SET TRACKS_SOLD = COALESCE(TRACKS_SOLD, 0) + \
%s WHERE ID = %s" % \
(t.quantity.value, t.track.value)
elif t.rec_deleted():
sql = "UPDATE DEMO_TRACKS SET TRACKS_SOLD = COALESCE(TRACKS_SOLD, 0) - \
(SELECT QUANTITY FROM DEMO_INVOICE_TABLE WHERE ID=%s) WHERE ID = %s" % \
(t.id.value, t.track.value)
elif t.rec_modified():
sql = "UPDATE DEMO_TRACKS SET TRACKS_SOLD = COALESCE(TRACKS_SOLD, 0) - \
(SELECT QUANTITY FROM DEMO_INVOICE_TABLE WHERE ID=%s) + %s WHERE ID = %s" % \
(t.id.value, t.quantity.value, t.track.value)
tracks_sql.append(sql)
sql = delta.apply_sql()
return item.task.execute(tracks_sql + [sql])
must be changed to
def on_apply(item, delta, params, connection):
with item.task.lock('invoice_saved'):
cursor = connection.cursor()
delta.update_deleted()
for d in delta:
for t in d.invoice_table:
if t.rec_inserted():
sql = "UPDATE DEMO_TRACKS SET TRACKS_SOLD = COALESCE(TRACKS_SOLD, 0) + \
%s WHERE ID = %s" % \
(t.quantity.value, t.track.value)
elif t.rec_deleted():
sql = "UPDATE DEMO_TRACKS SET TRACKS_SOLD = COALESCE(TRACKS_SOLD, 0) - \
(SELECT QUANTITY FROM DEMO_INVOICE_TABLE WHERE ID=%s) WHERE ID = %s" % \
(t.id.value, t.track.value)
elif t.rec_modified():
sql = "UPDATE DEMO_TRACKS SET TRACKS_SOLD = COALESCE(TRACKS_SOLD, 0) - \
(SELECT QUANTITY FROM DEMO_INVOICE_TABLE WHERE ID=%s) + %s WHERE ID = %s" % \
(t.id.value, t.quantity.value, t.track.value)
cursor.execute(sql)