sql - 将列更改为非空时避免 ORA-00054

标签 sql oracle ddl

在频繁使用的表上将现有列更改为“not null”时,我希望避免出现以下或任何其他错误消息:

SQL Error: ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired
00054. 00000 -  "resource busy and acquire with NOWAIT specified or timeout expired"
*Cause:    Interested resource is busy.
*Action:   Retry if necessary or increase timeout.

我的 Oracle 版本是:Oracle Database 12c 企业版版本 12.1.0.2.0 - 64 位生产版。我尝试使用独占锁,如下所示,但仍然收到错误。我通过在其他 session 中执行一些 DML 来模拟一个频繁使用的表。

第 1 节

create table iei1731 (
  col1 varchar2(1 char),
  col2 number(1,0)
);
insert into iei1731 (col1, col2) values ('1', 0);
commit;
update iei1731 set col1 = col1 where col1 = '1';

第二节

lock table iei1731 in exclusive mode;

第 1 节

rollback; -- now session 2 gets the exclusive lock on the table
update iei1731 set col1 = col1 where col1 = '1';

第二节

alter table iei1731 modify col2 not null; -- here I get the ORA-00054

第 1 节(清理)

rollback;
drop table iei1731;

所以我的问题是是否有可能将这个频繁使用的表上的列设置为非空而不出现任何错误消息?

最佳答案

使用DDL_LOCK_TIMEOUT让 session 等待锁定:

--Wait up to 10 minutes to acquire the lock.
alter session set DDL_LOCK_TIMEOUT=600;
alter table iei1731 modify col2 not null;

关于sql - 将列更改为非空时避免 ORA-00054,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45145974/

相关文章:

java - 逆向工程 JDBC Oracle 连接字符串

sql-server - SQL Server 中 ALTER COLUMN 语句的幕后操作

sql - 错误 - 未在预期位置找到关键字

oracle - 提交空交易很昂贵吗?

oracle - Delphi/BDE 应用程序中的问题 - Ora-942 表或 View 不存在

mysql - SQL 查询建表错误

sql - 为什么这个 postgresql 查询这么慢?

sql - Oracle中如何替换字符串?

php - 如何在 PHP 中获取外键引用的列名

c# - 从 IDataReader 读取字段时出现间歇性 System.IndexOutOfRangeException