问:如何用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命令之后,任何没有提交的事务都会回滚。(注意:如果存在很长的没有提交的事务,这种关闭数据库的方法也许不能快速完成,尽管它的名字是“立即”。)