我们都知道,ORACLE 对外提供服务有 这么几种方式 THIN CONNECT ,OCI CONNECT ,DATALINK等数据连接方式。
1.THIN Connect
在单实例情况下,JDBC Connect String : jdbc:oracle:thin:10.11.1.243:1521/orcl
其中IP 10.11.1.243:1521 服务器IP地址和Lisener监听端口。
orcl 为数据服务名。
在RAC环境中,JDBC Connect String :
jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)
(HOST = 10.11.1.231)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)
(HOST = 10.11.1.232)(PORT = 1521))(LOAD_BALANCE = yes)(FAILOVER = ON)(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = racdb.its)(FAILOVER_MODE=(TYPE = SELECT)(METHOD = BASIC)(RETIRES = 20)(DELAY = 15))))
其中HOST为两个几点的Lisener的配置。FAILOVER关键字 为配置故障切换。Load_balance为负载均衡打开设置。
SERVICE_NAME 为RAC系统的数据库实例名。
以下为JAVA系统的代码:
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.ResultSet; 4 import java.sql.Statement; 5 6 public class Test { 7 8 public static void main(String arg[]) { 9 try { 10 Class.forName("oracle.jdbc.driver.OracleDriver"); 11 String url="jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = host1)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = host2)(PORT = 1521))(LOAD_BALANCE = yes)(FAILOVER = ON)(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = db.domain)(FAILOVER_MODE=(TYPE = SELECT)(METHOD = BASIC)(RETIRES = 20)(DELAY = 15))))"; 12 Connection c = DriverManager.getConnection(url,"aa","aa"); 13 Statement s = c.createStatement(); 14 ResultSet r = s.executeQuery("select 1 from dual"); 15 while(r.next()) { 16 System.out.println(r.getString(1)); 17 } 18 }catch(Exception e) { 19 System.out.println(e.toString()); 20 } 21 } 22 23 }
2.OCI 连接的配置 也就是通常的TNS 配置
单实例的情况:很简单 不多做累赘
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.11.1.243)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
RAC情况:
RACDB1 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = racdb1-vip.its)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = racdb)
(INSTANCE_NAME = racdb1)
)
)
RACDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = racdb1-vip.its)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = racdb2-vip.its)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = racdb)
)
)
LISTENERS_RACDB =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = racdb1-vip.its)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = racdb2-vip.its)(PORT = 1521))
)
EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
RACDB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = racdb1-vip.its)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = racdb2-vip.its)(PORT = 1521))
(LOAD_BALANCE = yes)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = racdb)
)
)
LISTENERS_RACDB =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = racdb1-vip.its)(PORT = 1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = racdb2-vip.its)(PORT = 1521))
)
EXTPROC_CONNECTION_DATA =
(DESCRIPTION = (ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)
RACDB2 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = racdb2-vip.its)(PORT = 1521)) (CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = racdb)
(INSTANCE_NAME = racdb2)
)
)
2.