这次的客户是双机访问操作数据库,一台实例名是orcl1,一台是orcl2。访问数据库服务名是orcl,之前我也搜了些资料,最后的想法是写了一个测试程序来测试JDBC链接ORACLE RAC的连接串。代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class RacTest {
/**
* 测试RAC
*
*/
public static void main(String[] args) throws ClassNotFoundException {
String url;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Properties conProps = new Properties();
conProps.put("user", "sys");
conProps.put("password", "sys");
conProps.put("internal_logon", "sysdba");
// String username = "sys";
// String password = "oracle";
url = "jdbc:oracle:thin:@(description= (ADDRESS_LIST =";
url += "(address=(protocol=tcp)(host=10.37.27.111)(port=1521))";
url += "(address=(protocol=tcp)(host=10.37.27.112)(port=1521))";
url += "(load_balance=yes))";
url += "(connect_data =";
url += "(server = dedicated)";
url += "(service_name=orcl)";
url += "(failover_mode =";
url += "(type=session)";
url += "(method=basic)";
url += "(retries=5)";
url += "(delay=15))";
url += " ))";
String sql = "select * from v$instance";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, conProps);
// conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
System.out.print(rs.getString("INSTANCE_NAME"));
}
rs.close();
rs = null;
stmt.close();
stmt = null;
conn.close();
conn = null;
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
conn = null;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
}
Oracle RAC JDBC 连接测试
本文介绍了一种使用Java JDBC连接Oracle Real Application Clusters (RAC)的方法,并提供了一个示例程序来验证连接的有效性和负载均衡特性。文中详细解释了连接字符串各个参数的意义,包括地址配置、负载均衡设置和服务名指定等。
330

被折叠的 条评论
为什么被折叠?



