轉(zhuǎn)載請注明出處
理解
oracle中連接和會話
1. 概念不同:
連接是指物理的客戶端到oracle服務(wù)端的連接。一般是通過一個網(wǎng)絡(luò)的連接。
在已建立的連接上,建立客戶端與oracle的會話,以后客戶端與oracle的交互都在一個會話環(huán)境中進(jìn)行。
2. 關(guān)系是多對多:[同意網(wǎng)友的意見,應(yīng)該是1對多。一個會話要么沒有連接,要么在一個連接上。]
一個連接上可以建立0個,1個,2個,多個會話。
Oracle允許存在這樣的會話,就是失去了物理連接的會話。
3. 概念應(yīng)用:
l oracle的sessions參數(shù)決定的是會話數(shù)而不是物理連接數(shù)。
l oracle的臨時表中的數(shù)據(jù)是各會話間隔離的,而與連接概念無關(guān)。
l jdbc的connection,對oracle是一個會話的概念。
4. 實際應(yīng)用:
連接池,為了提高數(shù)據(jù)庫交互效率,一般驅(qū)動程序都有連接池概念的實現(xiàn)。
Oracle不同的驅(qū)動提供兩種連接池概念。
一種譯為隱式連接緩沖
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:@10.1.3.60:1521:orcl");
ods.setUser("NC50_DEV");
ods.setPassword("NC50_DEV");
ods.setConnectionCachingEnabled(true);
java.util.Properties jup = new java.util.Properties();
jup.setProperty("InitialLimit", "1");
jup.setProperty("MinLimit", "1");
jup.setProperty("MaxLimit", "2");
ods.setConnectionCacheProperties(jup);
Connection cx0 = ods.getConnection();
隱式連接緩沖在第一次getconnection()時將根據(jù)配置初始化連接,并在每一個連接上建立一個會話。然后從這些會話中返回一個給本次調(diào)用。以后如果在本次connection.close()前調(diào)用getconnection(),將肯定得到另外一個會話。如果初始時建立的會話不夠了,那么將新建物理連接,并在連接上建立一個會話。當(dāng)然不能超過兩個限制:1.MaxLimit【見程序,限制物理連接數(shù)】。2.sessions【oracle參數(shù)】。每次close()并不關(guān)閉實際的會話,而是返回連接緩沖中,供另一個getconnection()使用。所以可以得知,如果一個getconnection()中給某一個會話級臨時表插入數(shù)據(jù),然后close(),那么之后如果這個會話被重新得到,是可以看到插入的數(shù)據(jù)的。
一種譯為OCI連接池。
OracleOCIConnectionPool ods = new OracleOCIConnectionPool();
String tns = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS="
+ "(PROTOCOL=TCP)(HOST=10.1.3.60)(PORT=1521)))"
+ "(CONNECT_DATA=(SERVICE_NAME=ORCL)))";
ods.setURL("jdbc:oracle:oci:@" + tns);
ods.setUser("NC50_DEV");
ods.setPassword("NC50_DEV");
java.util.Properties jup = new java.util.Properties();
jup.setProperty(OracleOCIConnectionPool.CONNPOOL_MIN_LIMIT, "2"); jup.setProperty(OracleOCIConnectionPool.CONNPOOL_MAX_LIMIT, "2");
jup.setProperty(OracleOCIConnectionPool.CONNPOOL_INCREMENT, "0");
ods.setPoolConfig(jup);
Connection cx0 = ods.getConnection();
OCI連接池在第一次getconnection()時將根據(jù)配置初始化連接,并在每一個連接上建立一個會話。注意,連接上一旦建立了一個會話,這個連接將還可以再建會話。那么在此時,會任取一個建完會話的物理連接再建一個會話返回。這樣看來似乎OCI連接池只需建立一個物理連接就可以建立任意多的會話,當(dāng)然這時也要受限于oracle允許的sessions設(shè)置。實際上OCI到底1個物理連接對應(yīng)多少個會話,OCI自身有一個對應(yīng)用透明的管理。但是CONNPOOL_MAX_LIMIT一定是限制住此客戶端可用的物理連接數(shù)的。對于OCI連接池,每一個connection.close()都將實際關(guān)閉會話,但物理連接保留。所以可以得知,如果一個getconnection()中給某一個會話級臨時表插入數(shù)據(jù),然后close(),那么此數(shù)據(jù)將被刪除。
5.關(guān)于專用服務(wù)器模式下processes參數(shù)
對于隱式連接緩沖,每一個連接上建立一個會話,對于每一個會話都將對應(yīng)一個dedicated process。
對于OCI連接池,只有物理連接上建立的那個會話才對應(yīng)一個dedicated process(這里記為DP_A,DP_B)。而其他會話對應(yīng)一個PSUEDO process.這是一個輕量級process,它是不算processes數(shù)的。
所以關(guān)于這個問題“專用服務(wù)器模式是一個物理連接對應(yīng)一個process,還是一個會話對應(yīng)一個process?”,從這里可以理解oracle的設(shè)計思路確實一個是會話對應(yīng)一個process,與物理連接不相關(guān)。但是實際的實現(xiàn)呢?當(dāng)oci獲得的connection上執(zhí)行sql時,ORCLED的響應(yīng)還是會借助到DP_A,DP_B上,如果DP_A,DP_B正忙,那就得一樣地等。
補(bǔ)充:
1.物理連接一般跟網(wǎng)絡(luò)資源有關(guān)的。通過網(wǎng)絡(luò)的一個物理連接會占用客戶端一個新的臨時端口。
2.JTA事務(wù)管理,是針對多個會話的DB訪問,維系在一個事務(wù)中。這個概念是多會話,而不是刻意強(qiáng)調(diào)多個數(shù)據(jù)源(兩個ORACLE),甚至異種數(shù)據(jù)庫。JDBC事務(wù)管理,是建立在一個會話上的事務(wù)。
3.processes參數(shù)控制的是oracle所有進(jìn)程/線程,不僅僅是專用server,包括后臺普通進(jìn)程,典型的如果在MTS模式下,還包括dispatcher。而OCI下連接PSUEDO process的作用有點類似MTS模式下的dispatcher,但它不受processes限制。同時注意即使有這個PSUEDO process存在,專用模式和MTS還是截然不同,因為專用模式下必須等到DP_A,DP_B的客戶會話關(guān)閉后才可用DP_A,DP_B來服務(wù)PSUEDO process對應(yīng)的客戶會話,而MTS是不等會話結(jié)束就拉過來服務(wù)了。
官方解釋
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.