gpt4 book ai didi

java - 有两个sql插入的事务

转载 作者:可可西里 更新时间:2023-11-01 08:23:05 27 4
gpt4 key购买 nike

我有两个 sql 插入操作(比如表 A 和 B 中的示例),它们在一个事务中,因为我希望数据库保持一致,也就是说,A 中的元组必须在 B 中有引用。

在第二个插入中,我需要来自第一个的 ID,但在提交事务之前我没有得到这个 ID。
所以我卡住了。我不想从事务中取出第一个插入,可能会发生第一个插入正常但第二个不正常的情况,从而使我在数据库中处于不一致的状态。

这种情况下的最佳做法是什么?

编辑:这是代码:

TransactionStatus txStatus = transactionManager.getTransaction(txDefinition);
try{
Integer aId = insertIntoA();
insertIntoB(aId);
}catch(){
transactionManager.rollback(txStatus);
throw new CustomException();
}
transactionManager.commit(txStatus);

我想指出的是,在我提交事务之前我不会得到aId,因此将 null 插入 B。

最佳答案

在 MySQL 上,在 insertIntoA 中你应该能够做:

SELECT LAST_INSERT_ID()

...在您用于插入的同一连接上,假设它是您要查找的 identity 列值。

编辑:如果您这样做但没有效果(根据您的评论),我会查看中间层以了解发生了什么。 MySQL 没问题:

mysql> create table A (id int(11) not null auto_increment, descr varchar(64), primary key (id));
Query OK, 0 rows affected (0.13 sec)

mysql> create table B (fk int(11) not null, descr varchar(64));
Query OK, 0 rows affected (0.06 sec)

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into A (descr) values ('Testing 1 2 3');
Query OK, 1 row affected (0.00 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.03 sec)

mysql> insert into B (fk, descr) values (1, 'Test complete');
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.03 sec)

mysql> select * from A;
+----+---------------+
| id | descr |
+----+---------------+
| 1 | Testing 1 2 3 |
+----+---------------+
1 row in set (0.02 sec)

mysql> select * from B;
+----+---------------+
| fk | descr |
+----+---------------+
| 1 | Test complete |
+----+---------------+
1 row in set (0.00 sec)

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into A (descr) values ('Second test');
Query OK, 1 row affected (0.01 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)

mysql> insert into B (fk, descr) values (2, 'Second test complete');
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.08 sec)

mysql> select * from A;
+----+---------------+
| id | descr |
+----+---------------+
| 1 | Testing 1 2 3 |
| 2 | Second test |
+----+---------------+
2 rows in set (0.02 sec)

mysql> select * from B;
+----+----------------------+
| fk | descr |
+----+----------------------+
| 1 | Test complete |
| 2 | Second test complete |
+----+----------------------+
2 rows in set (0.00 sec)

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into A (descr) values ('We''ll roll this one back.');
Query OK, 1 row affected (0.00 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 3 |
+------------------+
1 row in set (0.00 sec)

mysql> insert into B (fk, descr) values (3, 'Won''t see this one.');
Query OK, 1 row affected (0.00 sec)

mysql> select * from B;
+----+----------------------+
| fk | descr |
+----+----------------------+
| 1 | Test complete |
| 2 | Second test complete |
| 3 | Won't see this one. |
+----+----------------------+
3 rows in set (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.03 sec)

mysql> select * from A;
+----+---------------+
| id | descr |
+----+---------------+
| 1 | Testing 1 2 3 |
| 2 | Second test |
+----+---------------+
2 rows in set (0.00 sec)

mysql> select * from B;
+----+----------------------+
| fk | descr |
+----+----------------------+
| 1 | Test complete |
| 2 | Second test complete |
+----+----------------------+
2 rows in set (0.00 sec)

关于java - 有两个sql插入的事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1543756/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com