I'm using Spring with PostgreSQL and I try to do a sort of UPSERT by using a code like this:
jt.update("delete from A where id = 1")
jt.update("insert into A (id, value) values (1, 100)")
wrapped inside a transaction (using @Transactional
).
The problem is that, when there are many concurrent requests, this code fails with 'duplicate key' errors, meaning the transaction is not isolated, or...
Am I missing something about how transactions work? Should I use a different mechanism here (ex. thread synchronization)?