Connect to database running in local from application running inside docker container

Viewed 496

I am new to docker, attempting to connect oracle database running in local from an application running inside docker.

Getting below error:

IO Error: The Network Adapter could not establish the connection.

I browsed that we can pull oracle image and run in the docker container but I want to connect locally running database.

Can someone please guide me how to connect oracle database running in local windows machine from application inside docker container.

Code to get SessionFactory instance:

private SessionFactory getSessionFactory() {
    SessionFactory sessionFactory = null;
    if (sessionFactory == null) {
        try {
            Configuration configuration = new Configuration();
            // Hibernate settings equivalent to hibernate.cfg.xml's
            // properties
            System.out.println("SessionFactory getting called");
            Properties settings = new Properties();
            settings.put(Environment.DRIVER, "oracle.jdbc.driver.OracleDriver");
            settings.put(Environment.URL, "jdbc:oracle:thin:@localhost:1521:xxxxx");
            settings.put(Environment.USER, "xxxxx");
            settings.put(Environment.PASS, "xxxx");
            settings.put(Environment.DIALECT, "org.hibernate.dialect.Oracle12cDialect");
            settings.put(Environment.SHOW_SQL, "true");
            settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
            settings.put(Environment.C3P0_MIN_SIZE, 10);
            settings.put(Environment.C3P0_MAX_SIZE, 100);
            settings.put(Environment.C3P0_TIMEOUT, 300);
            configuration.setProperties(settings);
            configuration.addAnnotatedClass(SecretQuestionsEnity.class).addAnnotatedClass(CustomerEntity.class)
                    .addAnnotatedClass(AddressEntity.class).addAnnotatedClass(SecretAnswersEntity.class)
                    .addAnnotatedClass(BranchEntity.class).addAnnotatedClass(AccountEntity.class)
                    .addAnnotatedClass(CurrentACEntity.class).addAnnotatedClass(SavingsACEntity.class)
                    .addAnnotatedClass(TransactionEntity.class).addAnnotatedClass(PayeeEntity.class);
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return sessionFactory;
}

Error :

2020-07-24 16:32:09 WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 17002, SQLState: 08006
2020-07-24 16:32:10 ERROR o.h.e.jdbc.spi.SqlExceptionHelper - IO Error: The Network Adapter could not establish the connection
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
1 Answers

If you are running an application inside docker container then the localhost refers to container not your local windows machine

settings.put(Environment.URL, "jdbc:oracle:thin:@localhost:1521:xxxxx");

Either you can give your windows machine IP address instead of localhost in the URL string or you can read it from environment variable by passing it in docker run command.

Related