gcp在spring引导服务中使用的postgres连接(使用sql云代理)

lnlaulya  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(268)

我正在开发一个spring引导服务来发布数据并将其持久化 GCP SQL Postgres 数据库。问题是,当我用请求强调服务时,会遇到一个sql异常,关于使用所有可用连接:

"FATAL: remaining connection slots are reserved for non-replication superuser connections"

我发现了这个问题,并添加了一个适当的hikari配置来限制已使用的连接并设置关闭空闲连接的时间限制,下面是我的properties.yml配置:

type: com.zaxxer.hikari.HikariDataSource
 hikari:
   initializationFailTimeout: 30000
   idle-timeout: 30000
   minimum-idle: 5
   maximum-pool-size: 15
   connection-timeout: 20000
   max-lifetime: 1000

当我使用相同的数据库在本地运行该服务时,该服务可以很好地使用该设置,但是当我从我的云设置运行它时,它会占用所有可用的连接,然后得到相同的异常。
重要!我正在使用sqlcloud代理连接到数据库。
以下是数据库可用连接的屏幕截图:
1-运行服务前

2-在本地运行服务之后

3-从云端运行服务后

fzwojiic

fzwojiic1#

在对这个问题进行了几天的调查之后,我们找到了一个解决方案,可以减轻问题,但不能完全解决问题(最后提到了理想的解决方案)。
如果您想继续使用sqlcloud代理,那么您需要接受这样一个事实:您不能完全控制您的数据库连接配置,因为sqlcloud代理可能会使这些连接保持活动状态超过您配置的时间(源代码)。
为了缓解此问题,我们使用docker registry中的sql cloud proxy 1.19.2,并使用以下hikari配置:

hikari:
  idle-timeout: 30000 # maximum amount of time (in milliseconds) that a connection is allowed to sit idle in the pool
  minimum-idle: 1 # minimum number of idle connections that HikariCP tries to maintain in the pool, including both idle and in-use connections. If the idle connections dip below this value, HikariCP will make a best effort to restore them quickly and efficiently
  maximum-pool-size: 15 # maximum size that the pool is allowed to reach, including both idle and in-use connections. Basically this value will determine the maximum number of actual connections to the database backend
  connection-timeout: 20000 #maximum number of milliseconds that a client will wait for a connection
  max-lifetime: 20000 # maximum lifetime in milliseconds of a connection in the pool after it is closed.

在这种情况下,正确的解决方案是使用一个共享的专有网络来与您的数据库建立一个私有连接,您将依靠您的数据库驱动程序来建立这些连接。

相关问题