简体   繁体   English

postgres从Java重启后重新连接到postgres数据库

[英]Reconnecting to a postgres database after postgres restart from Java

I'm using postgres 9.1, org.apache.commons.dbcp.BasicDataSource (for my connection pool) and Java 1.7. 我正在使用postgres 9.1, org.apache.commons.dbcp.BasicDataSource (用于我的连接池)和Java 1.7。 When I restart my postgres server, I get exceptions like org.postgresql.util.PSQLException: FATAL: terminating connection due to administrator command . 当我重新启动postgres服务器时,我得到了像org.postgresql.util.PSQLException: FATAL: terminating connection due to administrator command这样的异常org.postgresql.util.PSQLException: FATAL: terminating connection due to administrator command

How can I make it so the connections automatically re-connect to the restarted database? 如何使连接自动重新连接到重新启动的数据库?

DBCP has a connection validation query option - validationQuery , according to the docs . 根据文档 ,DBCP有一个连接验证查询选项 - validationQuery You can set it to something like SELECT 1; 您可以将其设置为SELECT 1; and DBCP will run that before returning a connection to test it. 并且DBCP将在返回连接之前运行该测试。

That said, your app really should handle this case. 也就是说,你的应用真的应该处理这种情况。 SQL queries can fail for all sorts of reasons and you should always do your queries in a retry loop with a time back-off and retry limit, plus some logic to decide what exceptions are recoverable on retry and which aren't (use the SQLState for that). SQL查询可能由于各种原因而失败,您应该始终在具有时间退避和重试限制的重试循环中执行查询,并使用一些逻辑来确定哪些异常在重试时可恢复,哪些不是(使用SQLState)为了那个原因)。

In particular, validation is subject to a race condition where you can have event orderings like: 特别是,验证受竞争条件的影响,您可以在其中进行以下事件排序:

  1. Validate 验证
  2. Pass connection to app 传递给app的连接
  3. Server shutdown 服务器关闭
  4. App runs first statement App运行第一个声明

or 要么

  1. Validate 验证
  2. Pass connection to app 传递给app的连接
  3. App runs first statement, opening a transaction App运行第一个语句,打开一个事务
  4. Server shutdown 服务器关闭
  5. App runs second statement App运行第二个声明

... so it remains important for your app to have a proper retry loop and good transaction handling. ...因此,对于您的应用来说,保持适当的重试循环和良好的事务处理仍然很重要。

You can get the SQLState from the SQLException: SQLException.getSQLState . 您可以从SQLException获取SQLState: SQLException.getSQLState The codes for PostgreSQL are in the PostgreSQL manual . PostgreSQL的代码在PostgreSQL手册中

In this particular case the PostgreSQL connection is telling you that the server was shut down after the connection was created. 在这种特殊情况下,PostgreSQL连接告诉您在创建连接后服务器已关闭。 DBCP connection pool does not handle this condition with it's default configuration. DBCP连接池无法使用默认配置处理此情况。

Even if you set validationQuery parameter to something like SELECT 1 , it will not be used, unless you also set at least one of the testOnXXXX parameters. 即使您将validationQuery参数设置为SELECT 1之类的东西,也不会使用它,除非您还设置了至少一个testOnXXXX参数。

I usually set both testOnCreate and testOnBorrow to true . 我通常将testOnCreatetestOnBorrow都设置为true

Also check other defaults of DBCP (in the org.apache.commons.pool2.impl.BaseObjectPoolConfig), because in my opinion they are not well suited for production environments. 还要检查DBCP的其他默认值(在org.apache.commons.pool2.impl.BaseObjectPoolConfig中),因为在我看来它们不适合生产环境。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM