简体   繁体   English

在没有数据库的情况下启动Java / Play2.5 REST API

[英]Start Java/Play2.5 REST API without DB

I would like to start my Java/Play! 我想开始我的Java / Play! Service even though the Database is not yet reachable. 即使无法访问数据库也可以提供服务。 So far, if the database is not available the service stops. 到目前为止,如果数据库不可用,该服务将停止。 Is it possible in Play to try and reconnect every second or so without completely stopping the service and reconnecting to db as soon as connection is available. 在Play中,是否有可能尝试每秒重新连接一次而没有完全停止服务并在连接可用后立即重新连接到db。

Our application.conf currently looks like this: 我们的application.conf当前看起来像这样:

play.evolutions.enabled=false
//play.db.pool=bonecp

db {
   default.driver=org.postgresql.Driver
   default.url="jdbc:postgresql://postgresdb.postgres:5432/postcode"
   default.username="<username>"
   default.password="<strong-password>"
}

play.db {
  config = "db"
  prototype.hikaricp = {
    connectionTimeout = 250 milliseconds
    initializationFailFast = false
    readOnly = true
  }
}

ebean.default = ["models.*"]

Currently I get the following logs: 目前,我得到以下日志:

2017-05-15 13:26:54.525 [error] postcode-play: Connection error: 
org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections
  Ort: Datei: postinit.c, Routine: InitPostgres, Zeile: 779.
  Server SQLState: 53300
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2412)
    at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2538)
    at org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:122)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:227)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
    at org.postgresql.Driver.makeConnection(Driver.java:431)
    at org.postgresql.Driver.connect(Driver.java:247)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:95)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:101)

2017-05-15 13:26:54.626 [error] postcode-play: Connection error: 
org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections
  Ort: Datei: postinit.c, Routine: InitPostgres, Zeile: 779.
  Server SQLState: 53300
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2412)
    at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2538)
    at org.postgresql.core.v3.QueryExecutorImpl.<init>(QueryExecutorImpl.java:122)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:227)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
    at org.postgresql.Driver.makeConnection(Driver.java:431)
    at org.postgresql.Driver.connect(Driver.java:247)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:95)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:101)
.
.
.

Indicating that the server does not stop altogether but tries to connect to db multiple times which is as desired. 指示服务器没有完全停止,而是尝试按需要多次连接到db。 But if the database becomes available the following logs are written: 但是,如果数据库可用,则会写入以下日志:

2017-05-15 14:08:43.748 [error] postcode-play: 

! @7430ed7gf - Internal server error, for (GET) [/?postcode=88682&country=DE] ->

play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]]
    at play.api.Configuration$.configError(Configuration.scala:155)
    at play.api.Configuration.reportError(Configuration.scala:808)
    at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:48)
    at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:42)
    at scala.collection.immutable.List.foreach(List.scala:392)
    at play.api.db.DefaultDBApi.connect(DefaultDBApi.scala:42)
    at play.api.db.DBApiProvider.get$lzycompute(DBModule.scala:72)
    at play.api.db.DBApiProvider.get(DBModule.scala:62)
    at play.api.db.DBApiProvider.get(DBModule.scala:58)
    at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:81)
Caused by: java.sql.SQLTransientConnectionException: HikariPool-4 - Connection is not available, request timed out after 5001ms.
    at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:548)
    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:186)
    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:145)
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:83)
    at play.api.db.DefaultDatabase.getConnection(Databases.scala:142)
    at play.api.db.DefaultDatabase.getConnection(Databases.scala:138)
    at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:44)
    at play.api.db.DefaultDBApi$$anonfun$connect$1.apply(DefaultDBApi.scala:42)
    at scala.collection.immutable.List.foreach(List.scala:392)
    at play.api.db.DefaultDBApi.connect(DefaultDBApi.scala:42)
Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:265)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
    at org.postgresql.Driver.makeConnection(Driver.java:431)
    at org.postgresql.Driver.connect(Driver.java:247)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:95)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:101)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:341)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:193)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:430)
Caused by: java.net.ConnectException: Verbindungsaufbau abgelehnt (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.postgresql.core.PGStream.<init>(PGStream.java:62)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:144)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194
)

The server cannot reach out to the database even though it is available now. 即使服务器现在可用,也无法与数据库联系。 If I restart the server the database will be found and can be worked with, but the database lways has to be there on startup. 如果我重新启动服务器,将找到数据库并且可以使用该数据库,但是数据库始终必须在启动时存在。 How can I fix that, so that the app can be started independently of the database? 如何解决此问题,以便可以独立于数据库启动应用程序?

If the database becomes unavailable after the application has started it is no problem after the db becomes available again. 如果数据库在应用程序启动后变得不可用,则在数据库再次变得可用之后没有问题。 it is automatically reconnected. 它会自动重新连接。

Any help is greatly apreciated! 非常感谢您的帮助!

It seems like you are leaving some connections open to Postgres. 似乎您在向Postgres开放一些联系。 These connections must be closed or else Postgres will hit its max_connections limit. 这些连接必须关闭 ,否则Postgres将达到其max_connections限制。

You could increase max_connections but it's not recommended as performance of Postgres will degrade if it's set too high. 您可以增加max_connections但不建议这样做,因为如果设置得太高,Postgres的性能将会降低。 Also, as you are using HikariCP it wouldn't be a bad idea to take a look at the HikariCP docs , in particular the idleTimeout and the minimumIdle config variables should help you. 此外,当您使用HikariCP时,看看HikariCP文档并不是一个坏主意,特别是idleTimeoutminimumIdle配置变量应该可以为您提供帮助。

If the above doesn't work then you must confirm exactly how many connections are being made to Postgres. 如果上述方法不起作用,则必须确切确认与Postgres建立了多少连接。 Maybe even check the number of sessions you personally are making to Postgres via SSH or through other apps, not just the number of Play Framework connections. 甚至可以检查您个人通过SSH或其他应用与Postgres进行的会话数,而不仅仅是Play框架连接数。

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

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