Oracle RAC安裝完畢后,監(jiān)聽、tnsname.ora按照Oracle默認配置好后,在服務器端用sqlplus連接沒有問題,但是用客戶端連接,會出現有時候連不上的錯誤,解決方法: 1、修改監(jiān)聽文件,增加SID_LIST_LISTENER,兩臺機器都要修改,修改完畢后如下: LISTENER_ORCL1 = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.1)(PORT = 1521)) ) ) ) SID_LIST_LISTENER_ORCL1 = (SID_LIST = (SID_DESC = (ORACLE_HOME = /u01/app/oracle/product/11.1.0) (SID_NAME = ora1) ) ) 2、修改tnsname.ora,兩臺機器都要修改,例如第一臺機器: 用Oracle的netca配置的時候,只配置了一個監(jiān)聽LISTENERS_ORCL,手工增加另外兩個監(jiān)聽。 LISTENER_ORCL2 = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.2)(PORT = 1521)) ) LISTENER_ORCL1 = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.1)(PORT = 1521)) ) LISTENERS_ORCL = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.2)(PORT = 1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.1)(PORT = 1521)) ) ORCL1 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.1)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = ORCL) ) ) ORCL2 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.2)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = ORCL) ) ) ORCL = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.1)(PORT = 1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.2)(PORT = 1521)) (LOAD_BALANCE = yes) ) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = ORCL) ) ) 3、修改pfile,將兩個本地監(jiān)聽設置上。 可以先導出一份spfile,修改完畢后再導入pfile。增加以下三行: *.local_listener='LISTENER_ORCL' ora1.local_listener='LISTENER_ORCL1' ora2.local_listener='LISTENER_ORCL2' 另外,在啟動oralce時,出現ora-32006的錯誤,數據庫倒是起來能用。到網上搜索一通,說是oracle10g以后 將*.log_archive_start=TRUE 這個參數去掉了,把這行從spfile中刪除了,再啟動沒有報錯。是否真的不需要這個參數,還需要再確認。 4、oracle驅動 經測試發(fā)現,在oracle RAC環(huán)境下,如果down掉一個實例,應用出現以下錯誤: Io 異常: Software caused connection abort: socket write error org.apache.cocoon.ProcessingException: error on execute servlet:login: javax.servlet.ServletException: error on execute servlet:login cause: java.sql.SQLException: Io 異常: Software caused connection abort: socket write error 經反復測試,確定原因是oracle的驅動ojdbc14.jar沒有放到WEB-INF/lib下的原因。此啟動在公共lib下,必須COPY到當前工程下,才能實現fail-over的自動切換. 5、測試jdbc連接的程序: 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 * * @param args * @throws ClassNotFoundException */ 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", "XXXX"); conProps.put("internal_logon", "sysdba"); url = "jdbc:oracle:thin:@(description= (ADDRESS_LIST ="; url += "(address=(protocol=tcp)(host=192.168.0.1)(port=1521))"; // 服務器IP url += "(address=(protocol=tcp)(host=192.168.0.2)(port=1521))"; url += "(load_balance=yes))"; // 是否負載均衡 url += "(connect_data ="; url += "(server = dedicated)";// 專用服務器模式 可以去掉? url += "(service_name=orcl)"; // 服務名 //url += "(failover_mode ="; //url += "(type=session)"; //url += "(method=preconnect)"; //url += "(retries=5)"; //url += "(delay=15))"; url += " ))"; /** * LOAD_BALANCE = yes表示程序走sword連接的時候,將在下面的地址里面隨機的選擇一個,達到接點間連接均勻 * failover表示開啟TAF(Transparent Application Failover)特性,其中TYPE = * SESSION表示當一個連接好的會話 * 的實例發(fā)生故障,系統(tǒng)會自動將會話切換到其他可用的實例,前臺應用無須再度發(fā)起連接,但會話正在執(zhí)行的SQL 需要重新執(zhí)行,METHOD = * BASIC表示初始連接就連接一個接點,他還有個選項是preconnect,表示初始連接所有的 * 接點,在failover的時候可以很快就切過去,這個基本是資源浪費 RETRIES:重試次數 DELAY:重試間的延遲(以秒為單位) * 如果指定了RETRIES,DELAY默認為1 如果指定了DELAY,RETRIES默認為5 */ // String sql = "select * from oa_sfm_fb_fwgl t where t.pk_id = // '17233'"; 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_HOME/network/admin/listerer.ora那個文件,用netca自動生成時只有LISTENER_ORCL1 = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.1)(PORT = 1521)) ) ) ) |
|