我正在编写一个存储服务调用的程序。我的桌子:
messages(id, content)
invocations(correlation_id, request_id, response_id).
程序接收更多线程的数据并在事务中执行:
insert content into messages
set request or response id in invocations
if update returned 0 then insert invocations
有时,由于主要密钥违规,交易会被回滚。
tr1 insert request into messages
tr2 insert respone into messages
tr1 try to update invocations but return 0
tr2 try to update invocations but return 0
tr1 insert into invocations
tr2 insert into invocations but this will fail
tr1 commit
tr2 rollback becouse of PK violation
我尝试将隔离级别更改为可序列化,并尝试使用merge而不是update和isnert,但两者都没有解决问题。
如何以正确的方式存储和更新调用?