简体   繁体   English

使用Java连接到Oracle集群

[英]Connect to an Oracle cluster in Java

We have a pair of Oracle servers which are set up as nodes in a cluster (apologies if my terminology is way off). 我们有一对Oracle服务器,它们被设置为集群中的节点(如果我的术语已经过时,我会道歉)。 In my tnsnames.ora file, we have an entry that looks like 在我的tnsnames.ora文件中,我们有一个看起来像的条目

EXAMPLE.GOV =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 1.2.3.4)(PORT = 1521))
    (ADDRESS = (PROTOCOL = TCP)(HOST = 1.2.3.5)(PORT = 1521))
    (LOAD_BALANCE = yes)
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = example.gov)
    )
  )

and this works when I connect with programs which use the tnsnames.ora file. 当我连接使用tnsnames.ora文件的程序时,这tnsnames.ora However, I also have a Java program which uses the oracle.jdbc.pool.OracleDataSource class to establish a connection 但是,我也有一个Java程序,它使用oracle.jdbc.pool.OracleDataSource类来建立连接

public static Connection connect() throws Exception {
    OracleDataSource ods = new OracleDataSource();
    ods.setDriverType("thin");
    ods.setServerName("1.2.3.4");
    ods.setDatabaseName("example");
    ods.setPortNumber(1521);
    ods.setUser("scott");
    ods.setPassword("tiger");
    return ods.getConnection();
}

which just connects to one of the nodes directly. 它只是直接连接到其中一个节点。 I'd like to instead use the load-balancing tnsnames.ora approach, where it uses load balancing or whatever to connect to either one of the nodes, so that if one of them is down then it'll automatically connect to the other. 我想改为使用负载均衡的tnsnames.ora方法,它使用负载均衡或任何连接到其中一个节点的方式,这样如果其中一个节点关闭,那么它将自动连接到另一个节点。

Since I only have two nodes, I could easily just try opening a connection to the first node, then if that doesn't work open a connection to the second one. 由于我只有两个节点,我可以轻松地尝试打开与第一个节点的连接,然后如果不起作用则打开与第二个节点的连接。 However, I'm wondering if there's a more correct way to do this. 但是,我想知道是否有更正确的方法来做到这一点。

I see that there's a setTNSEntryName parameter, but since my tnsnames.ora is in a nonstandard place, I'd need to set the TNS_ADMIN environment variable, which I'm not sure that I can even do from within Java. 我看到有一个setTNSEntryName参数,但由于我的tnsnames.ora位于非标准位置,我需要设置TNS_ADMIN环境变量,我不确定我甚至可以在Java中做什么。 Nor am I certain that this would work in any case. 我也不确定这在任何情况下都会有效。

Does anyone know how to connect to a cluster of Oracle nodes from a Java program? 有谁知道如何从Java程序连接到Oracle节点集群?

For Oracle JDBC thin driver I think this could work as the connection url: 对于Oracle JDBC瘦驱动程序,我认为这可以用作连接URL:

jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=on)
(ADDRESS=(PROTOCOL=TCP)(HOST=1.2.3.4) (PORT=1521))
(ADDRESS=(PROTOCOL=TCP)(HOST=1.2.3.5) (PORT=1521))
(CONNECT_DATA=(SERVICE_NAME=example.gov)))

Use the method setUrl to set the url. 使用setUrl方法设置url。 If URL is set all other properties like databasename, servername, portNumber, network protocol, tnsentry, and driver type will be ignored. 如果设置了URL,则将忽略所有其他属性,如databasename,servername,portNumber,network protocol,tnsentry和driver type。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM