在 HikariCP 中使用来自 TNSNAMES.ORA 的设置

Using settings from TNSNAMES.ORA in HikariCP

我正在尝试为我的应用程序连接池迁移到 HikariCP。目前,我想连接到 Oracle 数据库。

这是我使用的代码:

        HikariConfig hc = new HikariConfig();
        hc.setUsername(username);
        hc.setPassword(password);
        hc.setDataSourceClassName("oracle.jdbc.pool.OracleDataSource");
        hc.addDataSourceProperty("serverName", TNS);
        hc.addDataSourceProperty("portNumber", 1521);
        hc.addDataSourceProperty("driverType", "thin");


        HikariDataSource hds = new HikariDataSource(hc);
        hds.setPoolName("Pool::"+username+"@"+dbName);

TNS 变量来自我的配置,并引用服务器上 TNSNAMES.ora 文件中的设置。

SERVER_1 =
    (DESCRPTION = 
        (ADDRESS_LIST = 
                (ADDRESS = (PROTOCOL = TCP)(HOST = server01.intranet)(PORT = 1521))
        )
        (CONNECTION_DATA = 
                (SERVICE_NAME = SERVER1)
        )
    )

代码失败并显示以下堆栈跟踪:

INFO  HikariPool-1 - Starting...
ERROR HikariPool-1 - Exception during pool initialization.
java.sql.SQLRecoverableException: Erreur d'E/S: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:743)
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:566)
    at oracle.jdbc.pool.OracleDataSource.getPhysicalConnection(OracleDataSource.java:317)
    at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:241)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:443)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:514)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:111)
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:72)
    at com.test.commonlib.config.DbConfigReader.getDataSource(DbConfigReader.java:105)
    at com.test.commonlib.config.DbConfigReader.getDataSource(DbConfigReader.java:31)
    at com.test.module1.Module1.getConnectionModule1(Module1.java:258)
    at com.test.module1.fileinjector.fd.loadConf(fd.java:583)
    at com.test.module1.fileinjector.fd.<init>(fd.java:78)
    at com.test.module1.fileinjector.fd.getInstance(fd.java:84)
    at com.test.module1.Module1.init(Module1.java:78)
    at com.test.module1.scripts.TestPerso.TestPerso.testModule1(TestPerso.java:2009)
    at com.test.module1.scripts.TestPerso.TestPerso.main(TestPerso.java:145)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:470)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:506)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:595)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:230)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1452)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:496)
    ... 20 more
Caused by: java.net.UnknownHostException: SERVER_1
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress.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:120)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:159)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:428)
    ... 25 more
Exception in thread "main" com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Erreur d'E/S: The Network Adapter could not establish the connection
    at com.zaxxer.hikari.pool.HikariPool.throwPoolInitializationException(HikariPool.java:543)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:535)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:111)
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:72)
    at com.test.commonlib.config.DbConfigReader.getDataSource(DbConfigReader.java:105)
    at com.test.commonlib.config.DbConfigReader.getDataSource(DbConfigReader.java:31)
    at com.test.module1.Module1.getConnectionModule1(Module1.java:258)
    at com.test.module1.fileinjector.fd.loadConf(fd.java:583)
    at com.test.module1.fileinjector.fd.<init>(fd.java:78)
    at com.test.module1.fileinjector.fd.getInstance(fd.java:84)
    at com.test.module1.Module1.init(Module1.java:78)
    at com.test.module1.scripts.TestPerso.TestPerso.testModule1(TestPerso.java:2009)
    at com.test.module1.scripts.TestPerso.TestPerso.main(TestPerso.java:145)
Caused by: java.sql.SQLRecoverableException: Erreur d'E/S: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:743)
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:566)
    at oracle.jdbc.pool.OracleDataSource.getPhysicalConnection(OracleDataSource.java:317)
    at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:241)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:443)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:514)
    ... 11 more
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:470)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:506)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:595)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:230)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1452)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:496)
    ... 20 more
Caused by: java.net.UnknownHostException: SERVER_1
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress.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:120)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:159)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:428)
    ... 25 more

有没有人幸运地使用 TNS NAME 在 Oracle 上运行?

您可以使用 JDBC URL 代替:

dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setJdbcUrl("jdbc:oracle:thin:@server01.intranet:1521/SERVER1")

设置 "oracle.net.tns_admin" 系统 属性 以指定 TNSNAMES.ORA 文件的目录位置。

System.setProperty("oracle.net.tns_admin", "C:\Development\TNS_ADMIN");

如果您设置了TNS_ADMIN系统变量,您可以按以下方式设置

System.setProperty("oracle.net.tns_admin", System.getenv("TNS_ADMIN"));