环境介绍

双机操作系统:AIX 6.1数据库版本:oracle 11g R1 64bit主机名称:fly007

1、今天在数据库创建了5个dblink,在同一个会话中进行dblink查询测试时,当执行到第5条查询语句时,报ORA-02020 too many database links in use错误,注意,是通过dbink连接不同的数据库,如果是连接相同的数据库,是不会报该错误的,报错信息如下所示:

$hostnamefly007$ cat db_link.sqlselect sysdate from dual@to_fly1;select sysdate from dual@to_fly2;select sysdate from dual@to_fly3;select sysdate from dual@to_fly4;select sysdate from dual@to_fly5;$ sqlplus /nologSQL> conn /as sysdbaSQL> set echo onSQL> @db_link.sqlSQL> select sysdate from dual@to_fly1;SYSDATE------------------27-DEC-13SQL> select sysdate from dual@to_fly2;SYSDATE------------------27-DEC-13SQL> select sysdate from dual@to_fly3;SYSDATE------------------27-DEC-13SQL> select sysdate from dual@to_fly4;SYSDATE------------------27-DEC-13SQL> select sysdate from dual@to_fly5;select sysdate from dual@to_fly5ERROR at line 1:ORA-02020: too many database links in use

     2、使用oerr查看该错误信息:

$ oerr ora 202002020, 00000, "too many database links in use"// *Cause:  The current session has exceeded the INIT.ORA open_links maximum.// *Action: Increase the open_links limit, or free up some open links by// committing or rolling back the transaction and canceling open// cursors that reference remote databases.

    3、根据以上提示,查看open_links参数,默认为4

SQL> show parameter open_linksNAME                                 TYPE        VALUE------------------------------------ ----------- ------------------------------open_links                           integer     4open_links_per_instance              integer     4

     4、查看官方文档

OPEN_LINKSProperty    DescriptionParameter type  IntegerDefault value       4Modifiable    No  //即不可以在线更改该参数,需要重启数据库才能生效Range of values 0 to 255OPEN_LINKS specifies the maximum number of concurrent open connections to remote databases in one session.If you set OPEN_LINKS to 0, then no distributed transactions are allowed.

    open_links 指定在一次会话中同时打开的与远程数据库的连接的最大数量。该值应等于或超过 SQL 语句中引用的数据库的数量,这样才能打开所有数据库以便执行该语句。 取值范围为:0 - 255 (如果为 0,不允许分布式事务处理)

OPEN_LINKS_PER_INSTANCEProperty    DescriptionParameter type  IntegerDefault value       4Modifiable      NoRange of values  0 to 4294967295 (4 GB -1)Real Application Clusters,Multiple instances can have different values.OPEN_LINKS_PER_INSTANCE specifies the maximum number of migratable open connections globally for each database instance.XA transactions use migratable open connections so that the connections are cached after a transaction is committed

   open_links_per_instance指定 XA 应用程序中可移植的打开连接的最大数量。XA 事务处理使用可移植的打开的连接,以便在提交一个事务处理后能将这些连接高速缓存

 5、Do not set open_links too high, it is better for the application to close database links when

 no longer in use than to change the parameter to a high number

         不建议将open_links参数改的太高,我们建议在不使用dblink的时候,应用程序应关闭dblink

     6、每次用dblink连接远程数据库,即使只执行了select语句,也会启动一个事物; 所以必须提交!提交后,有两种方式可以关闭dblink,一种是退出该会话,这是隐式的方式,另外一种显示的关闭dblink:

alter session close database link 要关闭的dblink名称;

   7、如果不关闭dblink,则连接的资源不会被释放,我们可以在远程数据库,执行如下语句,我们发现该远程会话会一直保留直到原会话退出或者当我们显示的关闭dblink后,才会退出:

select * from v$session t where t.machine='fly007';

   这里要注意,需要先执行commit后,然后才能执行alter session close database link  dblink_name成功,否则会报:ORA-02080: database link is in use

         下面举例说明:

        1、当使用dblink查询到第4条语句后,进行提交,执行到第5条查询语句,不再报错:

$hostnamefly007$ cat db_link.sqlselect sysdate from dual@to_fly1;select sysdate from dual@to_fly2;select sysdate from dual@to_fly3;select sysdate from dual@to_fly4;commit;select sysdate from dual@to_fly5;$ sqlplus /nologSQL> conn /as sysdbaSQL> set echo onSQL> @db_link.sqlSQL> select sysdate from dual@to_fly1;SYSDATE------------------27-DEC-13SQL> select sysdate from dual@to_fly2;SYSDATE------------------27-DEC-13SQL> select sysdate from dual@to_fly3;SYSDATE------------------27-DEC-13SQL> select sysdate from dual@to_fly4;SYSDATE------------------27-DEC-13SQL> commit;Commit complete.SQL> select sysdate from dual@to_fly5;SYSDATE------------------27-DEC-13

        2、查询v$dblink视图

SQL> col db_link format a15;SQL> col IN_TRANSACTION format a15SQL> select db_link,in_transaction from v$dblink;DB_LINK         IN_TRANSACTION--------------- ---------------to_fly1               NOto_fly2               NOto_fly3               NOto_fly5               YES

       3、关闭dblink,再查看v$dblink,返回空,所有dblink资源释放

SQL> alter session close database link to_fly1;Session altered.SQL> alter session close database link to_fly2;Session altered.SQL> alter session close database link to_fly3;Session altered.SQL> alter session close database link to_fly5; //to_fly5的事务还处于ACTIVE状态,需要先提交,才能关闭dblinkERROR:ORA-02080: database link is in useSQL> commit;Commit complete.SQL> alter session close database link to_fly5;Session altered.SQL> select * from v$dblink;no rows selected

       4、查看数据库中总共有多少个dblink

SQL> select count(*) from dba_db_links;COUNT(*)----------5

       5、If you are not sure how many database links are opened up concurrently by your session's database 

application, you can query v$dblink.

SQL> col in_transaction format a15SQL> select in_transaction, count(*) from v$dblink group by in_transaction;IN_   COUNT(*)--- ----------YES          1

     6、修改open_links参数

SQL> alter system set open_links=10 scope=spfile;