在Oracle 11g Data Guard中实现Connect Time Failover & Transparent Application Failover(TAF)

背景介绍:

在switchover或failover时主库进行切换后,客户端获得自己重连主库的能力。


环境修改:
1.修改$ORACLE_HOME/network/admin/tnsnames.ora

PRIOCM=
  (DESCRIPTION =
    (ADDRESS_LIST=
      (ADDRESS = (PROTOCOL = TCP)(HOST = ocm1.example.com)(PORT = 1521))  --主库
      (ADDRESS = (PROTOCOL = TCP)(HOST = ocm2.example.com)(PORT = 1521))  --备库
    )
    (CONNECT_DATA =
      (SERVICE_NAME = priocm)  --指定service名字
    )
  )

2.在主库中创建并启动service

begin
 dbms_service.create_service('priocm','priocm');  --service_name,network_name
end;
/
begin
 DBMS_SERVICE.START_SERVICE('priocm');
end;
/


3.在主库中创建启动触发器,当database_role为非主库时关闭service

create or replace trigger priocmtrigg after startup on database
declare
 v_role varchar(30);
begin
 select database_role into v_role from v$database;
 if v_role = 'PRIMARY' then
 DBMS_SERVICE.START_SERVICE('priocm');
 else
 DBMS_SERVICE.STOP_SERVICE('priocm');
 end if;
end;
/

4.修改servicefailover参数

begin
 dbms_service.modify_service
 ('priocm',
 FAILOVER_METHOD => 'BASIC', --BASIC:是指在感知到节点故障时才创建到其他实例的连接。PRECONNECT: 是在最初建立连接时就同时建立到所有实例的连接,当发生故障时,立刻就可以切换到其他链路
 FAILOVER_TYPE => 'SELECT',  --session和select,这2种方式对于未提交的事务都会自动回滚,区别在于对select 语句的处理,对于select,用户正在执行的select语句会被转移到新的实例上,在新的节点上继续返回后续结果集,而已经返回的记录集则抛弃。
 FAILOVER_RETRIES => 200,    --重试次数
 FAILOVER_DELAY => 1);	     --重试间隔时间
end;
/

切换实验:

模拟在客户端通过service(priocm)连接

[oracle@ocm1 admin]$ sqlplus sys/oracle@priocm as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Sun Jun 25 16:07:33 2017

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SYS@priocm> select db_unique_name,name,database_role,switchover_status from v$database;  --连接到当前主库

DB_UNIQUE_NAME		       NAME	 DATABASE_ROLE	  SWITCHOVER_STATUS
------------------------------ --------- ---------------- --------------------
aux			       PROD3	 PRIMARY	  TO STANDBY

----------------------------------------------------------------------------------------------------------------
省略switchover过程,http://blog.csdn.net/u013169075/article/details/73555409
----------------------------------------------------------------------------------------------------------------

switchover后,再次模拟在客户端通过service(priocm)连接

SYS@priocm> /
select db_unique_name,name,database_role,switchover_status from v$database
*
ERROR at line 1:
ORA-03113: end-of-file on communication channel   --尝试操作主库抛出错误
Process ID: 4810
Session ID: 28 Serial number: 7


ERROR:
ORA-03114: not connected to ORACLE


SYS@priocm> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
--尝试重连主库
[oracle@ocm1 ~]$ sqlplus sys/oracle@priocm as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Sun Jun 25 16:49:45 2017

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SYS@priocm> select db_unique_name,name,database_role,switchover_status from v$database;  --已经连接到新主库

DB_UNIQUE_NAME		       NAME	 DATABASE_ROLE	  SWITCHOVER_STATUS
------------------------------ --------- ---------------- --------------------
PROD3			       PROD3	 PRIMARY	  FAILED DESTINATION


参考资料:
http://www.oracle.com/technetwork/cn/articles/database-performance/oracle-rac-connection-mgmt-1650424-zhs.html
https://uhesse.com/2009/08/19/connect-time-failover-transparent-application-failover-for-data-guard/

你可能感兴趣的:(Oracle,Data,Guard)