JDBC连接oracle RAC数据库配置

oracle数据库经常会有RAC架构的,多个节点管理一个数据库或者是备份机这些情况,那么要在java代码中连接数据库怎么办呢?jdbc普通的连接方式已经不太符合要求了,主要就是url的区别,小小总结一下。
配置过程,找到tnsname.ora文件,位置在\Oracle\Instant Client\network\admin下面,查看对应的sid的description,为
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = werprac3-vip.ttmtech.com.hk)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = werprac1-vip.ttmtech.com.hk)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = werprac2-vip.ttmtech.com.hk)(PORT = 1521))
)
(LOAD_BALANCE = YES)
(FAILOVER = ON)
(CONNECT_DATA =
(SERVICE_NAME = PRD1)
)
)

java连接RAC:

public static Connection getConnForOracleLive(String username,String password) throws Exception{

//驱动名
String driverName = "oracle.jdbc.driver.OracleDriver";
//连接服务器和数据库名
String url = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=werprac3-vip.ttmtech.com.hk)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=172.30.254.36)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=werprac1-vip.ttmtech.com.hk)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=172.30.254.38)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=werprac2-vip.ttmtech.com.hk)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=172.30.254.37)(PORT=1521)))(LOAD_BALANCE=YES)(FAILOVER=ON)(CONNECT_DATA=(SERVICE_NAME=PRD1)))";

Connection conn = null;
//加载驱动
Class.forName(driverName);
//获取连接
conn = DriverManager.getConnection(url, username, password);
System.out.println("获取oracleLive连接成功!");
return conn;
}
}
展开阅读全文

没有更多推荐了,返回首页