单个JDBC OracleDataSource / HikariCP与主/备份数据库

时间:2017-04-21 18:38:18

标签: java spring oracle jdbc hikaricp

我正在尝试设置一个引用我们的主数据库的连接池,直到说不健康,然后池故障转移,填满我们的备份。到目前为止,我一直在利用应用程序服务器的JNDI数据源的未记录功能,这样我就可以指定2个JDBC连接URL字符串:

jdbc:oracle:thin:@primary:1521:DB|jdbc:oracle:thin:@backup:1521:DB

我有以下代码,毫无疑问几个月之前部分Hikari / Spring文档中的部分内容。

@Bean(name = "dataSource")
public DataSource dataSource() throws SQLException {
    String userName = "user";
    String password = "pass";
    String server = "primary";
    String database = "DB";

    OracleDataSource ods = new OracleDataSource();
    ods.setServerName(server);
    ods.setDatabaseName(database);
    ods.setNetworkProtocol("tcp");
    ods.setUser(userName);
    ods.setPassword(password);
    ods.setPortNumber(1521);
    ods.setDriverType("thin");

    HikariConfig hkConfig = new HikariConfig();
    hkConfig.setDataSource(ods);
    hkConfig.setDataSourceClassName("oracle.jdbc.pool.OracleDataSource");
    hkConfig.setPoolName("springHikariRECPool");
    hkConfig.setMaximumPoolSize(15);
    hkConfig.setMinimumIdle(3);
    hkConfig.setMaxLifetime(1800000); // 30 minutes

    return new HikariDataSource(hkConfig);
}

我的Google-Fu让我失望了。有没有人对如何实现故障转移功能有任何想法?

编辑 - 重新。 @M。 Deinum“删除OracleDataSource的构造,只需在HikariConfig上设置URL。”

HikariConfig hkConfig = new HikariConfig();
hkConfig.setUsername(userName);
hkConfig.setPassword(password);
hkConfig.setJdbcUrl("jdbc:oracle:thin:@primary:1521:DB|jdbc:oracle:thin:@backup:1521:DB");
hkConfig.setDataSourceClassName("oracle.jdbc.pool.OracleDataSource");
hkConfig.setPoolName("springHikariRECPool");
hkConfig.setMaximumPoolSize(15);
hkConfig.setMinimumIdle(3);
hkConfig.setMaxLifetime(1800000);

不幸的是,这会产生一个相当长的堆栈,其基础是:

Caused by: java.sql.SQLException: Invalid Oracle URL specified: OracleDataSource.makeURL
    at oracle.jdbc.pool.OracleDataSource.makeURL(OracleDataSource.java:1277)
    at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:185)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:356)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:199)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:444)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:515)

调查here - Hikaricp Oracle connection issuehere - Invalid Oracle URL specified: OracleDataSource.makeURL会导致我添加一些其他属性。

hkConfig.addDataSourceProperty("portNumber", "1521");
hkConfig.addDataSourceProperty("driverType", "thin");

现在爆炸了:

Caused by: java.net.UnknownHostException: null: Name or service not known
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
    at java.net.InetAddress.getAllByName(InetAddress.java:1192)
    at java.net.InetAddress.getAllByName(InetAddress.java:1126)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:117)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:370)

不再引用JDBC URL,它会出现。 。 。并且,已确认 - 我从URL中取出了备份连接字符串,并使用标准的单服务器连接达到了相同的异常。因此,ODS要求按原始配置(或使用“属性”模拟)。

作为此方法的最后一次喘息,我尝试将serverName属性设置为“primary | standby”,并且正如预期的那样,它也会爆炸:

Caused by: java.net.UnknownHostException: primary|backup: Name or service not known
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
    at java.net.InetAddress.getAllByName(InetAddress.java:1192)
    at java.net.InetAddress.getAllByName(InetAddress.java:1126)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:117)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:411)
    ... 56 more

到目前为止,我没有注意到我正在使用ojdbc7.jar

1 个答案:

答案 0 :(得分:1)

使用标准方式。支持DataGuard,故障转移,RAC是Oracle JDBC驱动程序的本机功能。

首先使用tnsnames.ora,如此处所述"How to connect JDBC to tns oracle"

第二次使用tnsnames.ora中的多个主机:

DB =
(DESCRIPTION=
(ADDRESS_LIST=
  (LOAD_BALANCE=off)
  (FAILOVER=ON)
  (ADDRESS=(PROTOCOL=TCP)( HOST=primary)(PORT=1521))
  (ADDRESS=(PROTOCOL=TCP)( HOST=backup)(PORT=1521)))
(CONNECT_DATA=(SERVICE_NAME=DB)))

Oracle JDBC驱动程序将连接到主机,其中数据库为“OPEN”,并且存在名为“DB”的服务。

PS:您也可以将整个tns连接字符串直接作为参数传递给jdbc驱动程序。

url="jdbc:oracle:thin:@(DESCRIPTION=
  (LOAD_BALANCE=on)
   (ADDRESS_LIST=
   (ADDRESS=(PROTOCOL=TCP)(HOST=primary)(PORT=1521))
  (ADDRESS=(PROTOCOL=TCP)(HOST=secondary)(PORT=1521)))
  (CONNECT_DATA=(SERVICE_NAME=DB)))"