简体   繁体   English

从本地机器到 AWS RDS PostgreSQL 数据库的连接速度非常慢

[英]Painfully slow connection speed from local machine to AWS RDS PostgreSQL database

I've looked everywhere for an answer to this problem.我到处寻找这个问题的答案。 I found a similar (unanswered) question on SO but not related to Java or SSL issues.我在 SO 上发现了一个类似(未回答)的问题,但与 Java 或 SSL 问题无关。

Important Information:重要信息:

  • The database I connect to is PostgreSQL on RDS.我连接的数据库是 RDS 上的 PostgreSQL。
  • I can actually connect to the DB (it takes about 3 minutes to establish the connection).我实际上可以连接到数据库(建立连接大约需要 3 分钟)。 After the connection is established, simple queries take between 15-120 seconds.建立连接后,简单查询需要 15-120 秒。
  • The problem only happens when connecting from my code.只有从我的代码连接时才会出现问题。
  • Connecting from Postico (on the same machine) works perfectly.从 Postico 连接(在同一台机器上)完美运行。
  • Deploying to AWS EC2 the exact same connection works perfectly.将完全相同的连接部署到 AWS EC2 可以完美运行。
  • I have added the RDS certificates to the keystore.我已将 RDS 证书添加到密钥库。
  • The RDS force_ssl parameter is set to true. RDS force_ssl 参数设置为 true。
  • Notice the + "?ssl=true" parameter at the end of the connection string.请注意连接字符串末尾的+ "?ssl=true"参数。
  • Tested replacing the domain by the direct IP (to rule out DNS problems).测试用直接 IP 替换域(以排除 DNS 问题)。 Same thing.一样。

I suspect it has something to do with SSL but can't confirm it.我怀疑它与 SSL 有关,但无法确认。

This is my connection (pretty standard):这是我的连接(非常标准):

    String dbUriString = ConfigUtils.getEnv("DATABASE_URL");

    URI dbUri = new URI(dbUriString);

    String username = dbUri.getUserInfo().split(":")[0];
    String password = dbUri.getUserInfo().split(":")[1];
    String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath() + "?ssl=true";

    ComboPooledDataSource datasource = new ComboPooledDataSource();

    datasource.setUser(username);
    datasource.setPassword(password);
    datasource.setJdbcUrl(dbUrl);
    datasource.setDriverClass("org.postgresql.Driver");

    datasource.setMaxPoolSize(20);
    datasource.setMinPoolSize(5);
    datasource.setAcquireIncrement(1);
    datasource.setAcquireRetryAttempts(3);
    datasource.setInitialPoolSize(5);
    datasource.setTestConnectionOnCheckout(true);

    return datasource;

A little more information:更多信息:

  • It's a Play Framework application.这是一个 Play 框架应用程序。
  • Java 8. Java 8.
  • DataSource connector c3p0.数据源连接器 c3p0。

Any help is deeply appreciated.任何帮助都深表感谢。

Connecting to our Postgres database on AWS took up to a minute or two to estabilish a connection, then it worked fine.连接到我们在 AWS 上的 Postgres 数据库需要一两分钟来建立连接,然后就可以正常工作了。

The reason turned out to be DNS resolution.原因竟然是 DNS 分辨率。

I ran telnet <database host> 5432 and discovered it was trying to connect to an IPv6 connection.我运行telnet <database host> 5432并发现它正在尝试连接到 IPv6 连接。 After a minute or so, it would time out and try the IPv4 connection, which worked instantly.大约一分钟后,它会超时并尝试 IPv4 连接,该连接立即生效。

Running dscacheutil -q host -a name <database host> on my macOS machine indeed showed the database host was resolved to an IPv6 and IPv4 address.在我的 macOS 机器上运行dscacheutil -q host -a name <database host>确实显示数据库主机已解析为 IPv6 和 IPv4 地址。

I changed the DNS settings on my machine.我更改了我机器上的 DNS 设置。 Specifically, I told my VPN client to use Google's DNS servers for the amazonaws.com domain.具体来说,我告诉我的 VPN 客户端将 Google 的 DNS 服务器用于 amazonaws.com 域。 After that, everything worked perfectly.在那之后,一切都很完美。 Indeed, on rerunning dscacheutil -q host -a name <database host> , I only saw the IPv4 address this time.事实上,在重新运行dscacheutil -q host -a name <database host>时,我这次只看到了 IPv4 地址。

No idea by the IPv6 connection wasn't working, but the short of it was that DNS was part of the issue.不知道 IPv6 连接不起作用,但不足之处在于 DNS 是问题的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与本地计算机中的MS SQL数据库建立连接 - Make a connection to a MS SQL database in the local machine 从git导入AWS项目到本地计算机 - Importing an AWS project from git to local machine 如何确定传入连接来自本地计算机 - How to determine an incoming connection is from local machine Glide 从 firebase 加载图像使用 URL 非常缓慢 - Glide loads images from firebase painfully slow using URLs 使用JDK 1.7时,JavaMail在OS X Yosemite上的本地SMTP速度很慢 - JavaMail painfully slow to local SMTP on OS X Yosemite using JDK 1.7 AWS RDS连接数据库错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 - AWS RDS connection database error : com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure File.lastModified()痛苦地慢! - File.lastModified() painfully slow! Java文件上传速度很慢 - Java file uploading painfully slow ECLIPSE AWS Toolkit-新的RDS连接失败 - ECLIPSE AWS Toolkit -New RDS connection failure 如何通过 JDBC 连接器从我的本地 java 应用程序连接到 AWS RDS - How to connect to AWS RDS from my local java application by JDBC connector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM