Oracle session active 和 inactive 狀態 說明



. Session 狀態說明

            可以通過v$session 視圖的status列查看session 的狀態。  關於該視圖的使用,參考聯機文檔:

V$SESSION

http://download.oracle.com/docs/cd/E11882_01/server.112/e17110/dynviews_3016.htm#REFRN30223

 

 

STATUS

VARCHAR2(8)

Status of the session:

ACTIVE - Session currently executing SQL

INACTIVE

KILLED - Session marked to be killed

CACHED - Session temporarily cached for use by Oracle*XA

SNIPED - Session inactive, waiting on the client

 

 

有關狀態的說明:

1active 處於此狀態的會話,表示正在執行,處於活動狀態。

            官方文檔說明:

            Any session that is connected to the database and is waiting for an event that does not belong to the Idle wait class is considered as an active session.

 

2killed處於此狀態的會話,被標註爲刪除,表示出現了錯誤,正在回滾。

            當然,也是佔用系統資源的。還有一點就是,killed的狀態一般會持續較長時間,而且用windows下的工具pl/sql developerkill掉,是不管用的,要用命令:alter system kill session 'sid,serial#' ;

 

3inactive 處於此狀態的會話表示不是正在執行的

            該狀態處於等待操作(即等待需要執行的SQL語句),通常當DML語句已經完成。 但連接沒有釋放,這個可能是程序中沒有釋放,如果是使用中間件來連接的話,也可能是中間件的配置或者是bug 導致。

 

            inactive對數據庫本身沒有什麼影響,但是如果程序沒有及時commit,那麼就會造成佔用過多會話。容易是DB session 達到極限值。

            問了幾個朋友,他們的做法是不處理inactive 狀態的session 如果達到了session 的最大值, 就增加processes  sessions 參數。 如果kill inactive session 可能會到中間件有影響。 具體中間件這塊我也不太熟,等以後弄清楚了,在說。

 

 

. 處理inactive 狀態的session

            在前面說不處理inactive 狀態的session,但是還是有方法來解決的。 有兩種方法。

 

2.1  sqlnet.ora文件中設置expire_time 參數

官網有關這個參數的說明:

http://download.oracle.com/docs/cd/B19306_01/network.102/b14213/sqlnet.htm

 

SQLNET.EXPIRE_TIME

Purpose

            Use parameter SQLNET.EXPIRE_TIME to specify a the time interval, in minutes, to send a probe to verify that client/server connections are active. Setting a value greater than 0 ensures that connections are not left open indefinitely, due to an abnormal client termination. If the probe finds a terminated connection, or a connection that is no longer in use, it returns an error, causing the server process to exit. This parameter is primarily intended for the database server, which typically handles multiple connections at any one time.

            sqlnet.expire_time 的原理:Oracle Server 發送包探測dead connection ,如果連接關閉,或者不再用,則關閉相應的server process.

 

Limitations on using this terminated connection detection feature are:

1It is not allowed on bequeathed connections.

2Though very small, a probe packet generates additional traffic that may downgrade network performance.

3Depending on which operating system is in use, the server may need to perform additional processing to distinguish the connection probing event from other events that occur. This can also result in degraded network performance.

 

Default 0

Minimum Value 0

Recommended Value 10

 

Example

SQLNET.EXPIRE_TIME=10

 

 

2.2 設置用戶profileidle_time 參數

           

之前整理的一篇有關profile的文章:

            Oracle 用戶 profile 屬性

            http://blog.csdn.net/tianlesoftware/archive/2011/03/10/6238279.aspx

 

            注意,要啓用idle_time 要先啓用RESOURCE_LIMIT參數。 該參數默認是False 官網說明如下:

 

RESOURCE_LIMIT

Property

Description

Parameter type

Boolean

Default value

false

Modifiable

ALTER SYSTEM

Range of values

true | false

 

            RESOURCE_LIMIT determines whether resource limits are enforced in database profiles.

 

Values:

            TRUE Enables the enforcement of resource limits

            FALSEDisables the enforcement of resource limits

 

如下blog 在這塊說的比較清楚,並提供了相關的腳本:

            sqlnet.expire_time and IDLE_TIME

            http://space.itpub.net/10687595/viewspace-420407

 

 

            IDLE_TIME Specify the permitted periods of continuous inactive time during a session, expressed in minutes. Long-running queries and other operations are not subject to this limit.

 

            A valid database connection that is idle will respond to the probe packet causing no action on the part of the Server ,whereas the resource_limit will snipe the session when idle_time is exceeded. The 'sniped' session will get disconnected when the user(or the user process) tries to communicate with the server again.

           

            -- 通過idle_time限制session idle 時間。session idle超過設置時間,狀態爲sniped (v$session).,然而OS下的process並不會釋放,當session(user process) 再次與server process 通訊,將關閉相應的server process.

 

What does 'SNIPED' status in v$session mean?

            When IDLE_TIME is set in the users' profiles or the default profile. This will kill the sessions in the database (status in v$session now becomes SNIPED) and they will eventually disconnect. It does not always clean up the Unix session (LOCAL=NO sessions).

            At this time all oracle resources are released but the shadow processes remains and OS resources are not released. This shadow process is still counted towards the parameters of init.ora.

            This process is killed and entry from v$session is released only when user again tries to do something. Another way of forcing disconnect (if your users come in via SQL*Net) is to put the file sqlnet.ora on every client machine and include the parameter "SQLNET.EXPIRE_TIME" in it to force the close of the SQL*Net session

sqlnet.expire_time

            sqlnet.expire_time actually works on a different principle and is used to detect dead connections as opposed to disconnecting(actually 'sniping') a session based on idle_time which the profile accomplishes.


            But again, as you mentioned, expire_time works globally while idle_time profile works for that user. You can use both of them to make sure that the client not only gets sniped but also gets disconnected if the user process abnormally terminates.

 

修改示例:

SQL>alter profile default limit idle_time 10;

--需要重啓下oracle

 

查詢應用的連接數SQL:

/* Formatted on 2011/6/12 13:06:23 (QP5 v5.163.1008.3004) */

  SELECT b.MACHINE, b.PROGRAM, COUNT (*)

    FROM v$process a, v$session b

   WHERE a.ADDR = b.PADDR AND b.USERNAME IS NOT NULL

GROUP BY b.MACHINE, b.PROGRAM

ORDER BY COUNT (*) DESC;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章