简体   繁体   English

由于挂起事务 FK 插入,Mysql 事务获得“超出锁定等待超时”

[英]Mysql transaction getting 'Lock wait timeout exceeded' because of pending transaction FK insert

In my java application, I'm running a Transaction I where an insert is done on table A .在我的 Java 应用程序中,我正在运行一个事务 I ,其中在表 A上完成了插入操作。

Transaction I also calls a method which uses a separate connection to have a separate Transaction II in order to create a record in table B .事务 I还调用一个方法,该方法使用单独的连接来拥有单独的事务 II ,以便在表 B 中创建记录。 (This table is used to keep track of internal ID generation and it is important, that whatever happens to Transaction I , that Transaction II is commited). (此表用于跟踪内部 ID 生成,重要的是,无论事务 I发生什么,事务 II 都会被提交)。

Now, because of things, table B actually had a reference to the PK of table A (which is actually the problem).现在,因为事情,表B实际上引用了表A的PK(这实际上是问题所在)。

However since the record in A is just being created in the "outer" Transaction I , it is not yet commited, and the value to insert as reference in B (pointing to A ) thus does not exist in Transaction II .然而,由于A 中的记录刚刚在“外部”事务 I 中创建,它尚未提交,因此在事务 II中不存在作为B 中引用插入的值(指向A )。 (Or in other words, I would suspect that this value is not visible for Transaction II since it isn't commited) (或者换句话说,我怀疑这个值对于事务 II 不可见,因为它没有提交)

Now in this situation I'd expect to get a FK constraint violation error, but I am getting a现在在这种情况下,我希望得到 FK 约束违反错误,但我得到了

com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException:超出锁定等待超时; try restarting transaction .尝试重新启动事务

Testing the same scenario on a postgresql db raises the expected:在 postgresql 数据库上测试相同的场景会产生预期的结果:

org.postgresql.util.PSQLException: ERROR: insert or update on table " B " violates foreign key constraint org.postgresql.util.PSQLException:错误:在表“ B ”上插入或更新违反外键约束

I suspect that the mysql database knows about the insert of the first transaction and actually tries to wait in the second transaction for the first to finish to actually be able to insert the record of the second transaction?我怀疑 mysql 数据库知道第一个事务的插入,并且实际上尝试在第二个事务中等待第一个完成以实际能够插入第二个事务的记录?

Of course that can't ever happen since the first transaction also waits for the second to complete.当然,这不可能发生,因为第一个事务也在等待第二个事务完成。 So the "inner" transaction aborts with the timeout.因此,“内部”事务因超时而中止。

Is there a reason the two db systems behave differently, or do I miss something?是否有两个数据库系统行为不同的原因,或者我错过了什么?

Update: I think this has to do with the different default isolation levels of mysql (REPEATABLE_READ) and postgresql (READ_COMMITED)更新:我认为这与 mysql (REPEATABLE_READ) 和 postgresql (READ_COMMITED) 的不同默认隔离级别有关

You cannot have two separate connections and expect it to work.您不能有两个单独的连接并期望它工作。 You should write a code where you can pass the current connection object to the method so that the connection is re used in that method which will not complain about fk.您应该编写一个代码,您可以在其中将当前连接对象传递给该方法,以便在该方法中重新使用该连接,而不会抱怨 fk。

The solution for my specific problem was to simply remove the FK constraint between Table B and Table A since it is not needed anyway.我的具体问题的解决方案是简单地删除表 B表 A之间的 FK 约束,因为无论如何都不需要它。

The different behaviour between MySQL and PostgreSQL definitely derives from the default setting regarding the isolation levels. MySQL 和 PostgreSQL 之间的不同行为肯定源自有关隔离级别的默认设置。

暂无
暂无

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

相关问题 超过了锁定等待超时; 尝试使用spring和Mysql重新启动事务Java - Lock wait timeout exceeded; try restarting transaction Java with spring and Mysql MySQL错误:超过了锁定等待超时; 尝试重新启动事务查询 - MySQL Error: Lock wait timeout exceeded; try restarting transaction Query java.sql.SQLException:超出了锁定等待超时; 尝试重新启动事务异常在MYSQL中发生 - java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction exception occur in MYSQL 超过了锁定等待超时; 尝试在spring_Hibernate_Mysql中重新启动事务 - Lock wait timeout exceeded; try restarting transaction in spring_Hibernate_Mysql 如何修复 Mysql 表之前工作时的“超出锁定等待超时;尝试重新启动事务”? - How to fix "Lock wait timeout exceeded; try restarting transaction" for Mysql table when it was working before? 超过锁定等待超时; 尝试使用JDBC重新启动事务 - Lock wait timeout exceeded; try restarting transaction using JDBC 由于“超过了锁定等待超时,导致事务失败; 尝试重新开始交易” - Transactions fails due to “Lock wait timeout exceeded; try restarting transaction” 获取WARN:SQL错误:1205,SQLState:41000错误:超出锁定等待超时; 尝试重新启动事务。 使用休眠保存记录 - Getting WARN: SQL Error: 1205, SQLState: 41000 ERROR: Lock wait timeout exceeded; try restarting transaction. Saving a record in using hibernate 发出java.lang.Exception:超出了锁定等待超时; 尝试重新启动事务 - Issue java.lang.Exception: Lock wait timeout exceeded; try restarting transaction java jdbc事务中超过了锁定等待超时 - lock wait time out exceeded in java jdbc transaction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM