oracle下session的查詢與刪除


oracle下session的查詢與刪除

1、查詢當前session

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


USERNAME                              SID    SERIAL#
------------------------------ ---------- ----------
SYS                                   144          4
HYL                                   146         48
SCOTT                                 147         64
HR                                    159         15
--SERIAL#:SID有可能會反復,當兩個session的SID反復時,SERIAL#用來差別session

2、刪除當前session

SQL> alter system kill session '146,48';
System altered.


hyl的session下運行操作例如以下:


SQL> show user
USER is "HYL"
SQL> select * from test1;
select * from test1
*
ERROR at line 1:
ORA-00028: your session has been killed

3、刪除當前session的用戶

思路:先關閉session,然后再刪除用戶


演示:在不關閉session的情況下刪除用戶現象:

SQL> select username,account_status from dba_users;
--查看當前有哪些用戶
USERNAME                       ACCOUNT_STATUS
------------------------------ --------------------------------
CSMIG                          OPEN
MGMT_VIEW                      OPEN
SYS                            OPEN
SYSTEM                         OPEN
HYL                            OPEN
OE                             EXPIRED & LOCKED
6 rows selected.


SQL> select username,sid,serial# from v$session where username is not null;
--通過v$session視圖,查看會話的sid、serial#
USERNAME                              SID    SERIAL#
------------------------------ ---------- ----------
SYS                                   144          4
HYL                                   146         54
SCOTT                                 147         64
HR                                    159         15


SQL> drop user hyl;       --刪除,報錯了。表明會話中存在的用戶是不能被刪除的,須要先將其關閉
drop user hyl
*
ERROR at line 1:
ORA-01940: cannot drop a user that is currently connected

SQL> alter system kill session '146,54';
System altered.


--hyl的session下查看數據。信息顯示該session已經被kill掉,表明刪除session成功
SQL> select * from test1;
select * from test1
*
ERROR at line 1:
ORA-00028: your session has been killed


--以下完畢刪除用戶
SQL> drop user hyl;   --刪除用戶報錯了,此時hyl用戶下有對象存在
drop user hyl
*
ERROR at line 1:
ORA-01922: CASCADE must be specified to drop 'HYL'


SQL> drop user hyl cascade; --將用戶及其所有對象所有刪除
User dropped.


--嘗試hyl連接。報錯。無法使用hyl登陸session
SQL> conn hyl/oracle
ERROR:
ORA-01017: invalid username/password; logon denied


Warning: You are no longer connected to ORACLE.


--查看dba_users數據字典。表明hyl用戶已經被刪除
SQL> select username,account_status from dba_users;


USERNAME                       ACCOUNT_STATUS
------------------------------ --------------------------------
CSMIG                          OPEN
MGMT_VIEW                      OPEN
SYS                            OPEN
SYSTEM                         OPEN
OE                             EXPIRED & LOCKED
5 rows selected.


小結:


查詢當前會話:select username,sid,serial# from v$session where username is not null;
刪除當前會話:alter system kill session 'sid,serial#';
刪除當前會話的用戶,先kill session,再drop user(若用戶下有對象,使用cascade命令)



——————————————————————————————————————————————————————————————————————

補充:username為空


--補充:查詢會話時不加入“username is not null”條件時,會出現username為空的session信息
SQL> select username,sid,serial# from v$session;


USERNAME                              SID    SERIAL#
------------------------------ ---------- ----------
SYS                                   144          4
SCOTT                                 147         64
                                      149          2
                                      153        346
                                      154          1
                                      158          7
HR                                    159         15
                                      160          1
                                      161          1
                                      162          1
                                      163          1
                                      164          1
                                      165          1
                                      166          1
                                      167          1
                                      168          1
                                      169          1
                                      170          1
18 rows selected.


說明:
oracle對於username的原則:
Oracle內部進程的user#為0時,則username為空;
Oracle內部進程的username為空時,則user#為0。
username、user#兩個字段都是屬於表示用戶的字段。




由上得出例如以下:
前台進程是屬於某個數據庫用戶的,所曾經台進程的user#不為0,username不為空;
而后台進程不屬於不論什么數據庫用戶。所以后台進程的user#為0,username為空。


小結:
     

    用戶僅僅能使用server進程(前台進程)。不能使用后台進程,而前台進程和后台進程session能夠相應,所以在v$session中username為空(即user#為0)。


--補充完成

——————————————————————————————————————————————————————————————————————



***********************************************聲明************************************************ 

原創作品,出自 “深藍的blog” 博客。歡迎轉載,轉載時請務必注明出處(http://blog.csdn.net/huangyanlong)。

表述有錯誤之處。請您留言,不勝感激。

提醒:點擊文件夾,更有助於您的查看。

*****************************************************************************************************













免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM