cx_Oracle connect-具有负载平衡的连接字符串

时间:2019-01-28 12:14:19

标签: cx-oracle

我有格式为

的连接字符串
jdbc:oracle:thin:@(DESCRIPTION = (LOAD_BALANCE=ON) 
 (ADDRESS = (PROTOCOL = tcp)(HOST = aaa)(PORT = 1531))
 (ADDRESS = (PROTOCOL = tcp)(HOST = bbb)(PORT = 1526))
 (ADDRESS = (PROTOCOL = tcp)(HOST = ccc)(PORT = 1526))
 (ADDRESS = (PROTOCOL = tcp)(HOST = ddd)(PORT = 1526))
 (CONNECT_DATA = (SERVER=dedicated)(SERVICE_NAME=a.b.org))
)

如何使用cx_Oracle connect

connection = cx_Oracle.connect( .... ) 

使用上述指定格式的连接字符串?

1 个答案:

答案 0 :(得分:1)

实际上很简单。您可以执行以下操作:

dsn = """(DESCRIPTION = (LOAD_BALANCE=ON) 
 (ADDRESS = (PROTOCOL = tcp)(HOST = aaa)(PORT = 1531))
 (ADDRESS = (PROTOCOL = tcp)(HOST = bbb)(PORT = 1526))
 (ADDRESS = (PROTOCOL = tcp)(HOST = ccc)(PORT = 1526))
 (ADDRESS = (PROTOCOL = tcp)(HOST = ddd)(PORT = 1526))
 (CONNECT_DATA = (SERVER=dedicated)(SERVICE_NAME=a.b.org))
)

"""
cx_Oracle.connect("user", "password", dsn)

有效地,您可以在tnsnames.ora文件中找到的任何连接字符串也可以作为dsn参数直接传递给cx_Oracle.connect。