简体   繁体   中英

IO Error: The Network Adapter could not establish the connection when running oracle DB as docker container and trying to connect via Tomcat

I have setup a docker container with oracle db using following yml.

version: '3'
services:
  oracle:
    image: "carloscastillo/rgt-oracle-xe-11g"
    ports:
      - "49160:22"
      - "49161:1521"
      - "49162:8080"
    volumes:
      - "dbdata:/u01/app/oracle carloscastillo/rgt-oracle-xe-11g"
    environment:
      - ORACLE_ALLOW_REMOTE=true
      - ORACLE_ENABLE_XDB=true
volumes:
  dbdata:

I'm trying to access the db in tomcat.This is the Context.xml db configurations.

<Resource name="ORACLE" auth="Container" type="javax.sql.DataSource"
      factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
maxActive="100" maxIdle="30" maxWait="10000"
username="system" password="oracle" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:49161/xe"/>

Added the relevant driver into class path too.Still getting the error "IO Error: The Network Adapter could not establish the connection ". Also tried url as jdbc:oracle:thin:@localhost:49161:xe.This is the first time i'm doing this.Could not solve what i'm doing wrong.

I tried your docker-compose. Worked out of the box on my MacOS. I suggest you add double-forslash to your connect string.

jdbc:oracle:thin:@//localhost:49161/xe

The double-forslash is to tell your JDBC driver, "Hey, do not try to use any environment information except what I give you solely on this one line" - EZConnect (EasyConnect)"

An advice is always to make sure you can connect from sqlplus or sqlcl before moving on the any app-lib (java,python,php,etc..). Like so:

sqlplus system/oracle@//localhost:49161/xe

SQL*Plus: Release 19.0.0.0.0 - Production on Wed Nov 11 13:11:30 2020
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

system@XE> @inst

INSTANCE INFORMATION

Ins                                                                          Stat Para             DB     Shdn
  # Instance             Host                   Version    Up since          us   llel Th# Arch    Status Pend Logins  SGA Size (GB)
--- -------------------- ---------------------- ---------- ----------------- ---- ---- --- ------- ------ ---- ------- -------------
  1 XE                   5600801e8a25           11.2.0.2.0 11.11.20 11:58:33 OPEN NO     1 STOPPED ACTIVE NO   ALLOWED             1

Elapsed: 00:00:00.02

I have seen plenty of times where the database listener isn't started although the container and database processes are running. My approach to this is

# restart the container until it works. Second or third time it normally works....
docker-container restart oracle 

# fire up the listener. Remember to run as 'oracle' and wait for 10s until the service_name 'XE' is exposed in the listener process
docker-compose exec -u oracle oracle sh -c "lsnrctl stop;lsnrctl start;echo 'alter database register' | sqlplus / as sysdba; sleep 10; lsnrctl status"

# For real world CI/CD scenarios I will add a healthcheck to the docker-compose service.
    healthcheck:
      test: sql -L -S sys/oracle@localhost:1521/XE as sysdba < /dev/null |grep 'ORA-'; if [[ $$? == 1 ]]; then echo 0; else echo 1; fi
      interval: 1m
      timeout: 10s
      retries: 20
      start_period: 40s

Best of luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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