Database Reference
In-Depth Information
Python 2.6 or newer to wrap a group of database interactions in one subtransaction
in a more Pythonic way:
hannu=# CREATE TABLE test_ex(i int);
CREATE TABLE
hannu=# DO LANGUAGE plpythonu $$
hannu$# plpy.execute('insert into test_ex
values(1)')
hannu$# try:
hannu$# with plpy.subtransaction():
hannu$# plpy.execute('insert into
test_ex values(2)')
hannu$# plpy.execute('fail!')
hannu$# except plpy.spiexceptions.SyntaxError:
hannu$# pass # silently ignore, avoid doing
this in prod. code
hannu$# plpy.execute('insert into test_ex
values(3)')
hannu$# $$;
DO
hannu=# select * from test_ex;
i
---
1
3
(2 rows)
Atomicity in Python
While the subtransactions manage data changes in the PostgreSQL database, the
variables on Python side's of the fence live their separate lives. Python does not
provide even a single-statement level atomicity, as demonstrated by the following:
>>> a = 1
>>> a[1] = a = 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Search WWH ::




Custom Search