connection和session的关系

来源:互联网 发布:好看的20部同志网络剧 编辑:程序博客网 时间:2024/05/19 04:07
1.官方解释:
 
1.1 connection:
   A connection is a physical path from a client to an Oracle instance. A connection is established either over a network or over an IPC mechanism. A connection is typically between a client process and either a dedicated server or a dispatcher. However,using Oracle’s Connection Manager (CMAN), a connection may be between a client and CMAN, and CMAN and the database.
 
1.2 session
   A session is a logical entity that exists in the instance. It is your session state, or a collection of data structures in memory that represents your unique session. It is what would come first to most people’s minds when thinking of a “database connection.” It is your session in the server, where you execute SQL, commit transactions, and run stored procedures.
 
1.3 connection vs. session
   A connection may have zero, one, or more sessions established on it. Each session is separate and independent,even though they all share the same physical connection to the database. A commit in one session does not affect any other session on that connection. In fact, each session using that connection could use different user identities!
   In Oracle, a connection is simply a physical circuit between your client process and the database instance—a network connection, most commonly. The connection may be to a dedicated server process or to a dispatcher. As previously stated, a connection may have zero or more sessions, meaning that a connection may exist with no corresponding sessions. Additionally, a session may or may not have a connection. Using advanced Oracle Net features such as connection pooling, a physical connection may be dropped by a client, leaving the session intact (but idle). When the client wants to perform some operation in that session, it would reestablish the physical connection.
 
 
2.示例
 
SQL> conn seagull/aaaa
Connected.
SQL> select username||'  '||sid||'  '||serial#||'  '||server||'  '||paddr||'  '||status from v$session where username='SEAGULL';
USERNAME||''||SID||''||SERIAL#||''||SERVER||''||PADDR||''||STATUS
--------------------------------------------------------------------------------
SEAGULL  140  61  DEDICATED  2A220948  ACTIVE

SQL> set autotrace on statistics
SQL> select username||'  '||sid||'  '||serial#||'  '||server||'  '||paddr||'  '||status from v$session where username='SEAGULL';
USERNAME||''||SID||''||SERIAL#||''||SERVER||''||PADDR||''||STATUS
--------------------------------------------------------------------------------
SEAGULL  140  61  DEDICATED  2A220948  ACTIVE
SEAGULL  142  231  DEDICATED  2A220948  INACTIVE

Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          0  consistent gets
          0  physical reads
          0  redo size
        596  bytes sent via SQL*Net to client
        400  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          2  rows processed
SQL>

从例子可以看到,进程(paddr=2A220948)对应了两个session(140,142),即我们通过一个dedicated server拥有了两个sessoin,其实那个inactive的session(142)就是autotrace session,用来watch我们真正的sessoin的,可以知道autotrace在我们发出insert,delete,update,select,merge时会作出一下动作:
1)用当前的connection新建1个new session,当然,如果创建后就不用再创建了
2)然后new session会读取original session的v$sessstat视图并记录统计信息
3)接着在original session中执行DML
4)执行完毕后,new session会再次读取original session的v$sessstat信息,并且把和之前的差别统计出来,显示给用户看.
 
如果关闭autotrace,那么new session会消失,如下:
 
SQL> set autotrace off
SQL> select username||'  '||sid||'  '||serial#||'  '||server||'  '||paddr||'  '||status from v$session where username='SEAGULL';
USERNAME||''||SID||''||SERIAL#||''||SERVER||''||PADDR||''||STATUS
--------------------------------------------------------------------------------
SEAGULL  140  61  DEDICATED  2A220948  ACTIVE
SQL>
 
此时在sqlplus中执行disconnect操作:
SQL> disconnect
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining options
 
在另外的sqlplus登录界面里查询v$session
SQL> select * from v$session where username='SEAGULL';
now rows return
SQL> select addr,pid,spid,terminal,username from v$process where addr='2A220948';
ADDR            PID SPID         TERMINAL                       USERNAME
-------- ---------- ------------ ------------------------------ ---------------
2A220948         21 13132        pts/0                          oracle10
发现此时session(sid=140)已经没有了,但用之前的paddr='2A220948'还能查出信息,表明connection还在,表明1个connection可以对应0个session.
 
再次原来的sqlplus里面执行connect操作:
SQL> connect seagull/aaaa
Connected.

查询v$session
SQL> select username||'  '||sid||'  '||serial#||'  '||server||'  '||paddr||'  '||status from v$session where username='SEAGULL';
USERNAME||''||SID||''||SERIAL#||''||SERVER||''||PADDR||''||STATUS
--------------------------------------------------------------------------------
SEAGULL  146  103  DEDICATED  2A220948  ACTIVE
发现1个new session在原来的connection(paddr=2A220948)基础上又被创建了。
原创粉丝点击