JDBC 连接RAC负载均衡URL简单配置总结
在通过JDBC连接RAC的时候,我由于各种原因会出现以下问题。
问题一:通过JDBC连接如果使用ip连接方式
例:
jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =
(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.36.6)(PORT=1521))
(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.36.7)(PORT=1521)))
(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=gfront )))
Host={ip地址的方式}在是无法连接到RAC设备的。
会抛出一个 NL Exception 异常(NL异常是指你的jdbc url配置不正确才会出现的异常。)
问题二:在配置过程中如果不了解jdbc连接方式的会省去jdbc:oracle:thin:@ url前面的当然这只是我在操作中的疏忽。且"Host="后面赋值必须为主机的名称。ip地址是无法连接的。
正确的RAC配置如下:
jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =
(ADDRESS=(PROTOCOL=TCP)(HOST=vip1)(PORT=1521))
(ADDRESS=(PROTOCOL=TCP)(HOST=vip2)(PORT=1521)))
(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=gfront )))
下面是一段Demo代码:
RAC配置环境相关信息
192.168.36.6 vip1
192.168.36.7 vip2
数据库的端口号为:1521
实例名:gfront1 和gfront2
数据库的端口号为:1521
用户:system
密码:111111
package com.rac.jdbc.url;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class RacUrl {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String connStr = "jdbc:oracle:thin:@(DESCRIPTION ="
+ "(ADDRESS_LIST ="
+ "(ADDRESS=(PROTOCOL=TCP)(HOST=vip1)(PORT=1521))"
+ "(ADDRESS=(PROTOCOL=TCP)(HOST=vip2)(PORT=1521))"
+ ")" + "(CONNECT_DATA=" + "(SERVER=DEDICATED)"
+ "(SERVICE_NAME=gfront )" + ")" + ")";
int isDBA = 1;
String jdbc = "oracle.jdbc.driver.OracleDriver";
Class.forName(jdbc);
Connection conn = null;
if (isDBA == 1) {
Properties props = new Properties();
props.put("user", "system");
props.put("password", "111111");
props.put("internal_logon", "sysdba");
conn = DriverManager.getConnection(connStr, props);
System.out.println(conn == null?"失败":"成功");
} else {
conn = DriverManager.getConnection(connStr, "system", "111111");
System.out.println(conn == null?"失败":"成功");
}
}
}