启动没有数据库的{Java / Play2.5 REST API

时间:2017-05-15 12:17:36

标签: java playframework

我想开始我的Java / Play!即使数据库尚不可访问,也可以进行服务。到目前为止,如果数据库不可用,则服务停止。是否可以在Play中尝试重新连接,而不是完全停止服务,并在连接可用时立即重新连接到db。

我们的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.*"]

目前我收到以下日志:

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)
.
.
.

指示服务器没有完全停止,但尝试多次连接到db,这是所需的。但是,如果数据库可用,则会写入以下日志:

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
)

即使服务器现在可用,服务器也无法联系到数据库。如果我重新启动服务器,将找到数据库并可以使用,但数据库lways必须在启动时出现。我该如何解决这个问题,以便可以独立于数据库启动应用程序?

如果数据库在应用程序启动后变得不可用,则在数据库再次可用后没有问题。它会自动重新连接。

任何帮助都非常有用!

1 个答案:

答案 0 :(得分:0)

好像你要给Postgres留下一些联系。必须关闭这些连接,否则Postgres将达到max_connections限制。

你可以increase max_connections,但不建议这样做,因为如果Postgres设置得太高,它的性能会降低。此外,当您使用HikariCP时,查看HikariCP docs并不是一个坏主意,特别是idleTimeoutminimumIdle配置变量可以帮助您。

如果上述方法无效,则必须确切确认Postgres的连接数量。甚至可以通过SSH或其他应用程序检查您个人对Postgres进行的会话数量,而不仅仅是Play Framework连接的数量。