ORA-01940 如何drop user

使用drop user bpmuser cascade时遇到以下问题

18:03:13  [DROP - 0 row(s), 0.000 secs]  [Error Code: 1940, SQL State: 42000]  ORA-01940: cannot drop a user that is currently connected
... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec  [0 successful, 0 warnings, 1 errors]

正常情况下一定是有别的程序远程连接数据库到数据库, 并且tibcohost没有停掉, 所以oracle数据库不会允许删除正在被使用的数据库.

下面谈谈异常情况, 如果想强行删掉数据库怎么办.

查看session中是否存在BPMUSER, 注意这里区分大小写, 小写查不出来!!!
select * from v$session where username='BPMUSER'
select sid,serial# from v$session where username='BPMUSER'

方法一. 如果在远程客户端, 可以用alter system kill session [sid],[serial#];

比如,
alter system kill session '26,4323';

 
被kill掉的session,状态会被标记为killed,Oracle会在该用户下一次touch时清除该进程. 我们发现当一个session被kill掉以后,该session的paddr被修改,如果有多个session被kill,那么多个session的paddr都被更改为相同的进程地址:

SQL> select saddr,sid,serial#,paddr,username,status from v$session where username is not null;

SADDR           SID    SERIAL# PADDR    USERNAME                       STATUS
-------- ---------- ---------- -------- ------------------------------ --------
542E0E6C         11        314 542B70E8 EYGLE                          INACTIVE
542E5044         18        662 542B6D38 SYS                            ACTIVE


SQL> alter system kill session '11,314';

System altered.

SQL> select saddr,sid,serial#,paddr,username,status from v$session where username is not null;

SADDR           SID    SERIAL# PADDR    USERNAME                       STATUS
-------- ---------- ---------- -------- ------------------------------ --------
542E0E6C         11        314 542D6BD4 EYGLE                          KILLED
542E5044         18        662 542B6D38 SYS                            ACTIVE

SQL> select saddr,sid,serial#,paddr,username,status from v$session where username is not null;

SADDR           SID    SERIAL# PADDR    USERNAME                       STATUS
-------- ---------- ---------- -------- ------------------------------ --------
542E0E6C         11        314 542D6BD4 EYGLE                          KILLED
542E2AA4         14        397 542B7498 EQSP                           INACTIVE
542E5044         18        662 542B6D38 SYS                            ACTIVE

SQL> alter system kill session '14,397';

System altered.

SQL> select saddr,sid,serial#,paddr,username,status from v$session where username is not null;

SADDR           SID    SERIAL# PADDR    USERNAME                       STATUS
-------- ---------- ---------- -------- ------------------------------ --------
542E0E6C         11        314 542D6BD4 EYGLE                          KILLED
542E2AA4         14        397 542D6BD4 EQSP                           KILLED
542E5044         18        662 542B6D38 SYS                            ACTIVE



方法二.但这种方式很多时候是无法释放资源的, 从而无法drop user, 如果这样, 我们就需要查询spid,在操作系统级来kill这些进程. 通过v$session和v$process的相同地址关联来获得spid. 如果像上面的例子, 在kill后v$session.paddr已经改变,我们无法通过v$session和v$process关联来获得spid, 参考方法三.


select spid, osuser, s.program, s.username from v$process p, v$session s where p.addr=s.paddr and s.username='BPMUSER'



获得spid后, 需要登录的数据库所在的操作系统上, 在命令行中使用orakill orcl [spid]

在命令行中直接输入orakill会出现帮助说明:

C:\Documents and Settings\Administrator>orakill

Usage:  orakill sid thread

  where sid    = the Oracle instance to target
        thread = the thread id of the thread to kill

The thread id should be retrieved from the spid column of a query such as:

        select spid, osuser, s.program from
        v$process p, v$session s where p.addr=s.paddr


C:\Documents and Settings\Administrator>orakill orcl 2192

Kill of thread id 2192 in instance orcl successfully signalled.

最后使用drop user



方法三. 如果无法通过v$session和v$process关联来获得spid

那还可以怎么办呢?

我们来看一下下面的查询:

  SQL> SELECT s.username,s.status,
  2  x.ADDR,x.KSLLAPSC,x.KSLLAPSN,x.KSLLASPO,x.KSLLID1R,x.KSLLRTYP,
  3  decode(bitand (x.ksuprflg,2),0,null,1)
  4  FROM x$ksupr x,v$session s
  5  WHERE s.paddr(+)=x.addr
  6  and bitand(ksspaflg,1)!=0;


USERNAME                       STATUS   ADDR       KSLLAPSC   KSLLAPSN KSLLASPO       KSLLID1R KS D
------------------------------ -------- -------- ---------- ---------- ------------ ---------- -- -
                                        542B44A8          0          0                       0
                               ACTIVE   542B4858          1         14 24069                 0    1
                               ACTIVE   542B4C08         26         16 15901                 0    1
                               ACTIVE   542B4FB8          7         46 24083                 0    1
                               ACTIVE   542B5368         12         15 24081                 0    1
                               ACTIVE   542B5718         15         46 24083                 0    1
                               ACTIVE   542B5AC8         79          4 15923                 0    1
                               ACTIVE   542B5E78         50         16 24085                 0    1
                               ACTIVE   542B6228        754         15 24081                 0    1
                               ACTIVE   542B65D8          1         14 24069                 0    1
                               ACTIVE   542B6988          2         30 14571                 0    1

USERNAME                       STATUS   ADDR       KSLLAPSC   KSLLAPSN KSLLASPO       KSLLID1R KS D
------------------------------ -------- -------- ---------- ---------- ------------ ---------- -- -
SYS                            ACTIVE   542B6D38          2          8 24071                 0
                                        542B70E8          1         15 24081               195 EV
                                        542B7498          1         15 24081               195 EV
SYS                            INACTIVE 542B7848          0          0                       0
SYS                            INACTIVE 542B7BF8          1         15 24081               195 EV

16 rows selected.

         
我们注意,红字标出的部分就是被Kill掉的进程的进程地址.

简化一点,其实就是如下概念:

SQL> select p.addr from v$process p where pid <> 1
  2  minus
  3  select s.paddr from v$session s;
ADDR
--------
542B70E8
542B7498





Ok,现在我们获得了进程地址,就可以在v$process中找到spid,然后可以使用Kill或者orakill在系统级来杀掉这些进程.

实际上,我猜测:

当在Oracle中kill session以后, Oracle只是简单的把相关session的paddr 指向同一个虚拟地址.

此时v$process和v$session失去关联,进程就此中断.

然后Oracle就等待PMON去清除这些Session.所以通常等待一个被标记为Killed的Session退出需要花费很长的时间.

如果此时被Kill的process,重新尝试执行任务,那么马上会收到进程中断的提示,process退出,此时Oracle会立即启动PMON
来清除该session.这被作为一次异常中断处理.

猜你喜欢

转载自mxy0521.iteye.com/blog/1837540