播放应用程序在空闲状态下死机,重新启动新请求?

时间:2015-06-11 10:00:34

标签: java hibernate playframework playframework-2.3 hikaricp

我正在使用HiknateCp,Hibernate和我在java中的playframework应用程序。在几分钟的空闲状态之后,似乎播放应用程序死了,当它获得新请求时它再次启动。有时候我也看到Db持久性错误,而且sessionfactory不可用的消息,我认为这是这个问题的副作用。为什么应用程序死机处于空闲状态,我可以将它配置为不这样做吗?

在几分钟的空闲时间内,每次新请求后都会得到以下日志 -

[info] application - Application shutdown...
[info] application - Stopping HikariCP connection pool...
[info] application - Starting HikariCP connection pool...

Persistence.xml: -

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> 
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.use_sql_comments" value="true"/>
            <property name="hibernate.logSql" value="true"/>            
            <property name="hibernate.connection.provider_class" value="com.zaxxer.hikari.hibernate.HikariConnectionProvider"/>
            <property name="hibernate.hikari.dataSourceClassName" value="com.impossibl.postgres.jdbc.PGDataSource"/>
        </properties>
    </persistence-unit>

</persistence>

Application.conf: -

# JPA configuration
# ~~~~~
# You can declare as many Ebean servers as you want.
# By convention, the default server is named `default`
jpa.default=defaultPersistenceUnit

# Assets configuration
# ~~~~~
"assets.cache./public/stylesheets/bootstrap.min.css"="max-age=360000"

# Logger
# ~~~~~
# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .

# Root logger:
logger=ERROR

# Logger used by the framework:
logger.play=INFO

# Logger provided to your application:
logger.application=DEBUG

谢谢!

2 个答案:

答案 0 :(得分:1)

您的数据库连接提供商什么时候不做任何重新连接,您需要向您的提供商说明在这种情况下该做什么,例如:

    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
                         version="2.0">

                <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
                    <provider>org.hibernate.ejb.HibernatePersistence</provider>
                    <properties>
                        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> 
                        <property name="hibernate.show_sql" value="true"/>
                        <property name="hibernate.format_sql" value="true"/>
                        <property name="hibernate.use_sql_comments" value="true"/>
                        <property name="hibernate.logSql" value="true"/>            
                        <property name="hibernate.connection.provider_class" value="com.zaxxer.hikari.hibernate.HikariConnectionProvider"/>
                        <property name="hibernate.hikari.dataSourceClassName" value="com.impossibl.postgres.jdbc.PGDataSource"


<property name="hibernate.hikari.timeBetweenEvictionRunsMillis="30000" />
        <property name="hibernate.hikari.minEvictableIdleTimeMillis="60000"/>
        <property name="hibernate.hikari.removeAbandonedOnBorrow="true" />    <property name="hibernate.hikari.removeAbandonedOnMaintenance="true" />
    <property name="hibernate.hikari.maxActive="30" />
    <property name="hibernate.hikari.maxIdle="10" />
    <property name="hibernate.hikari.maxWait="10000" />
    <property name="hibernate.hikari.initialSize="5" />
    <property name="hibernate.hikari.validationQuery= "SELECT 1" /> 
    <property name="hibernate.hikari.validationInterval="30000" />
    <property name="hibernate.hikari.removeAbandoned="true" /> 
    <property name="hibernate.hikari.removeAbandonedTimeout="60" /> 
    <property name="hibernate.hikari.logAbandoned="true"/>


                    </properties>
                </persistence-unit>

            </persistence>

点击此链接 https://github.com/brettwooldridge/HikariCP/wiki/Bad-Behavior:-Handling-Database-Down

答案 1 :(得分:0)

这背后的原因可能是连接被数据库关闭,因此当应用程序尝试查询这些连接时,sessionFactory不可用,并且会抛出异常。

我过去遇到过类似的问题,唯一的区别是我使用的是c3p0连接池。因此,请尝试为您的连接提供商设置connectionTestQueryidleTimeout属性。

You may refer to this answer