Thursday, March 26, 2015

JDBC URL to connect RAC database or How add multiple hosts in jdbc url

Today I will share new problem which I have solved.

In my last project I was connected to Oracle database (RAC) database using multiple hosts and I want to share oracle connection string which uses multiple hosts.

So while you are connecting from oracle clients (as PL/SQL developer) it is easy to define multiple hosts in tnsnames.ora





dbnameTest = 
 (DESCRIPTION =
  (ADDRESS = (PROTOCOL = TCP)(HOST = 1.1.1.111)(PORT = 1521))
  (ADDRESS = (PROTOCOL = TCP)(HOST = 1.1.1.222)(PORT = 1521))
  (LOAD_BALANCE = yes)
  (CONNECT_DATA =
    (SERVER = DEDICATED)
    (SERVICE_NAME = testDB)
  )
 )
But how we can define our connection string in Java to connect such database (which uses multiple hosts)?
It is easy - just copy the tnsentries from your tnsnames.ora to connection string as below
jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=1.1.1.111)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=1.1.1.222)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=testDB)))

If you want to connect multiple hosts from mysql u can use below connection string:
jdbc:mysql://1.1.1.111:3306,1.1.1.222:6306/mysql

That is all :)

No comments:

Post a Comment