gpt4 book ai didi

oracle - 如何确定 JPA 中是否发生了 Oracle 可延迟约束违规?

转载 作者:行者123 更新时间:2023-12-02 04:04:07 24 4
gpt4 key购买 nike

如果事务违反延迟约束,Oracle 11g 会在提交时静默回滚事务。实际得到错误的一种方法是在调用 commit 之前将约束设置为立即。使用 Oracle SQL Developer 可以正常工作,但在 JPA (EclipseLink) native 查询中执行此操作会导致应用程序锁定。如果违反了延迟约束,是否有其他方法可以通知?或者我没有正确使用 JPA?

表定义:

create table foo
(
foo_id integer not null,
order_id integer not null,
constraint foo_pk primary key (foo_id),
constraint foo_ak unique (order_id) deferrable
);

SQL 生成违规并查看控制台上显示的错误:
set constraint foo_ak deferred;
insert into foo values (1, 1);
insert into foo values (2, 1);
set constraint foo_ak immediate; -- Will rollback and display error

JPA 尝试重新排序无状态 session Bean 中的 foo 实体集合:
public void edit(List<Foo> foos) {
Query deferred = em.createNativeQuery("set constraint foo_ak deferred");
Query immediate = em.createNativeQuery("set constraint foo_ak immediate");

deferred.executeUpdate(); // This works

for(Foo f: foos) {
em.merge(f); // This works too
}

immediate.executeUpdate(); // Hang with no output!
// Note: if I comment out above line the transaction may
// be silently rolled back on deferred constraint violation
}

最佳答案

问题可能出在语句缓存和应用服务器事务策略中。

尝试:

  • 强制实体管理器通过在执行 {} 后调用 em.flush() 将数据发送到数据库

  • 和/或
  • 在您的方法中将@TransactionManagement 属性更改为bean 管理和管理事务
  • 关于oracle - 如何确定 JPA 中是否发生了 Oracle 可延迟约束违规?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8592622/

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