简体   繁体   English

Oracle-依靠ROLLBACK进行数据验证

[英]Oracle- relying on ROLLBACK for data validation

Is relying on the oracle ROLLBACK command good practice for importing data, validating the data and THEN performing a ROLLBACK ? 依靠oracle ROLLBACK命令导入数据,验证数据然后执行ROLLBACK良好实践是吗?

I've had a data import program built for our ERP, and looking at the code, they insert the data into the real tables, validate, and if it fails validation, they perform a ROLLBACK. 我已经为ERP构建了一个数据导入程序,然后查看代码,他们将数据插入到真实表中,进行验证,如果验证失败,则执行ROLLBACK。 I've always validated data before inserting, but just curious if this is an accepted method to rely on? 我一直在插入数据之前先对数据进行验证,但是只是想知道这是否是一种可以依靠的方法?

There are a few things to remember here- 这里有几件事要记住-

  • Constraints enable us preserve data integrity. 约束使我们能够保留数据完整性。 This means that constraints allow us to enforce business rules (or at least the most basic of those) at the database level itself. 这意味着约束使我们能够在数据库级别本身上执行业务规则(或至少是最基本的业务规则)。

  • A commit or a rollback is a method of preserving or undoing the changes made in a transaction. commitrollback是一种保留或撤消在事务中所做的更改的方法。 If you issue a commit after a series of successfully run DML statements, the changes are preserved. 如果在一系列成功运行的DML语句之后发出commit ,则更改将保留。 The rollback statement would undo the changes. rollback语句将撤消更改。

  • If, in a series of DML statements, if one of those fails, the effects of that particular statement are rolled back. 如果在一系列DML语句中,如果其中一个失败,则将回滚该特定语句的效果。 Eg, if an UPDATE statement updates 10 rows and one of those violates a vital constraint, any of the 10 rows are not updated. 例如,如果UPDATE语句更新10行,而其中之一违反了重要约束,则这10行中的任何一行都不会更新。 Yet, the effects of its preceding statements are not implicitly rolled back. 但是,其先前语句的效果并未隐式回滚。

  • In order to preserve data integrity and keep the data as per the business requirements, you must issue a manual ROLLBACK statement if any of the DMLs fail. 为了保持数据完整性并按照业务要求保留数据,如果任何DML失败,则必须发出手动ROLLBACK语句。

  • What you are seeing in your program is the same practice. 您在程序中看到的是相同的做法。 It doesn't issue a ROLLBACK after a successful transaction, but only after a failed DML, if you look at the code closely. 如果您仔细查看代码,它不会在成功事务之后发出ROLLBACK ,而只会在失败的DML之后发出ROLLBACK This is indeed a good practice to roll back on failure and commit only if everything goes right. 确实,这是一种很好的做法,可以仅在一切正常的情况下回退失败并提交。

  • Front end checks on data are indeed an essential part of any application. 数据的前端检查确实是任何应用程序的重要组成部分。 This ensures that the data being entered conforms to the business roles. 这样可以确保输入的数据符合业务角色。 Even in this case, constraints must be applied to perform checks at the database level. 即使在这种情况下,也必须施加约束以在数据库级别执行检查。 This is particularly helpful when some rookie makes changes to the front end and tries to enter invalid data. 当某些菜鸟对前端进行更改并尝试输入无效数据时,这特别有用。 This is also helpful when someone is bypassing the application and entering data manually. 当有人绕过应用程序并手动输入数据时,这也很有用。 Hence putting constraints on the database level is always necessary. 因此,在数据库级别上施加约束总是必要的。

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

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