从docker容器中运行的应用程序连接到本地运行的数据库

时间:2020-07-24 16:34:34

标签: java database windows docker docker-container

我是docker的新手,试图从docker内部运行的应用程序连接本地运行的oracle数据库。

得到以下错误:

IO错误:网络适配器无法建立连接

我浏览了我们可以提取oracle映像并在docker容器中运行,但是我想连接本地运行的数据库。

有人可以指导我如何从local windows machine连接在application inside docker container中运行的oracle数据库。

获取SessionFactory实例的代码:

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;
}

错误:

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 个答案:

答案 0 :(得分:2)

如果您正在docker容器中运行应用程序,那么localhost指的是container,而不是您的本地Windows计算机

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

您可以在URL字符串中提供您的 Windows机器IP地址,而不是localhost,也可以在docker {{1}中通过passing it从环境变量中读取它。 }命令。