java.sql.SQLException网络适配器无法建立连接

时间:2017-09-16 14:20:35

标签: oracle hibernate spring-boot jdbc

我有一个使用以下ojdbc驱动程序的java / hibernate / jpa应用程序(由于历史原因,必须连接到Oracle 10 DB)。我收到以下错误消息

  

2017-09-16 13:36:02 WARN o.s.b.a.orm.jpa.DatabaseLookup - 无法   从datasource确定jdbc url   org.springframework.jdbc.support.MetaDataAccessException:不能   get Connection用于提取元数据;嵌套异常是   org.springframework.jdbc.CannotGetJdbcConnectionException:不能   获取JDBC连接;嵌套异常是java.sql.SQLException:Io   例外:网络适配器无法建立连接

每当我使用主机的内部定义(持有DB - 而不是127.0.0.1环回地址)时,它都有效。每当我使用外部定义的主机名时,它都不起作用。

使用 application.properties文件时,APP的以下连接字符串 DOES WORK

  

spring.datasource.url = JDBC:预言:瘦:@的 internalhost :1527:DSM   (例如:192.x.x.x - 192.88.33.22)请注意,我不使用127.0.0.1   (环回地址)

使用 application.properties文件时,APP的以下连接字符串无法正常工作

  

spring.datasource.url = JDBC:预言:瘦:@的 externalhost.com :1527:DSM   (例如:99.22.123.44)

我在OS LEVEL运行以下内容 - 此测试正在工作

  

SQLPLUS   斯科特/老虎@(DESCRIPTION =(ADDRESS =(PROTOCOL = TCP)(HOST = externalhost.com)(PORT = 1527))(CONNECT_DATA =(SERVICE_NAME = DSM)))

我运行了以下Java测试(在OS LEVEL上)并且它正在工作

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class OracleJDBCExample {
    public static void main(String[] argv) {
        System.out.println("-------- Oracle JDBC Connection Testing ------");
        try {
              Class.forName("oracle.jdbc.driver.OracleDriver");
            /*Class.forName("oracle.jdbc.OracleDriver");*/

        } catch (ClassNotFoundException e) {
            System.out.println("Where is your Oracle JDBC Driver?");
            e.printStackTrace();
            return;
        }
        System.out.println("Oracle JDBC Driver Registered!");
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@externalhost.com:1527:DSM", "scott", "tiger");              
        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;
        }
        if (connection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
    }

如何解决此问题? TIA

application.properties文件

# Oracle settings
spring.datasource.url=jdbc\:oracle\:thin\:@externalhost.com\:1527\:DSM  (ex: 99.22.123.44)
spring.datasource.username=scott
spring.datasource.password=tiger
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

#thymeleaf
spring.thymeleaf.cache=false

收到错误

 :: Spring Boot ::        (v1.5.4.RELEASE)

2017-09-16 13:35:34 DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletConfigInitParams] PropertySource with lowest search precedence
2017-09-16 13:35:34 DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [servletContextInitParams] PropertySource with lowest search precedence
2017-09-16 13:35:34 DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
2017-09-16 13:35:34 DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
2017-09-16 13:35:34 DEBUG o.s.w.c.s.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,systemProperties,systemEnvironment]
2017-09-16 13:35:34 INFO  ccinfw.MainApplication - Starting MainApplication on kdclaptop with PID 17172 (C:\Users\dgmufasa\git\LocalCCINFieldworkServer\ccinfieldworkserver\target\classes started by dgmufasa in C:\Users\dgmufasa\git\LocalCCINFieldworkServer\ccinfieldworkserver)
2017-09-16 13:35:34 INFO  ccinfw.MainApplication - No active profile set, falling back to default profiles: default
2017-09-16 13:35:35 INFO  o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@44616f65: startup date [Sat Sep 16 13:35:35 UTC 2017]; root of context hierarchy
2017-09-16 13:35:38 DEBUG o.s.w.c.s.StandardServletEnvironment - Adding [applicationConfig: [classpath:/application.properties]] PropertySource with search precedence immediately lower than [applicationConfigurationProperties]
2017-09-16 13:35:38 DEBUG o.s.w.c.s.StandardServletEnvironment - Removing [applicationConfigurationProperties] PropertySource
2017-09-16 13:35:38 DEBUG o.s.w.c.s.StandardServletEnvironment - Removing [defaultProperties] PropertySource
2017-09-16 13:35:39 INFO  o.s.b.c.e.t.TomcatEmbeddedServletContainer - Tomcat initialized with port(s): 8080 (http)
2017-09-16 13:35:39 INFO  o.a.catalina.core.StandardService - Starting service [Tomcat]
2017-09-16 13:35:39 INFO  o.a.catalina.core.StandardEngine - Starting Servlet Engine: Apache Tomcat/8.5.15
2017-09-16 13:35:39 INFO  o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2017-09-16 13:35:39 DEBUG o.s.web.context.ContextLoader - Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2017-09-16 13:35:39 INFO  o.s.web.context.ContextLoader - Root 

[...snip ...]

2017-09-16 13:35:39 INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'webRequestLoggingFilter' to: [/*]
2017-09-16 13:35:39 INFO  o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'applicationContextIdFilter' to: [/*]
2017-09-16 13:35:40 DEBUG o.s.w.c.s.StandardServletEnvironment - Replacing [servletContextInitParams] PropertySource with [servletContextInitParams]
2017-09-16 13:36:01 ERROR o.a.tomcat.jdbc.pool.ConnectionPool - Unable to create initial connections of pool.
java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:735)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:667)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:482)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:326)
    at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:366)
    at org.springframework.boot.autoconfigure.orm.jpa.DatabaseLookup.getDatabase(DatabaseLookup.java:72)
    at org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.determineDatabase(JpaProperties.java:139)
    at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.jpaVendorAdapter(JpaBaseConfiguration.java:105)
    at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$d97029fd.CGLIB$jpaVendorAdapter$5(<generated>)
    at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$d97029fd$$FastClassBySpringCGLIB$$be6e3771.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
    at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$d97029fd.jpaVendorAdapter(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at ccinfw.MainApplication.main(MainApplication.java:19)
2017-09-16 13:36:02 WARN  o.s.b.a.orm.jpa.DatabaseLookup - Unable to determine jdbc url from datasource
org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Io exception: The Network Adapter could not establish the connection

更新(取出pom.xml def)

@Mark Rotteveel。感谢您的回复SID = service-name。见下文。

SQL> show parameter service
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      DSM

另外,请注意:

WORKS (Hibernate is doing the connection):
spring.datasource.url=jdbc:oracle:thin:@internalhost:1527:DSM  
internalhost example (192.167.1.xxx)

DOES NOT WORK (Hibernate is doing the connection)
spring.datasource.url=jdbc:oracle:thin:@externalhost.com:1527:DSM 
externalhost.com example (92.34.144.88)

DOES NOT WORK (Hibernate is doing the connection)
spring.datasource.url=jdbc:oracle:thin:@//externalhost.com:1527/DSM 
externalhost.com example (92.34.144.88)

TEST AT OS LEVEL DOES WORK ( using externalhost.com as a HOST )
sqlplus scott/tiger@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=externalhost.com)(PORT=1527))(CONNECT_DATA=(SERVICE_NAME=DSM)))

TEST AT OS LEVEL USING **PLAIN JAVA CODE** - in other words NO Hibernate - DOES WORK ( using externalhost.com as a HOST )
(see above)

如何进行更详细的追踪?

起初我认为这是一个网络错误 - 但是 - 我仍然可以使用常规JAVA连接到数据库,也可以在操作系统级别连接。再次,这些工作。使用Hibernate连接时,它似乎不起作用。

但是在内部(例如:192.168.1.xxx)地址上使用Hibernate可以正常工作。 将Hibernate用于完全相同的机器(但仅使用外部地址 - 例如:93.23.44.22)不起作用。

0 个答案:

没有答案