Spring boot wait_timeout

时间:2016-08-08 04:26:36

标签: java jdbc spring-boot spring-jdbc

我遇到 wait_timeout:

错误的问题
Mon Aug 08 06:16:31 CEST 2016
There was an unexpected error (type=Internal Server Error, status=500).
PreparedStatementCallback; SQL [SELECT main_cat FROM category_mapping WHERE cat=?]; The last packet successfully received from the server was 246,098,167 milliseconds ago. The last packet sent successfully to the server was 246,098,260 milliseconds ago. 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.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 246,098,167 milliseconds ago. The last packet sent successfully to the server was 246,098,260 milliseconds ago. 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.

application.properties:

spring.datasource.url=jdbc:mysql://localhost/allek?autoReconnect=true
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.testWhileIdle = true
spring.datasource.timeBetweenEvictionRunsMillis = 3600000
spring.datasource.validationQuery = SELECT 1

CategoryRepository.java:

package pl.bpo.archiwumallegro.repositories;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public class CategoryRepository {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public CategoryRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Optional<Long> findMainCategory(Long category) {
        String sql = "SELECT main_cat FROM category_mapping WHERE cat=?";
        return Optional.ofNullable(
                jdbcTemplate.queryForObject(sql, Long.class, category));
    }
}

我仍然能够始终保持连接或自动开启?

0 个答案:

没有答案