The last packet successfully received from the server was 43417 seconds ago

Viewed 13773

We are using Spring 4.2.5, Hibernate 4.1.4, MYSql to build REST services. We are facing very weird issue and couldn't understand what is happening exactly and getting below error:

The last packet successfully received from the server was 43417 seconds ago.The last packet sent successfully to the server was 43417 seconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

To get the database connection we are using java based configuration like below:

    @Configuration
    @EnableTransactionManagement
    @ComponentScan({ "api.configuration" })

public class HibernateConfiguration {
    @Autowired
    private Environment environment;

    private static final Logger logger = LoggerFactory.getLogger(HibernateConfiguration.class);
    @Bean
    public LocalSessionFactoryBean sessionFactory() throws PropertyVetoException {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "api.domain" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test?autoReconnect=true");
        dataSource.setUsername("test"); 
        dataSource.setPassword("test");
        return dataSource;
    }


    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.format_sql", "true");
        properties.put("hibernate.hbm2ddl.auto", "update");
        properties.put("connection.autoReconnect",  "true");
        properties.put("connection.autoReconnectForPools","true");
        properties.put("connection.is-connection-validation-required", "true");

        return properties;        
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws PropertyVetoException {
       LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
       em.setDataSource(dataSource());
       em.setPackagesToScan(new String[] { "api.domain" });

       JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
       em.setJpaVendorAdapter(vendorAdapter);
       em.setJpaProperties(hibernateProperties());

       return em;
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws PropertyVetoException{
       JpaTransactionManager transactionManager = new JpaTransactionManager();
       transactionManager.setEntityManagerFactory(entityManagerFactory().getObject() );
       return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
       return new PersistenceExceptionTranslationPostProcessor();
    }
}

Have googled out and find out we can solve this by adding below two properties:

<property name=”validationQuery” value=”SELECT 1″ />

<property name=”testOnBorrow” value=”true” />

As we are using java configuration instead of XML based, we don't know the exact property name to add for validationQuery and testOnBorrow in java class as we have hibernate.show_sql

Please suggest

1 Answers
Related