程式、會話、連線之間的差異

beatony發表於2011-07-29
 

程式、會話、連線之間的差異

分類: Oracle 其它特性 20人閱讀 評論(0) 舉報
--========================
-- 程式、會話、連線之間的差異
--========================


    在使用Oracle database的時候,連線與會話是我們經常碰到的詞語之一。咋一看貌似一回事,事實則不然。一個連線上可以建立零個、
一個、甚至多個會話。啊,咋這樣呢?是的,沒錯。這也是我們經常誤解的原因。
    各個會話之間是單獨的,獨立於其他會話,即便是同一個連線的多個會話也是如此。
    
一、幾個術語之間的定義(參照Oracle 9i &10g 程式設計藝術)
    
        連線(connection):連線是從客戶到Oracle 例項的一條物理路徑。連線可以在網路上建立,或者透過IPC 機制建立。通常會在
    客戶程式與一個專用伺服器或一個排程器之間建立連線。
    
        會話(session):會話是例項中存在的一個邏輯實體。這就是你的會話狀態(session state),也就是表示特定會話的一組記憶體
    中的資料結構.提到"資料庫連線"時,大多數人首先想到的就是“會話”。你要在伺服器中的會話上執行SQL、提交事務和執行儲存過程。

二、透過例子演示來檢視之間的關係
    1. 無連線,無會話,無程式的情形           
  1. --&gt沒有建立建立連線時,沒有任何會話伺服器程式                                            
  2. [oracle@odbp ~]$ ps -ef | grep oracleorcl                                               
  3. oracle    5685  5446  0 19:30 pts/1    00:00:00 grep oracleorcl                         
  4.                                                                                         
  5. [oracle@odbp ~]$ sqlplus /nolog                                                         
  6.                                                                                         
  7. SQL*Plus: Release 10.2.0.4.0 - Production on Mon Jun 27 19:30:49 2011                   
  8. Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.                                 
  9.                                                                                         
  10. idle> ho ps -ef | grep oracleorcl    --&gt使用nolog登入是同樣也看不到任何會話伺服器程式    
  11. oracle    5691  5686  0 19:31 pts/0    00:00:00 /bin/bash -c ps -ef | grep oracleorcl   
--&gt沒有建立建立連線時,沒有任何會話伺服器程式 [oracle@odbp ~]$ ps -ef | grep oracleorcl oracle 5685 5446 0 19:30 pts/1 00:00:00 grep oracleorcl [oracle@odbp ~]$ sqlplus /nolog SQL*Plus: Release 10.2.0.4.0 - Production on Mon Jun 27 19:30:49 2011 Copyright (c) 1982, 2007, Oracle. All Rights Reserved. idle> ho ps -ef | grep oracleorcl --&gt使用nolog登入是同樣也看不到任何會話伺服器程式 oracle 5691 5686 0 19:31 pts/0 00:00:00 /bin/bash -c ps -ef | grep oracleorcl
       2. 單個連線,單個會話,單個程式       
  1. --&gt使用scott身份登入,有一個對應的伺服器程式被產生                                                              
  2. idle> conn scott/tiger                                                                                         
  3. Connected.                                                                                                     
  4. scott@ORCL> select sid,serial#,username from v$session where username is not null;                             
  5.                                                                                                                
  6.        SID    SERIAL# USERNAME                                                                                 
  7. ---------- ---------- -------------------------                                                                 
  8.        159          5 SCOTT                                                                                    
  9.                                                                                                                
  10. scott@SQL> ho ps -ef | grep oracleorcl                                                                         
  11. oracle    5696  5686  0 19:32 ?        00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))   
  12. oracle    5699  5686  0 19:32 pts/0    00:00:00 /bin/bash -c ps -ef | grep oracleorcl                          
--&gt使用scott身份登入,有一個對應的伺服器程式被產生 idle> conn scott/tiger Connected. scott@ORCL> select sid,serial#,username from v$session where username is not null; SID SERIAL# USERNAME ---------- ---------- ------------------------- 159 5 SCOTT scott@SQL> ho ps -ef | grep oracleorcl oracle 5696 5686 0 19:32 ? 00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq))) oracle 5699 5686 0 19:32 pts/0 00:00:00 /bin/bash -c ps -ef | grep oracleorcl
    3. 無連線,無會話,單個程式      
  1. --&gt使用disconnect斷開會話,但對應的伺服器程式並沒有撤銷,直到使用exit則對應的伺服器程式被釋放                  
  2. scott@SQL> disconnect                                                                                         
  3. Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production                      
  4. With the Partitioning, OLAP, Data Mining and Real Application Testing options                                 
  5.                                                                                                               
  6. --&gt此時開啟另外一個會話session2來檢視scott的會話是否還存在,從下面的查詢中已經不存在scott使用者的會話            
  7. sys@ORCL> select sid,serial#,username from v$session where username='SCOTT';                                  
  8.                                                                                                               
  9. no rows selected                                                                                              
  10.                                                                                                               
  11. scott@SQL> ho ps -ef | grep 5696  --&gt對應的後臺程式依然存在                                                    
  12. oracle    5696  5686  0 19:32 ?        00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))  
  13. oracle    5702  5686  0 19:32 pts/0    00:00:00 /bin/bash -c ps -ef | grep 5696                               
  14.                                                                                                               
  15. scott@ORCL> exit                                                                                              
  16. Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production                      
  17. With the Partitioning, OLAP, Data Mining and Real Application Testing options                                 
  18. [oracle@odbp admin]$ ps -ef | grep 5696  --&gtexit命令退出後則相應的程式5696被釋放                               
  19. oracle    4082 16943  0 19:45 pts/0    00:00:00 grep 5696                                                     
--&gt使用disconnect斷開會話,但對應的伺服器程式並沒有撤銷,直到使用exit則對應的伺服器程式被釋放 scott@SQL> disconnect Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options --&gt此時開啟另外一個會話session2來檢視scott的會話是否還存在,從下面的查詢中已經不存在scott使用者的會話 sys@ORCL> select sid,serial#,username from v$session where username='SCOTT'; no rows selected scott@SQL> ho ps -ef | grep 5696 --&gt對應的後臺程式依然存在 oracle 5696 5686 0 19:32 ? 00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq))) oracle 5702 5686 0 19:32 pts/0 00:00:00 /bin/bash -c ps -ef | grep 5696 scott@ORCL> exit Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options [oracle@odbp admin]$ ps -ef | grep 5696 --&gtexit命令退出後則相應的程式5696被釋放 oracle 4082 16943 0 19:45 pts/0 00:00:00 grep 5696
    4. 單個連線,多個會話,單個程式       
  1. --&gt從檢視中觀察對應的session與後臺程式                                                                          
  2. --&gt在session1中使用scott登入                                                                                    
  3. idle> conn scott/tiger;                                                                                        
  4. Connected.                                                                                                     
  5.                                                                                                                
  6. --&gt在session2 中使用sys帳戶登入                                                                                 
  7. sys@ORCL> select sid,serial#,username from v$session where username is not null;                               
  8.                                                                                                                
  9.        SID    SERIAL# USERNAME                                                                                 
  10. ---------- ---------- ------------------------------                                                            
  11.        141          4 SYS                                                                                      
  12.        159          5 SCOTT                                                                                    
  13.                                
  14.                                                                                                                
  15. --&gt在session1中開啟autotrace功能                                                                                
  16. scott@ORCL> set autotrace on                                                                                   
  17.                                                                                                                
  18. --&gt可以看到在session2的v$session檢視查詢時多出了一個賬戶為scott,但SID與SERIAL#與之前不同的記錄                 
  19. sys@ORCL> set linesize 160                                                                                     
  20. sys@ORCL> SELECT spid, s.sid, s.serial#,s.status,s.username, p.program                                         
  21.   2  FROM v$process p, v$session s                                                                             
  22.   3  WHERE p.addr = s.paddr                                                                                    
  23.   4  and s.username='SCOTT';                                                                                   
  24.                                                                                                                
  25. SPID                SID    SERIAL# STATUS   USERNAME                  PROGRAM                                  
  26. ------------ ---------- ---------- -------- ------------------------- --------------------------------------    
  27. 4602                159          5 INACTIVE SCOTT                     oracle@oradb.robinson.com (TNS V1-V3)    
  28. 4602                139         25 INACTIVE SCOTT                     oracle@oradb.robinson.com (TNS V1-V3)    
  29.                                                                                                                
  30. sys@ORCL> ho ps -ef | grep 4602                                                                                
  31. oracle    4602  4499  0 18:36 ?        00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))   
  32. oracle    4856  4655  0 18:47 pts/3    00:00:00 /bin/bash -c ps -ef | grep 4602                                
  33.                                                                                                                
  34. sys@ORCL> ho ps -ef | grep oracleorcl                                                                          
  35. oracle    4602  4499  0 18:36 ?        00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))   
  36. oracle    4656  4655  0 18:36 ?        00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))   
  37. oracle    4859  4655  0 18:47 pts/3    00:00:00 /bin/bash -c ps -ef | grep oracleorcl                          
  38.                                                                                                                
  39. --&gt從上面的查詢結果可以看出,SCOTT使用者對應的後臺程式僅有一個,其spid為4062                                     
--&gt從檢視中觀察對應的session與後臺程式 --&gt在session1中使用scott登入 idle> conn scott/tiger; Connected. --&gt在session2 中使用sys帳戶登入 sys@ORCL> select sid,serial#,username from v$session where username is not null; SID SERIAL# USERNAME ---------- ---------- ------------------------------ 141 4 SYS 159 5 SCOTT --&gt在session1中開啟autotrace功能 scott@ORCL> set autotrace on --&gt可以看到在session2的v$session檢視查詢時多出了一個賬戶為scott,但SID與SERIAL#與之前不同的記錄 sys@ORCL> set linesize 160 sys@ORCL> SELECT spid, s.sid, s.serial#,s.status,s.username, p.program 2 FROM v$process p, v$session s 3 WHERE p.addr = s.paddr 4 and s.username='SCOTT'; SPID SID SERIAL# STATUS USERNAME PROGRAM ------------ ---------- ---------- -------- ------------------------- -------------------------------------- 4602 159 5 INACTIVE SCOTT oracle@oradb.robinson.com (TNS V1-V3) 4602 139 25 INACTIVE SCOTT oracle@oradb.robinson.com (TNS V1-V3) sys@ORCL> ho ps -ef | grep 4602 oracle 4602 4499 0 18:36 ? 00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq))) oracle 4856 4655 0 18:47 pts/3 00:00:00 /bin/bash -c ps -ef | grep 4602 sys@ORCL> ho ps -ef | grep oracleorcl oracle 4602 4499 0 18:36 ? 00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq))) oracle 4656 4655 0 18:36 ? 00:00:00 oracleorcl (DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq))) oracle 4859 4655 0 18:47 pts/3 00:00:00 /bin/bash -c ps -ef | grep oracleorcl --&gt從上面的查詢結果可以看出,SCOTT使用者對應的後臺程式僅有一個,其spid為4062
        set autotrace 完成的動作           
  1. 當啟用set autotrace功能後,通常會建立一個新的會話用於監控當前的操作並返回統計資訊,下面描述其過程                 
  2. a.在session1執行一個查詢,則此時原來建立的會話(159,5)執行DML或DQL操作                                              
  3. b.新建立的會話(139,25)會話則開始查詢V$SESSTAT 檢視來記住實際會話(即執行DML 的會話)的初始統計值                    
  4. c.原會話(139,25)中得DML或DQL操作執行                                                                              
  5. d.新會話(139,25)將再次查詢V$SESSTAT 檢視,根據與上次的差值計算統計資訊並生成執行時的執行計劃以及統計資訊予以返回  
當啟用set autotrace功能後,通常會建立一個新的會話用於監控當前的操作並返回統計資訊,下面描述其過程 a.在session1執行一個查詢,則此時原來建立的會話(159,5)執行DML或DQL操作 b.新建立的會話(139,25)會話則開始查詢V$SESSTAT 檢視來記住實際會話(即執行DML 的會話)的初始統計值 c.原會話(139,25)中得DML或DQL操作執行 d.新會話(139,25)將再次查詢V$SESSTAT 檢視,根據與上次的差值計算統計資訊並生成執行時的執行計劃以及統計資訊予以返回
        有關啟用set autotrace 請參考:啟用 AUTOTRACE 功能       
  1. --&gt下面演示在session1中的查詢                                                                                  
  2. scott@ORCL> select count(1) from emp;                                                                         
  3.                                                                                                               
  4.   COUNT(1)                                                                                                    
  5. ----------                                                                                                     
  6.         14                                                                                                    
  7.                                                                                                               
  8. Execution Plan                                                                                                
  9. ----------------------------------------------------------                                                     
  10. Plan hash value: 2937609675                                                                                   
  11.                                                                                                               
  12. -------------------------------------------------------------------                                            
  13. | Id  | Operation        | Name   | Rows  | Cost (%CPU)| Time     |                                           
  14. -------------------------------------------------------------------                                            
  15. |   0 | SELECT STATEMENT |        |     1 |     1   (0)| 00:00:01 |                                           
  16. |   1 |  SORT AGGREGATE  |        |     1 |            |          |                                           
  17. |   2 |   INDEX FULL SCAN| PK_EMP |    14 |     1   (0)| 00:00:01 |                                           
  18. -------------------------------------------------------------------                                            
  19.                                                                                                               
  20. Statistics                                                                                                    
  21. ----------------------------------------------------------                                                     
  22.         296  recursive calls                                                                                  
  23.           0  db block gets                                                                                    
  24.          54  consistent gets                                                                                  
  25.           1  physical reads                                                                                   
  26.           0  redo size                                                                                        
  27.         411  bytes sent via SQL*Net to client                                                                 
  28.         385  bytes received via SQL*Net from client                                                           
  29.           2  SQL*Net roundtrips to/from client                                                                
  30.           6  sorts (memory)                                                                                   
  31.           0  sorts (disk)                                                                                     
  32.           1  rows processed                                                                                   
  33.                                                                                                               
  34. scott@ORCL> set autotrace off;                                                                                
  35. --&gt在session2中再次執行查詢,可以看到會話139,25已經被釋放                                                      
  36. sys@ORCL> /                                                                                                   
  37.                                                                                                               
  38. SPID                SID    SERIAL# STATUS   USERNAME                  PROGRAM                                 
  39. ------------ ---------- ---------- -------- ------------------------- --------------------------------------   
  40. 4602                159          5 INACTIVE SCOTT                     oracle@oradb.robinson.com (TNS V1-V3)   
--&gt下面演示在session1中的查詢 scott@ORCL> select count(1) from emp; COUNT(1) ---------- 14 Execution Plan ---------------------------------------------------------- Plan hash value: 2937609675 ------------------------------------------------------------------- | Id | Operation | Name | Rows | Cost (%CPU)| Time | ------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 1 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | | | | 2 | INDEX FULL SCAN| PK_EMP | 14 | 1 (0)| 00:00:01 | ------------------------------------------------------------------- Statistics ---------------------------------------------------------- 296 recursive calls 0 db block gets 54 consistent gets 1 physical reads 0 redo size 411 bytes sent via SQL*Net to client 385 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 6 sorts (memory) 0 sorts (disk) 1 rows processed scott@ORCL> set autotrace off; --&gt在session2中再次執行查詢,可以看到會話139,25已經被釋放 sys@ORCL> / SPID SID SERIAL# STATUS USERNAME PROGRAM ------------ ---------- ---------- -------- ------------------------- -------------------------------------- 4602 159 5 INACTIVE SCOTT oracle@oradb.robinson.com (TNS V1-V3)
    5.SID不變,serial#變化的情形       
  1. --&gt將所有的會話全部退出,下面來檢視SID不變而serial#變化的情形                                                              
  2. [oracle@oradb ~]$ ps -ef | grep oracleorcl    --&gt此時Oracle資料庫無任何伺服器程式                                          
  3. oracle   26767 16943  0 19:49 pts/0    00:00:00 grep oracleorcl                                                             
  4. [oracle@oradb ~]$ sqlplus scott/tiger@orcl                                                                                
  5.                                                                                                                           
  6. Connected to:                                                                                                             
  7. Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production                                                    
  8. With the Partitioning, OLAP and Data Mining options                                                                       
  9.                                                                                                                           
  10. scott@ORCL> select sid,serial#,username from v$session where username='SCOTT';                                            
  11.                                                                                                                           
  12.        SID    SERIAL# USERNAME                                                                                            
  13. ---------- ---------- ------------------------------                                                                       
  14.        134         39 SCOTT                                                                                               
  15.                                                                                                                           
  16. scott@ORCL> exit                                                                                                          
  17. Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production                                  
  18. With the Partitioning, OLAP and Data Mining options                                                                       
  19. [uniread] Saved history (652 lines)                                                                                       
  20. [oracle@oradb ~]$ sqlplus scott/tiger@orcl                                                                                
  21.                                                                                                                           
  22. Connected to:                                                                                                             
  23. Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production                                                    
  24. With the Partitioning, OLAP and Data Mining options                                                                       
  25.                                                                                                                           
  26. scott@ORCL> select sid,serial#,username from v$session where username='SCOTT';                                            
  27.                                                                                                                           
  28.        SID    SERIAL# USERNAME                                                                                            
  29. ---------- ---------- ------------------------------                                                                       
  30.        134         41 SCOTT                                                                                               
  31. --&gt從上面的情形可以看出儘管scott使用者退出後重新登入,依然使用了相同的SID,因此在執行kill session時,一定要注意SID,serial#   
  32. --&gt兩者的值,以免kill掉不該kill的session                                                                                  
--&gt將所有的會話全部退出,下面來檢視SID不變而serial#變化的情形 [oracle@oradb ~]$ ps -ef | grep oracleorcl --&gt此時Oracle資料庫無任何伺服器程式 oracle 26767 16943 0 19:49 pts/0 00:00:00 grep oracleorcl [oracle@oradb ~]$ sqlplus scott/tiger@orcl Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production With the Partitioning, OLAP and Data Mining options scott@ORCL> select sid,serial#,username from v$session where username='SCOTT'; SID SERIAL# USERNAME ---------- ---------- ------------------------------ 134 39 SCOTT scott@ORCL> exit Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production With the Partitioning, OLAP and Data Mining options [uniread] Saved history (652 lines) [oracle@oradb ~]$ sqlplus scott/tiger@orcl Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production With the Partitioning, OLAP and Data Mining options scott@ORCL> select sid,serial#,username from v$session where username='SCOTT'; SID SERIAL# USERNAME ---------- ---------- ------------------------------ 134 41 SCOTT --&gt從上面的情形可以看出儘管scott使用者退出後重新登入,依然使用了相同的SID,因此在執行kill session時,一定要注意SID,serial# --&gt兩者的值,以免kill掉不該kill的session
        有關kill session的說明,請參考:Oracle 徹底 kill session

三、session與process的設定關係
    session:指定了一個例項中允許的會話數,即能同時登入到資料庫的併發使用者數。
    process: 指定了一個例項在作業系統級別能同時執行的程式數,包括後臺程式與伺服器程式。
    由上面的分析可知,一個後臺程式可能同時對應對個會話,因此通常sessions的值是大於processes的值
    通常的設定公式
        sessions = 1.1 * processes + 5       
  1. --&gt如在下面的系統的設定中processes得值為150,session的值設定為170,              
  2.                                                                                     
  3. scott@ORCL> select name,value from v$parameter where name='processes';          
  4.                                                                                 
  5. NAME                 VALUE                                                      
  6. -------------------- --------------------                                        
  7. processes            150                                                        
  8.                                                                                 
  9. scott@ORCL> select name,value from v$parameter where name='sessions';           
  10.                                                                                 
  11. NAME                 VALUE                                                      
  12. -------------------- --------------------                                        
  13. sessions             170                                                        
  14.                                                                                 
  15. scott@ORCL> select 150*1.1+5 from dual;                                         
  16.                                                                                 
  17.  150*1.1+5                                                                      
  18. ----------                                                                       
  19.        170    

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22578826/viewspace-703462/,如需轉載,請註明出處,否則將追究法律責任。

相關文章