javaoracle-databasespring-bootlistenertnsnames

Failing to obtain a JDBC connection to Oracle


I am currently trying to link my Spring Boot application using JDBC to connect to my Oracle database.

The overall design of the project is to be able to perform CRUD operations.

When I compile and run my project initially there are no issues presented.

When I try to connect to the localhost in the browser using the predetermined port in the application.properties file, I get the following error message from the browser:

There was an unexpected error (type=Internal Server Error, status=500).
Failed to obtain JDBC Connection; nested exception is 
java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection

The (minimized) error message that I receive in the Eclipse console is the following:

java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
     at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:801) ~[ojdbc8-18.3.0.0.jar:18.3.0.0.0]
     ...
     ...

Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
     at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:569) ~[ojdbc8-18.3.0.0.jar:18.3.0.0.0]
     ...
     ...

Caused by: java.net.UnknownHostException: locahost
    at java.net.InetAddress.getAllByName0(Unknown Source) ~[na:1.8.0_151]
     ...
     ...

2021-08-14 10:09:01.177 ERROR 33096 --- [nio-8095-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection] with root cause

java.net.UnknownHostException: locahost
     at java.net.InetAddress.getAllByName0(Unknown Source) ~[na:1.8.0_151]
     ...
     ...

My TNSListener and my OracleServiceXE are both running.

tnsnames.ora file

    XE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = LAPTOP-R01ERMVG)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SID = XE)
    )
  )

LISTENER_XE =
  (ADDRESS = (PROTOCOL = TCP)(HOST = LAPTOP-R01ERMVG)(PORT = 1521))


ORACLR_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
    (CONNECT_DATA =
      (SID = CLRExtProc)
      (PRESENTATION = RO)
    )
  )

listener.ora file

 SID_LIST_LISTENER=
   (SID_LIST=
     (SID_DESC=

          (GLOBAL_DBNAME=salesdb.mycompany)

          (SID_NAME=XE)         
          (ORACLE_HOME=C:\app\bendg\product\18.0.0\dbhomeXE)
            #PRESPAWN CONFIG
         (PRESPAWN_MAX=20)
      (PRESPAWN_LIST=
           (PRESPAWN_DESC=(PROTOCOL=tcp)(POOL_SIZE=2)(TIMEOUT=1))
      )
     )
    )

My hosts file has a single line stating:

127.0.0.1       localhost

For my project build:

application.properties file

spring.datasource.url=jdbc:oracle:thin:@locahost:1521:XE

spring.datasource.username=system
spring.datasource.password=password
logging.level.root=INFO
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

server.port=8095

pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>net.codejava</groupId>
  <artifactId>central</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8.RELEASE</version>
  </parent>
  
  <!-- Dependencies -->
  
  <dependencies> 

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>

     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
        
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
        
    <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>18.3.0.0</version>                         
    </dependency>
                
  </dependencies>
    
        <build>
            <plugins>
                <plugin> 
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>               
                </plugin>           
            </plugins>      
        </build>
          
</project>

For the Oracle Database Connection:

From reading similar questions and answers it sounds like there's an issue within my tnsnames/listener files however I cannot distinguish where the problem lies in my project after fiddling around for the best part of two days.


Solution

  • Update your application.properties to below and check this might solve your connectivity

    spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE