英文:
Connect to database running in local from application running inside docker container
问题
我是docker的新手,尝试从在docker内部运行的应用程序连接本地运行的oracle数据库。
遇到以下错误:
IO错误:网络适配器无法建立连接。
我浏览了一下,我们可以拉取oracle镜像并在docker容器中运行,但我想从本地运行的数据库连接。
请问有人可以指导我如何从“本地Windows计算机”连接到“docker容器内的应用程序”中运行的oracle数据库吗?
获取SessionFactory
实例的代码:
private SessionFactory getSessionFactory() {
SessionFactory sessionFactory = null;
if (sessionFactory == null) {
try {
Configuration configuration = new Configuration();
// Hibernate设置,相当于hibernate.cfg.xml中的属性
System.out.println("正在调用SessionFactory");
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 警告 o.h.e.jdbc.spi.SqlExceptionHelper - SQL错误:17002,SQL状态:08006
2020-07-24 16:32:10 错误 o.h.e.jdbc.spi.SqlExceptionHelper - IO错误:网络适配器无法建立连接
org.hibernate.service.spi.ServiceException: 无法创建所请求的服务[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)
英文:
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
得分: 2
如果您在Docker容器内运行应用程序,则localhost
指的是容器
,而不是您的本地Windows计算机。
settings.put(Environment.URL, "jdbc:oracle:thin:@localhost:1521:xxxxx");
您可以在URL字符串中使用您的Windows计算机IP地址,而不是localhost
,或者您可以通过在Docker run
命令中传递它来从环境变量中读取它。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论