是否可以在Oracle中跳过回滚表?

huangapple go评论77阅读模式
英文:

Is it possible to skip an table from Rollback in Oracle?

问题

我有一个情景,需要验证数据并将有效数据插入一些表中,然后在Oracle包中更新审核表中的状态。当存在验证失败时,希望回滚除审核表信息以外的所有其他表数据。是否可以跳过一个表的回滚。在每个表添加状态到审核表之后。
感谢提前的帮助。

英文:

I am having a scenario where validating the data and inserting the valid data into some of the tables and updating the status into audit table in Oracle package. when there is a validation failure then want to revert all other table data except audit table information. Is it possible to skip rollback for one table. After each table adding status to audit table.
Thanks in advance for help.

答案1

得分: 2

通常,这是由一个单独的自主事务过程完成的,该过程修改审核表并提交所做的更改。

由于这是一个自主事务,因此commit不会影响主事务,所以一旦您执行rollback,您在所有其他表中所做的更改都将被回滚,但存储在审核表中的信息将保持不变

英文:

Usually, it is done by a separate autonomous transaction procedure which modifies the audit table and commits changes being made.

As it is an autonomous transaction, that commit won't affect the main transaction so - once you rollback - changes you made in all other tables will be rolled back, but info stored in audit table will remain as is.

答案2

得分: 1

不可以。当您执行ROLLBACK操作时,整个事务都会被回滚,您无法选择哪些部分要回滚,哪些部分不回滚。

如果存在验证失败,并且想要撤销除审计表信息之外的所有表数据,您需要:

  1. 使用多个事务,在一个事务中执行(经过验证的)数据修改,在另一个事务中执行审计数据修改,然后回滚第一个事务并提交第二个事务。通常使用AUTONOMOUS TRANSACTION来实现这一点。
  2. 使用单个事务,首先执行审计数据修改,创建一个SAVEPOINT,然后执行(经过验证的)数据修改,如果它们失败,则使用ROLLBACK TO SAVEPOINT来撤销该保存点之后的所有更改。然后您可以选择COMMIT或在SAVEPOINT之前的上一次COMMIT之前回滚。
  3. 使用单个事务,首先执行审计数据修改并提交它们,然后执行(经过验证的)数据修改,如果它们失败,则回滚到最后一个COMMIT之前,以撤销所有更改。
英文:

> Is it possible to skip rollback for one table.

No. When you ROLLBACK a transaction, the entire transaction is rolled back and you cannot pick and choose which parts of it are rolled-back or not.

> When there is a validation failure then want to revert all other table data except audit table information.

You need to either:

  1. Use multiple transactions and perform the (validated) data modifications in one transaction and the audit data modifications in a separate transaction and then ROLLBACK the first transaction and COMMIT the second transaction. This is normally done using an AUTONOMOUS TRANSACTION.
  2. Use a single transaction and perform the audit data modifications first, create a SAVEPOINT and only then perform the (validated) data modifications and if they fail then ROLLBACK TO SAVEPOINT to undo all the changes after that save point. You can then choose to COMMIT or could still ROLLBACK to the previous COMMIT before the SAVEPOINT.
  3. Use a single transaction and perform the audit data modifications first and COMMIT them and only then perform the (validated) data modifications and if they fail then ROLLBACK to undo all the changes to that last COMMIT.

huangapple
  • 本文由 发表于 2023年7月6日 14:17:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定