zoukankan      html  css  js  c++  java
  • 进程、会话、连接之间的差异

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

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

    二、通过例子演示来查看之间的关系
        1. 无连接,无会话,无进程的情形            

    [sql] view plain copy
     
     print?
    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   

           2. 单个连接,单个会话,单个进程        

    [sql] view plain copy
     
     print?
    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                          

        3. 无连接,无会话,单个进程      

    [sql] view plain copy
     
     print?
    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                                                     

        4. 单个连接,多个会话,单个进程       

    [sql] view plain copy
     
     print?
    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                                     

            set autotrace 完成的动作            

    [sql] view plain copy
     
     print?
    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 请参考:启用 AUTOTRACE 功能        

    [sql] view plain copy
     
     print?
    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)   

        5.SID不变,serial#变化的情形        

    [sql] view plain copy
     
     print?
    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                                                                                  

            有关kill session的说明,请参考:Oracle 彻底 kill session

    三、session与process的设置关系
        session:指定了一个实例中允许的会话数,即能同时登录到数据库的并发用户数。
        process: 指定了一个实例在操作系统级别能同时运行的进程数,包括后台进程与服务器进程。
        由上面的分析可知,一个后台进程可能同时对应对个会话,因此通常sessions的值是大于processes的值
        通常的设置公式
            sessions = 1.1 * processes + 5        

    [sql] view plain copy
     
     print?
    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                                                                            

    四、更多参考
        DIFFERENCES BETWEEN PROCESSES, SESSIONS AND CONNECTIONS       

    五、快捷参考

    有关性能优化请参考

        Oracle硬解析与软解析

        共享池的调整与优化(Sharedpool Tuning)

        Buffercache 的调整与优化(一)

        Oracle表缓存(cachingtable)的使用

    有关ORACLE体系结构请参考

        Oracle表空间与数据文件

        Oracle密码文件

        Oracle参数文件

        Oracle联机重做日志文件(ONLINE LOG FILE)

        Oracle控制文件(CONTROLFILE)

        Oracle归档日志

        Oracle回滚(ROLLBACK)和撤销(UNDO)

        Oracle数据库实例启动关闭过程

        Oracle10g SGA 的自动化管理

        Oracle实例和Oracle数据库(Oracle体系结构)

    有关闪回特性请参考

        Oracle闪回特性(FLASHBACK DATABASE)

        Oracle闪回特性(FLASHBACK DROP & RECYCLEBIN)

        Oracle闪回特性(Flashback Query、FlashbackTable)

        Oracle闪回特性(Flashback Version、Flashback Transaction)

    有关基于用户管理的备份和备份恢复的概念请参考

        Oracle冷备份

        Oracle热备份

        Oracle备份恢复概念

        Oracle实例恢复

        Oracle基于用户管理恢复的处理(详细描述了介质恢复及其处理)

        SYSTEM表空间管理及备份恢复

        SYSAUX表空间管理及恢复

    有关RMAN的备份恢复与管理请参考

        RMAN 概述及其体系结构

        RMAN 配置、监控与管理

        RMAN 备份详解

        RMAN 还原与恢复

        RMANcatalog 的创建和使用

        基于catalog 创建RMAN存储脚本

    基于catalog 的RMAN 备份与恢复

    使用RMAN迁移文件系统数据库到ASM

        RMAN 备份路径困惑(使用plus archivelog时)

    有关ORACLE故障请参考

        ORA-32004的错误处理

        ORA-01658错误

        CRS-0215错误处理

        ORA-00119,ORA-00132 错误处理

        又一例SPFILE设置错误导致数据库无法启动

        对参数FAST_START_MTTR_TARGET= 0 的误解及设定

        SPFILE错误导致数据库无法启动(ORA-01565)

    有关ASM请参考

        创建ASM实例及ASM数据库

        ASM 磁盘、目录的管理

        使用 ASMCMD 工具管理ASM目录及文件

    有关SQL/PLSQL请参考

        SQLPlus常用命令

        替代变量与SQL*Plus环境设置

        使用Uniread实现SQLplus翻页功能

        SQL 基础-->SELECT 查询

        SQL 基础--> NEW_VALUE的使用

        SQL 基础--> 集合运算(UNION与UNION ALL)

        SQL 基础--> 常用函数

        SQL 基础--> 视图(CREATEVIEW)

        SQL 基础--> 创建和管理表

        SQL 基础--> 多表查询

        SQL 基础--> 过滤和排序

        SQL 基础--> 子查询

        SQL 基础--> 分组与分组函数

        SQL 基础--> 层次化查询(STARTBY ... CONNECT BY PRIOR)

        SQL 基础--> ROLLUP与CUBE运算符实现数据汇总

        PL/SQL--> 游标

        PL/SQL--> 异常处理(Exception)

        PL/SQL--> 语言基础

        PL/SQL--> 流程控制

        PL/SQL--> PL/SQL记录

        PL/SQL--> 包的创建与管理

        PL/SQL--> 隐式游标(SQL%FOUND)

        PL/SQL--> 包重载、初始化

        PL/SQL--> DBMS_DDL包的使用

        PL/SQL--> DML 触发器

        PL/SQL--> INSTEAD OF 触发器

        PL/SQL--> 存储过程

        PL/SQL--> 函数

        PL/SQL--> 动态SQL

        PL/SQL--> 动态SQL的常见错误

    有关ORACLE其它特性

        Oracle常用目录结构(10g)

        使用OEM,SQL*Plus,iSQL*Plus 管理Oracle实例

        日志记录模式(LOGGING、FORCE LOGGING 、NOLOGGING)

        表段、索引段上的LOGGING与NOLOGGING

        OralceOMF 功能详解

        Oracle用户、对象权限、系统权限  

        Oracle角色、配置文件

        Oracle分区表

        Oracle外部表

        使用外部表管理Oracle 告警日志(ALAERT_$SID.LOG)

        簇表及簇表管理(Index clustered tables)

        数据泵 EXPDP 导出工具的使用

        数据泵 IMPDP 导入工具的使用

        导入导出 Oracle 分区表数据

        SQL*Loader使用方法

        启用用户进程跟踪

        配置非默认端口的动态服务注册

        配置ORACLE 客户端连接到数据库

        systemsys,sysoper sysdba 的区别

        ORACLE_SID、DB_NAME、INSTANCE_NAME、DB_DOMIAN、GLOBAL_NAME

        Oracle补丁全集 (Oracle 9i 10g 11g Path)

        Oracle10.2.0.1 升级到10.2.0.4

        Oracle彻底 kill session

  • 相关阅读:
    JSP_EL使用
    Ajax乱码问题
    Myeclipse安装svn插件(link方式)
    JAVA多线程通信
    Java序列化与反序列化(Serializable)
    Java 字符流实现文件读写操作(FileReader-FileWriter)
    Java 字节流实现文件读写操作(InputStream-OutputStream)
    JAVA环境变量配置
    Flex设置外部浏览器
    J2EE5(Servlet2.5)对EL表达式的支持
  • 原文地址:https://www.cnblogs.com/guanshan/p/guan2017-04-10.html
Copyright © 2011-2022 走看看