Java Oracle Transaction

its*_*sme 2 java oracle transactions jdbc

我想将数据插入到不同的表中的数据库中.由于约束,我必须按特定顺序执行此操作.这意味着,首先插入表a,然后b,然后c,....并且没有混合表.但我正在编写一个程序,它可以获取多个csv文件,并将它们导入数据库,但程序无法知道什么是正确的顺序.所以我认为交易是正确的方式,因为我听说,数据一致性必须只存在于交易结束时.但这不起作用

我的代码看起来像这样:

Connection connection = DriverManager.getConnection(url, user, pw);
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
statement.addBatch("INSERT INTO c ....");
statement.addBatch("INSERT INTO a ....");
statement.addBatch("INSERT INTO b ....");
statement.addBatch("INSERT INTO a ....");
// ...
statement.executeBatch();
statement.close();
connection.commit();
Run Code Online (Sandbox Code Playgroud)

但我会得到ORA-02291(完整性约束违规):-(

Mat*_*son 8

您需要使约束可延迟,这样在提交之前不会检查它.有一个关于它的好文章在这里

> drop table c
table C dropped.
> drop table p
table P dropped.
> create table p (id number primary key)
table P created.
> create table c (id number primary key, p_id number)
table C created.
> alter table c add constraint pk_p foreign key (p_id) references p (id) deferrable
table C altered.
> insert into c values ( 1, 1 )
1 rows inserted.
> insert into p values ( 1 )
1 rows inserted.
> commit
commited.
Run Code Online (Sandbox Code Playgroud)