kcbzps oracle_Oracle 11g DRCP配置与使用

Oracle 11g DRCP配置与使用

Oracle 11g推出了驻留连接池(Database Resident Connection Pool)特性,提供了数据库层面上的连接池管理机制,为应对高并发、短会话前端应用进行有益的尝试。

DRCP的配置很简单,本篇中让我们一起来配置一个11g环境上的DRCP,分析其工作特性。

1、Database Level Configuration

配置DRCP是分为两个步骤:database level configuration和application level configuration。首先在Database Server层面创建连接池对象。我们使用Oracle 11g进行试验。

SQL> select * from v$version;

BANNER

---------------------------------------

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

PL/SQL Release 11.2.0.1.0 - Production

CORE   11.2.0.1.0   Production

TNS for Linux: Version 11.2.0.1.0 - Production

NLSRTL Version 11.2.0.1.0–Production

在默认情况下,Oracle 11g中是有一个默认的连接池对象。在视图dba_cpool_info中,可以看到连接池信息。

SQL> select connection_pool, status, minsize, maxsize, INACTIVITY_TIMEOUT from dba_cpool_info;

CONNECTION_POOL               STATUS             MINSIZE   MAXSIZE INACTIVITY_TIMEOUT

------------------------------ ---------------- ---------- ---------- ------------------

SYS_DEFAULT_CONNECTION_POOL   INACTIVE                 4        40               300

连接池sys_default_connection_pool就是默认连接池,状态是inactive。使用dbms_connection_pool包,可以方便的对其进行管理。

SQL> exec dbms_connection_pool.start_pool();

PL/SQL procedure successfully completed

使用start_pool方法,可以对默认池进行开启。注意:该方法还有参数pool_name,如果指定了名称,就可以创建出一个自定义的连接池,也可以同时管理多个连接池情况。如果不指定名称,就针对默认连接池进行管理。

Start_pool之后,dba_cpool_info视图中可以看到连接池状态。

SQL> select connection_pool, status from dba_cpool_info;

CONNECTION_POOL               STATUS              MINSIZE

------------------------------ ---------------- ----------

SYS_DEFAULT_CONNECTION_POOL   ACTIVE                   4

注意,此时没有连接,但是我们观察后台进程,发现新增加了连接池Server Process对象。

[oracle@oracle11g ~]$ ps -ef | grep ora_n

oracle   5800    1 1 05:07 ?       00:00:00ora_n000_wilson

oracle   5816 5584 0 05:07 pts/0   00:00:00 grep ora_n

[oracle@oracle11g ~]$ ps -ef | grep ora_l

oracle   5689    1 0 05:03 ?       00:00:00 ora_lgwr_wilson

oracle   5802    1 0 05:07 ?       00:00:00ora_l000_wilson

oracle   5804    1 0 05:07 ?       00:00:00ora_l001_wilson

oracle   5806    1 0 05:07 ?       00:00:00 ora_l002_wilson

oracle   5808    1 0 05:07 ?       00:00:00ora_l003_wilson

oracle   5818 5584 0 05:07 pts/0   00:00:00 grep ora_l

其中ora_n000进程是对应的Connection Broker对象,负责连接管理。Ora_lxxx进程就是连接池中的连接对象。当没有连接的时候,连接池维持minsize大小,与配置minsize=4相匹配。

下面进行application level configuration。

2、Application Level Configuration

在应用层面,可以从连接字符串或者本地服务名上进行配置。本篇主要从本地服务名上进行配置。

在tnsnames.ora中,我们可以进行本地命名配置。为了保证正确性,笔者倾向使用Oracle提供的netca工具来进行基础配置。

--开启X Windows Passive模式

[oracle@oracle11g ~]$ export DISPLAY=192.168.0.1:0.0

[oracle@oracle11g ~]$ netca

Oracle Net Services Configuration:

Default local naming configuration complete.

Created net service name: wilsondrcp

Oracle Net Services configuration successful. The exit code is 0

之后,修改tnsname.ora文件的相关内容。

WILSONDRCP=

(DESCRIPTION =

(ADDRESS_LIST =

(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.88)(PORT = 1521))

)

(CONNECT_DATA =

(SERVER = POOLED) --新增加到其中,表明连接使用连接池;

(SERVICE_NAME = wilson)

)

)

可以使用tnsping方法进行验证。

[oracle@oracle11g ~]$ tnspingwilsondrcp

TNS Ping Utility for Linux: Version 11.2.0.1.0 - Production on 01-MAR-2012 05:10:46

Copyright (c) 1997, 2009, Oracle. All rights reserved.

Used parameter files:

/u01/oracle/network/admin/sqlnet.ora

Used TNSNAMES adapter to resolve the alias

Attempting

to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL =

TCP)(HOST = 192.168.0.88)(PORT = 1521))) (CONNECT_DATA =(SERVER =

POOLED)(SERVICE_NAME = wilson)))

OK (0 msec)

配置了wilsondrcp名称之后,就可以使用sqlplus等工具进行连接。

[oracle@oracle11g ~]$ sqlplus /nolog

SQL*Plus: Release 11.2.0.1.0 Production on Thu Mar 1 05:11:47 2012

Copyright (c) 1982, 2009, Oracle. All rights reserved.

SQL> conn scott/tiger@wilsondrcp

Connected.

SQL> select sid from v$mystat where rownum<2;

SID

----------

146

此时,会话(sid=146)已经连接。此时,我们可以查看后台进程情况。

--标记LOCAL的本地连接进程不存在;

[oracle@oracle11g ~]$ ps -ef | grep LOCAL

oracle   5963 5931 0 05:14 pts/1   00:00:00 grep LOCAL

那么,我们连接的会话进程究竟是哪一个呢?

SQL> select paddr from v$session where sid=146;

PADDR

--------

38BCD994

SQL> select pid, spid from v$process where addr='38BCD994';

PID SPID

---------- ------------------------

315806–OS Level的进程编号;

[oracle@oracle11g ~]$ ps -ef | grep 5806

oracle   5806    1 0 05:07 ?       00:00:00 ora_l002_wilson

oracle   5975 5931 0 05:15 pts/1   00:00:00 grep 5806

对应的ora_l002_wilson进程就是我们刚刚看到的连接池进程。说明:我们使用sqlplus连接使用的连接池对象是通过DRCP。

在sqlplus退出之后,l002进程依然存在,只是被释放回连接池。

SQL> quit

Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

[oracle@oracle11g ~]$ ps -ef | grep 5806

oracle   5806    1 0 05:07 ?       00:00:00 ora_l002_wilson

oracle   5981 5931 0 05:15 pts/1   00:00:00 grep 5806

[oracle@oracle11g ~]$

3、连接池关闭

DRCP连接池是可以关闭的,同样使用dbms_connection_pool的stop_pool方法。

--关闭connection pool

SQL> exec dbms_connection_pool.stop_pool;

PL/SQL procedure successfully completed

[oracle@oracle11g ~]$ ps -ef | grep ora_n

oracle   5800    1 0 05:07 ?       00:00:00 ora_n000_wilson

oracle   6008 5931 0 05:18 pts/1   00:00:00 grep ora_n

[oracle@oracle11g ~]$ ps -ef | grep ora_l

oracle   5689    1 0 05:03 ?       00:00:00 ora_lgwr_wilson

oracle   6010 5931 0 05:18 pts/1   00:00:00 grep ora_l

关闭连接池后,连接池中连接lxxx立即被销毁释放。Connection Broker进程暂时存在。但是,过一会之后,该进程消失。

[oracle@oracle11g ~]$ ps -ef | grep ora_n

oracle   6027 5931 0 05:22 pts/1   00:00:00 grep ora_n

4、配置DRCP关键参数

在dba_cpool_info视图中,我们可以查看到相应的参数信息。

SQL> desc dba_cpool_info;

Name                  Type         Nullable Default Comments

---------------------- ------------- -------- ------- -----------------------------------------------------------

CONNECTION_POOL       VARCHAR2(128) Y               Connection pool name

STATUS                VARCHAR2(16) Y               connection pool status

MINSIZE               NUMBER       Y       Minimum number of connections

MAXSIZE               NUMBER       Y        Maximum number of connections

INCRSIZE              NUMBER       Y      Increment number of connections

SESSION_CACHED_CURSORS NUMBER       Y               Session cached cursors

INACTIVITY_TIMEOUT    NUMBER       Y               Timeout for an idle session

MAX_THINK_TIME        NUMBER   Y    Max time for client to start activity on an acquired session

MAX_USE_SESSION       NUMBER       Y   Maximum life of a session based on usage

MAX_LIFETIME_SESSION  NUMBER       Y    Maximum life of a session based on time

NUM_CBROK             NUMBER       Y

MAXCONN_CBROK         NUMBER       Y

在DRCP连接池中,最常用的几个参数是minsize、maxsize和inactivity_timeout。

Minsize是说明DRCP连接池初始连接进程数量,如果请求数量超过这个minsize值,就会进行自动拓展。每次进行拓展的进程个数是incrsize参数。

Maxsize表示DRCP最大的拓展进程数量。当已经达到这个数量之后,DRCP连接池就不会获取到连接,被hange住。

DRCP对应的应用需求是“短会话、高并发”的应用场景。所以DRCP服务的连接必然是短时间交互。Inactivity_timeout参数就是设置这个timeout值。如果会话连接到这个连接之后,超过一定时间没有inactive交互,Oracle会自动将其断开。Server

Process被释放回连接池。

配置connection pool,我们可以使用dbms_connection_pool方法configure_pool。

SQL> exec dbms_connection_pool.configure_pool(minsize => 1,maxsize =>3,incrsize => 1,inactivity_timeout =>60);

PL/SQL procedure successfully completed

SQL> select connection_pool, status, minsize, maxsize, INACTIVITY_TIMEOUT from dba_cpool_info;

CONNECTION_POOL       STATUS       MINSIZE   MAXSIZE INACTIVITY_TIMEOUT

------------------------------ ------- --------- ---------- ------------------

SYS_DEFAULT_CONNECTION_POOL   INACTIVE        1         3                60

--启动连接池;

SQL> exec dbms_connection_pool.start_pool;

PL/SQL procedure successfully completed

--后台进程情况;

[oracle@oracle11g ~]$ ps -ef | grep ora_n

oracle   6035    1 3 05:22 ?       00:00:00 ora_n000_wilson

oracle   6039 5931 0 05:23 pts/1   00:00:00 grep ora_n

[oracle@oracle11g ~]$ ps -ef | grep ora_l

oracle   5689    1 0 05:02 ?       00:00:00 ora_lgwr_wilson

oracle   6037    1 0 05:22 ?       00:00:00 ora_l000_wilson

oracle   6041 5931 0 05:23 pts/1   00:00:00 grep ora_l

5、maxsize值突破实验

我们实验一下,当突破maxsize值的时候,会出现什么现象。我们启用sqlplus连接。

--第一会话

SQL> conn scott/tiger@wilsondrcp

Connected.

SQL> set time on;

05:23:45 SQL> select sid from v$mystat where rownum<2;

SID

----------

14

--第二会话

SQL> conn scott/tiger@wilsondrcp

Connected.

SQL> set time on

05:24:26 SQL> select sid from v$mystat where rownum<2;

SID

----------

22

启动了两个连接,此时连接池进程情况如下:

[oracle@oracle11g ~]$ ps -ef | grep ora_l

oracle   5689    1 0 05:03 ?       00:00:00 ora_lgwr_wilson

oracle   6037    1 0 05:22 ?       00:00:00 ora_l000_wilson

oracle   6052    1 0 05:23 ?       00:00:00 ora_l001_wilson

oracle   6054    1 0 05:23 ?       00:00:00 ora_l002_wilson

oracle   6118 5931 0 05:24 pts/1   00:00:00 grep ora_l

拓展到三个连接进程。但是,此时如果我们进行第三个连接连入,就不允许了。

SQL*Plus: Release 11.2.0.1.0 Production on Thu Mar 1 05:25:16 2012

Copyright (c) 1982, 2009, Oracle. All rights reserved.

SQL> conn scott/tiger@wilsondrcp

(数据库连接动作hange住)

这里,我们需要注意一个细节:连接池中存在三个server process,但是为什么第三个连接不能连入。注意:在DRCP连接池中,Oracle是要保留一个连接作为身份权限验证等操作使用的。不能将其分配出去。Maxsize我们设置为3,所以自然没有连接。

第三个连接hange住一段时间后,自动连入。

Connected.

SQL> select sid from v$mystat where rownum<2;

SID

----------

14

第三个会话连入。此时第一个和第二个会话是被强制断开。

--第一会话

05:24:37 SQL> select sid from v$mystat where rownum<2;

select sid from v$mystat where rownum<2

*

ERROR at line 1:

ORA-03113: end-of-file on communication channel

Process ID: 6052

Session ID: 14 Serial number: 5

--第二会话

05:24:47 SQL> select sid from v$mystat where rownum<2;

select sid from v$mystat where rownum<2

*

ERROR at line 1:

ORA-03113: end-of-file on communication channel

Process ID: 6054

Session ID: 22 Serial number: 17

此时,后台进程状态有释放连接。

[oracle@oracle11g ~]$ ps -ef | grep ora_l

oracle   5689    1 0 05:03 ?       00:00:00 ora_lgwr_wilson

oracle   6037    1 0 05:22 ?       00:00:00 ora_l000_wilson

oracle   6052    1 0 05:23 ?       00:00:00 ora_l001_wilson

oracle   6202 5931 0 05:28 pts/1   00:00:00 grep ora_l

附带,dbms_connection_pool的restore_defaults方法,可以将设置值置回。

SQL> exec dbms_connection_pool.stop_pool;

PL/SQL procedure successfully completed

SQL> select connection_pool, status, minsize, maxsize, INACTIVITY_TIMEOUT from dba_cpool_info;

CONNECTION_POOL               STATUS             MINSIZE   MAXSIZE INACTIVITY_TIMEOUT

------------------------------ ---------------- ---------- ---------- -----------------

SYS_DEFAULT_CONNECTION_POOL   INACTIVE                 1         3                60

SQL> exec dbms_connection_pool.restore_defaults;

PL/SQL procedure successfully completed

SQL> select connection_pool, status, minsize, maxsize, INACTIVITY_TIMEOUT from dba_cpool_info;

CONNECTION_POOL               STATUS             MINSIZE   MAXSIZE INACTIVITY_TIMEOUT

----------------------------- ---------------- ---------- ---------- ------------------

SYS_DEFAULT_CONNECTION_POOL   INACTIVE                 4        40               300

6、结论

DRCP是Oracle

11g中的新特性。借助DRCP,一些高并发、短会话应用可以获得数据库层面的高效连接池。笔者猜测Shared

Server模式就是DRCP的一种早期雏形。在现代企业级应用系统中,连接池是中间件的一个重要组件。当一些应用,如PHP不能提供有效连接池的时候,DRCP也许是不错的选择。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值