记得在一个群里曾经讨论过一个问题:是先有session还是先有connection呢?当时也没有定论。回答问题的要点在于:什么是session,什么是connection,两者的存在是以什么为标识的,两者直接有怎样的依存关系。
首先看下三者的概念。以下所有概念来源于11gconcept。
Process
[php]
All connected Oracle Database users must run two modules of code to access an Oracle Database instance.
Application or Oracle tool: A database user runs a database application (such as a precompiler program) or an Oracle tool (such as SQL*Plus), which issues SQL statements to an Oracle database.
Oracle database server code: Each user has some Oracle database code executing on his or her behalf, which interprets and processes the application's SQL statements.
These code modules are run by processes. A process is a "thread of control" or a mechanism in an operating system that can run a series of steps. (Some operating systems use the terms job or task.) A process normally has its own private memory area in which it runs.
The processes in an Oracle Database system can be categorized into two major groups:
User processes run the application or Oracle tool code.
Oracle Database processes run the Oracle database server code. They include server processes and background processes.[/php]
process其实就是程序代码的运行时版。可以按照作者意图去自动执行特定的任务。Oracle安装process所执行任务的来源分为user process和server process.
[php]
Connections and Sessions
Connection and session are closely related to user process but are very different in meaning.
A connection is a communication pathway between a user process and an Oracle Database instance. A communication pathway is established using available interprocess communication mechanisms (on a computer that runs both the user process and Oracle Database) or network software (when different computers run the database application and Oracle Database, and communicate through a network).
A session is a specific connection of a user to an Oracle Database instance through a user process. For example, when a user starts SQL*Plus, the user must provide a valid user name and password, and then a session is established for that user. A session lasts from the time the user connects until the time the user disconnects or exits the database application.
Multiple sessions can be created and exist concurrently for a single Oracle Database user using the same user name. For example, a user with the user name/password of SCOTT/TIGER can connect to the same Oracle Database instance several times.
In configurations without the shared server, Oracle Database creates a server process on behalf of each user session. However, with the shared server, many user sessions can share a single server process.
[/php]
connection其实就是通信链路,对oracle讲就是user process和instance间的通信链路,可以使用IPC或者TCP之类的网络机制。
session则是一个数据库用户在user process和instance之间所建立的一个具体链接.session存在的前提是建立了连接。建立了链接的同时,session已经存在了。当user信息错误时,user process向instance发送链接请求,instance验证user信息,发现user信息错误,返回错误信息。这个过程只是两者的通信,试图建链,还根本没建立成功,这时候既不存在connection也不存在session。从网络模型上理解二者之间的关系,应该会比较容易点。似乎建立了链接的同时,session也跟着出现了。
举个例子,打电话。但我们想和对方通话的时候,我们首先要拨号,当对方按下接听键时,双方直接就建立一条通信链路,这时双方就可以谈话了,session其实就是指从会话准备就绪开始到会话结束,会话结束了就要断开链路。
session可以有active和inactive的状态,当按下接听键时,链路建立了,当我们说hello时,会话开始了,当我们交谈时是active的,当我们沉默时是inactive的,当我们说bye的时候,会话结束了,当我们挂机的时候,链路断了。
所以到底是现有session还是先有connection的问题也就清楚了,其实他们是同时的,只是对同一事件同一现象同一事务在不同层次上的描述,从底层通信看,是叫connection,从上层user process和instance看,是两者的session已经开始了。当我们敲exit时,会话中断,底层拆链。
如果非要分个先后的话,那session是要放在connection之间的。
至于server process和要讨论的问题没什么关系。
以上只是一些原理上的猜测,还是有两个不确定点的,一是connection和session建立的标准是什么,这个是至关重要的,二是oracle处理的二者的方法是什么。事实上oracle建立connection后是要去建立session信息,也就是say hello to user process,所以session建立似乎是要在connection后的。
以上只是我的一些猜想,欢迎大家拍砖。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7591490/viewspace-1008809/ ,如需转载,请注明出处,否则将追究法律责任。