2015年4月20日 星期一

Java - Oracle 連線使用 tnsname

Java - Oracle 連線使用 tnsname

web server 必需安裝 oracle client,然後設定好 tnsnames.ora
<tnsname> =
 (DESCRIPTION =
   (ADDRESS_LIST =
     (ADDRESS = (PROTOCOL = TCP)(Host = <hostname>)(Port = <port>))
   )
 (CONNECT_DATA =
   (SERVICE_NAME = <sid>)
 )
)

connection string 中的 url 由原本的 jdbc:oracle:thin:@<hostname>:<port>:<sid>
改成 jdbc:oracle:thin:@<tnsname>

然後要讓程式知道 tnsnames.ora 的位置,有二個方法:
1. java 程式在資料庫 open session 前設定系統變數
System.setProperty("oracle.net.tns_admin", "D:/oracle/product/10.2.0/client_1/NETWORK/ADMIN");

2. 將設定加入 tomcat7w 或是啟動 sh 中
-Doracle.net.tns_admin=D:/oracle/product/10.2.0/client_1/NETWORK/ADMIN