上次记录了Java使用thin方式连接Oracle集群的Uri写法,参考网上资料有连接不稳定的说法,本人尚未做测试,但是查资料发现OCI(Oracle Call Interface)的方式更为稳定,最后决定使用这种方式。按照以下步骤完成环境配置:
1.安装Oracle client,根据提示或者直接在安装目录创建配置文件:tnsnames.ora,内容可参考下面内容:
DBCENTER =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = db1_vip)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = db2_vip)(PORT = 1521))
(LOAD_BALANCE = yes)
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = dbcenter)
)
)
DBCENTER1 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = db1_vip)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = dbcenter)
(INSTANCE_NAME = dbcenter1)
)
)
DBCENTER2 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = db2_vip)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = dbcenter)
(INSTANCE_NAME = dbcenter2)
)
)2.在C:/WINDOWS/system32/drivers/etc/hosts 文件增加IP配置。
3.在代码中使用jdbc:oracle:oci:@dbcenter的方式连接Oracle集群。
经测试连接正常。