简体   繁体   English

自动提交设置为false时的完整性约束

[英]Integrity constraints when autocommit is set to false

A service I work on uses the JDBC API to update an Oracle database. 我正在使用的服务使用JDBC API更新Oracle数据库。 One of the methods that updates the database has autocommit set to false in order to execute 2 SQL statements. 为了执行2条SQL语句,更新数据库的方法之一已将autocommit设置为false。 But even though autocommit is set to false the first SQL statement gets executed and we receive a integrity constraint violation error. 但是,即使autocommit设置为false,也会执行第一个SQL语句,并且我们会收到违反完整性约束的错误。

java.sql.SQLIntegrityConstraintViolationException: ORA-02292: integrity constraint (V500.XFK3) violated - child record found  

Shouldn't disabling autocommit wait for the commit method to be called? 不应该禁用自动提交等待调用commit方法吗? If not how can both queries be executed? 如果没有,如何执行两个查询?

On tables that have the constraint disabled the above error doesn't show up and the queries are executed. 在禁用了约束的表上,不会显示上述错误,并且将执行查询。

Constraints are evaluate on the operation - not the commit of the operation. 约束是在操作上评估的-而不是操作的提交。 What you need to do is either deal with deferred constraints, or do the operations in the correct order to avoid foreign key violations. 您需要做的是要么处理延迟的约束,要么以正确的顺序进行操作以避免外键冲突。 Insert the parent and then do the child. 插入父母,然后做孩子。

for a brief intro to deferrable constraints and some subtle issues with using them, start here 有关可延期约束的简要介绍以及使用它们的一些细微问题,请从这里开始

I would strongly suggest, however, that the parent-child relationship constraints in the database were probably created for good reason - so disabling the constraints is a bad idea that will lead to bad data. 但是,我强烈建议在数据库中创建父子关系约束是有充分理由的-因此,禁用约束是一个糟糕的主意,它将导致不良数据。 And deferable constraints should be used as seldom as possible. 并且尽可能少使用可延展的约束。 You may think its a pain to have to figure out the correct order of operations to satisfy constraints, but its the right way to go if you are having the UI do direct DML. 您可能认为必须找出正确的操作顺序来满足约束是一件很痛苦的事情,但是如果让UI直接使用DML,这是正确的做法。 To be honest, I'm a far bigger fan of having PL/SQL APIs exposed to the outside world with the table details hidden for a variety of security and maintenance reasons. 老实说,我更喜欢将PL / SQL API暴露给外界,并且出于各种安全和维护原因,隐藏了表详细信息。 The UI should just be saying "store this data entity" and the interface knows how to put it all in the correct tables. UI应该只说“存储此数据实体”,并且界面知道如何将它们全部放入正确的表中。 That way the back end can be redesigned for efficiency without the front end needing to be aware of it. 这样,可以重新设计后端以提高效率,而前端无需意识到这一点。

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

相关问题 应该将数据源的自动提交设置为false吗? - Should autocommit of a datasource be set to false? Liferay Spring set自动提交false - Liferay Spring set autocommit false 当JDBC AutoCommit为False并且未设置显式事务边界时会发生什么 - What happens when JDBC AutoCommit is False and explicit Transaction boundaries aren't set 在JDBC中,当autocommit为false并且没有设置显式保存点时,它是好的样式还是浪费回滚? - In JDBC, when autocommit is false and no explicit savepoints have been set, is it good style or a waste to rollback? 如何在Websphere 8.5.5数据库连接中将autoCommit设置为false - How to set autoCommit to false in Websphere 8.5.5 database connection 如何在Oracle数据源连接中将autocommit值设置为false? - How to set autocommit value to false in Oracle Data Source connection? 无法在Oracle数据源连接中将自动提交值设置为false - Not able to set autocommit value to false in Oracle Data Source connection 跨多个DAO spring mvc设置AutoCommit False - Set AutoCommit False across multiple DAO spring mvc 自动提交设置为false时,select是否返回最后插入的行? - Does select return last inserted row while autocommit set false? 如何在Apache骆驼SQL批量插入中设置自动提交错误? - How to set autocommit false in apache camel sql batch insertion?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM