Oracle 数据库关闭 之 使用IMMEDIATE选项关闭数据库

本文通过四个场景探讨了在执行shutdown immediate命令时,数据库对新用户连接、DDL语句、DML语句及未提交事务的处理。执行该命令后,将阻止新用户连接,DDL语句无法执行,DML语句在事务开始后会被回滚,未提交的事务也会被自动回滚。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问:如何用NORMAL选项关闭数据库?

答:使用shutdown immediate命令!

场景一:在执行shutdown immediate命令的时候,是否还可以有新的用户连接到数据库?

会话一:

SQL> startup
ORACLE instance started.

Total System Global Area  236000356 bytes
Fixed Size                   451684 bytes
Variable Size             201326592 bytes
Database Buffers           33554432 bytes
Redo Buffers                 667648 bytes
Database mounted.
Database opened.
再运行以下语句:
SQL> shutdown immediate
同时,在会话二,新连接一个用户到数据库,执行以下语句:
SQL> conn scott/tiger
此时,会产生如下结果:

会话一:

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

会话二:

SQL> conn scott/tiger
ERROR:
ORA-01089: immediate shutdown in progress - no operations are permitted

结论:在执行shutdown immediate命令之后,不允许有新的用户连接到数据库。


场景二:在执行shutdown immediate命令的时候,假设用户scott执行DDL语句,请问是否会执行成功?

会话一:

SQL> startup
ORACLE instance started.

Total System Global Area  236000356 bytes
Fixed Size                   451684 bytes
Variable Size             201326592 bytes
Database Buffers           33554432 bytes
Redo Buffers                 667648 bytes
Database mounted.
Database opened.
会话二:
SQL> conn scott/tiger
Connected

会话一:

SQL> shutdown immediate

同时,在会话二,执行以下DDL语句:

SQL> create table t (x number);
此时,会产生如下结果:

会话一:

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

会话二:

SQL> conn scott/tiger
ERROR:
ORA-01089: immediate shutdown in progress - no operations are permitted
结论:在执行shutdown immediate命令之后,所有连接到数据库的用户执行的DDL语句都无效。


场景三:在执行shutdown immediate命令的时候,假设用户scott执行DML语句,启动新的事务,请问是否会执行成功?

会话一:

SQL> startup
ORACLE instance started.

Total System Global Area  236000356 bytes
Fixed Size                   451684 bytes
Variable Size             201326592 bytes
Database Buffers           33554432 bytes
Redo Buffers                 667648 bytes
Database mounted.
Database opened.

会话二:

SQL> conn scott/tiger
Connected
SQL> create table t(x number);

Table created.

会话一:

SQL> shutdown immediate
同时,在会话二,执行以下DML语句:

SQL> insert into t values(1);
此时,会产生如下结果:

会话一:

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

会话二:

SQL> insert into t values(1);
insert into t values(1)
*
ERROR at line 1:
ORA-01089: immediate shutdown in progress - no operations are permitted
结论:在执行shutdown immediate命令之后,不允许启动新的事务。


场景四:在执行shutdown immediate命令的时候,假设用户scott执行Insert语句,事务没有提交,请问是否会自动提交还是会回滚?

会话一:

SQL> startup
ORACLE instance started.

Total System Global Area  236000356 bytes
Fixed Size                   451684 bytes
Variable Size             201326592 bytes
Database Buffers           33554432 bytes
Redo Buffers                 667648 bytes
Database mounted.
Database opened.
会话二:

SQL> conn scott/tiger
Connected.
SQL> desc t;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 X                                                  NUMBER
SQL> truncate table t;

Table truncated.

SQL> insert into t values (1);

1 row created.

会话一:

SQL> shutdown immediate
此时,会产生如下结果:

会话一:

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
重启数据库,查看表T里面是否有数据?

SQL> startup
ORACLE instance started.

Total System Global Area  236000356 bytes
Fixed Size                   451684 bytes
Variable Size             201326592 bytes
Database Buffers           33554432 bytes
Redo Buffers                 667648 bytes
Database mounted.
Database opened.
SQL> select * from t;

no rows selected
结论:在执行shutdown immediate命令之后,任何没有提交的事务都会回滚。(注意:如果存在很长的没有提交的事务,这种关闭数据库的方法也许不能快速完成,尽管它的名字是“立即”。)







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值