Play application dies in idle state, restart on new request?

978 views Asked by At

I am using HikariCp, Hibernate with my playframework application in java. After few mins of idle state, it seems play application dies, and when it gets new request it starts again. Sometime I have also seen Db persistence error with message that sessionfactory not available, which I believe is side-effect of this issue. Why application dies in idle state, and can I some how configure it to not do so ?

I get following logs, after every new requests in few mins of idle time -

[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

Thanks !

2

There are 2 answers

0
Sangam Belose On

The reason behind this might be the connections are closed by database and so when the application tries to query on those connections sessionFactory is not available and it will throw exception.

I have faced similar issues in past and the only difference is I was using the c3p0 connection pool. So try setting the connectionTestQuery and idleTimeout property for your connection provider.

You may refer to this answer

0
Sven G. On

Your database connection provider when is iddle donĀ“t do anything for re-connect, you will need indicate to your provider what to do in this case, for example:

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

check this link https://github.com/brettwooldridge/HikariCP/wiki/Bad-Behavior:-Handling-Database-Down