ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 进程、会话、连接之间的差异

进程、会话、连接之间的差异

原创 Linux操作系统 作者:beatony 时间:2011-07-29 11:21:50 0 删除 编辑
 

进程、会话、连接之间的差异

分类: Oracle 其它特性 20人阅读 评论(0) 收藏 举报
--========================
-- 进程、会话、连接之间的差异
--========================


    在使用Oracle database的时候,连接与会话是我们经常碰到的词语之一。咋一看貌似一回事,事实则不然。一个连接上可以建立零个、
一个、甚至多个会话。啊,咋这样呢?是的,没错。这也是我们经常误解的原因。
    各个会话之间是单独的,独立于其他会话,即便是同一个连接的多个会话也是如此。
    
一、几个术语之间的定义(参照Oracle 9i &10g 编程艺术)
    
        连接(connection):连接是从客户到Oracle 实例的一条物理路径。连接可以在网络上建立,或者通过IPC 机制建立。通常会在
    客户进程与一个专用服务器或一个调度器之间建立连接。
    
        会话(session):会话是实例中存在的一个逻辑实体。这就是你的会话状态(session state),也就是表示特定会话的一组内存
    中的数据结构.提到"数据库连接"时,大多数人首先想到的就是“会话”。你要在服务器中的会话上执行SQL、提交事务和运行存储过程。

二、通过例子演示来查看之间的关系
    1. 无连接,无会话,无进程的情形           
  1. -->没有建立建立连接时,没有任何会话服务器进程                                            
  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    -->使用nolog登录是同样也看不到任何会话服务器进程    
  11. oracle    5691  5686  0 19:31 pts/0    00:00:00 /bin/bash -c ps -ef | grep oracleorcl   
-->没有建立建立连接时,没有任何会话服务器进程 [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 -->使用nolog登录是同样也看不到任何会话服务器进程 oracle 5691 5686 0 19:31 pts/0 00:00:00 /bin/bash -c ps -ef | grep oracleorcl
       2. 单个连接,单个会话,单个进程       
  1. -->使用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                          
-->使用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. -->使用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. -->此时开启另外一个会话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  -->对应的后台进程依然存在                                                    
  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  -->exit命令退出后则相应的进程5696被释放                               
  19. oracle    4082 16943  0 19:45 pts/0    00:00:00 grep 5696                                                     
-->使用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 -->此时开启另外一个会话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 -->对应的后台进程依然存在 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 -->exit命令退出后则相应的进程5696被释放 oracle 4082 16943 0 19:45 pts/0 00:00:00 grep 5696
    4. 单个连接,多个会话,单个进程       
  1. -->从视图中观察对应的session与后台进程                                                                          
  2. -->在session1中使用scott登录                                                                                    
  3. idle> conn scott/tiger;                                                                                        
  4. Connected.                                                                                                     
  5.                                                                                                                
  6. -->在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. -->在session1中开启autotrace功能                                                                                
  16. scott@ORCL> set autotrace on                                                                                   
  17.                                                                                                                
  18. -->可以看到在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. -->从上面的查询结果可以看出,SCOTT用户对应的后台进程仅有一个,其spid为4062                                     
-->从视图中观察对应的session与后台进程 -->在session1中使用scott登录 idle> conn scott/tiger; Connected. -->在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 -->在session1中开启autotrace功能 scott@ORCL> set autotrace on -->可以看到在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 -->从上面的查询结果可以看出,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. -->下面演示在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. -->在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)   
-->下面演示在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; -->在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. -->将所有的会话全部退出,下面来查看SID不变而serial#变化的情形                                                              
  2. [oracle@oradb ~]$ ps -ef | grep oracleorcl    -->此时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. -->从上面的情形可以看出尽管scott用户退出后重新登录,依然使用了相同的SID,因此在执行kill session时,一定要注意SID,serial#   
  32. -->两者的值,以免kill掉不该kill的session                                                                                  
-->将所有的会话全部退出,下面来查看SID不变而serial#变化的情形 [oracle@oradb ~]$ ps -ef | grep oracleorcl -->此时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 -->从上面的情形可以看出尽管scott用户退出后重新登录,依然使用了相同的SID,因此在执行kill session时,一定要注意SID,serial# -->两者的值,以免kill掉不该kill的session
        有关kill session的说明,请参考:Oracle 彻底 kill session

三、session与process的设置关系
    session:指定了一个实例中允许的会话数,即能同时登录到数据库的并发用户数。
    process: 指定了一个实例在操作系统级别能同时运行的进程数,包括后台进程与服务器进程。
    由上面的分析可知,一个后台进程可能同时对应对个会话,因此通常sessions的值是大于processes的值
    通常的设置公式
        sessions = 1.1 * processes + 5       
  1. -->如在下面的系统的设置中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博客 ” ,链接:https://blog.itpub.net/22578826/viewspace-703462/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-05-18

  • 博文量
    192
  • 访问量
    495757