简体   繁体   English

Oracle中的事务

[英]Transactions in Oracle

I have few stored procedures in Oracle database as third-party API, and I want to chain these api-calls in one transaction. 我在Oracle数据库中没有作为第三方API的存储过程,我想在一个事务中链接这些api调用。 Can I do it? 我可以做吗? As I understand from that answer I can use savepoint. 根据我的回答,我可以使用保存点。 But does it works if these API already have commit statements? 但是,如果这些API已经具有commit语句,是否可行? Will Oracle roll back nested commits or not? Oracle是否会回滚嵌套的提交?

Also, how Oracle works with savepoint and concurrency? 另外,Oracle如何使用保存点和并发性? Ie if we have diagram: 即如果我们有图:

connection#1 (my api call)              connection#2 (3rd party api call)
savepoint sp1;
                                        savepoint sp2;
update t1 where id=1 set val=1;         update t1 where id=2 set val=2;
                                        commit; --done
call bad_stored_proc();
rollback to sp1;

What will happens here? 这里会发生什么? Will rollback affect only row (id=1,val=1), or both rows? 回滚会只影响(id = 1,val = 1)行,还是影响两行?

Oracle will not rollback nested commits. Oracle不会回滚嵌套的提交。 They're committed. 他们承诺。

The best you can do (and it's not really that great) would be to wrap the 3rd party API in a procedure and mark that procedure with the PRAGMA AUTONOMOUS_TRANSACTION . 您能做的最好的事情(并不是那么好)是将第三方API包装在一个过程中,并使用PRAGMA AUTONOMOUS_TRANSACTION标记该过程。 That would limit the 3rd party API to committing only its own work. 那将限制第三方API仅提交自己的工作。 That means, if you rolled back, the 3rd party transaction work would be committed but any other work you did wouldn't be. 这意味着,如果您回滚,则将执行第三方交易工作,但您未进行任何其他工作。

That is generally a really bad thing, because it will leave your data logically corrupted in the vast majority of design situations. 这通常是一件很糟糕的事情,因为在绝大多数设计情况下,这会使您的数据在逻辑上受到破坏。 I mention it in the unlikely chance that are facing a design situation where you can get away with it. 我提到它的可能性很小,因为它面临着无法摆脱的设计环境。

If the third party procedures call commit within the procedure, there is no way to run all of them as one transaction, without modifying the procedures themselves. 如果第三方过程在过程中调用commit,则无法在不修改过程本身的情况下将所有过程作为一个事务运行。

Oracle does not support nested commits or nested transactions. Oracle不支持嵌套的提交或嵌套的事务。 Commit means just that - you are committing to writing the changes to the database and losing the option to rollback the changes. 提交的意思就是-您承诺将更改写入数据库,并且丢失了回滚更改的选项。

You can use a session parameter to check whether the third party procedures contain any commit statements. 您可以使用会话参数来检查第三方过程是否包含任何commit语句。

alter session disable commit in procedure; 

With this set, any procedures that attempt to commit will throw an error message.* 使用此设置,任何尝试提交的过程都将引发错误消息。*

  • With thanks to Matthew McPeak for correcting my misconception of what this parameter does. 感谢Matthew McPeak纠正了我对该参数的误解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM