Spring Boot应用程序MySQL SocketTimeoutException

时间:2019-03-07 14:32:08

标签: mysql spring-boot

我有一个使用Spring Boot的应用程序,该应用程序作为守护程序运行,并且每24小时使用MySQL进行一次处理。我昨天发布了它,今天它第一次尝试执行处理,但是不幸的是,我得到了以下错误(相关的stacktrace段):

package enroute.spring.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import enroute.spring.model.College;
import enroute.spring.model.Course;
import enroute.spring.model.ErrorResponse;
import enroute.spring.services.area.AreaService;
import enroute.spring.services.college.CollegeService;
import enroute.spring.services.college_course.CourseCollegeService;
import enroute.spring.services.course.CourseService;
import enroute.spring.services.stream.StreamService;

@RestController
public class ApplicationController {
	
	@Autowired
    private CollegeService collegeService;
 
	@Autowired
    private CourseService courseService;
	
	@Autowired
    private AreaService areaService;
	
	@Autowired
    private CourseCollegeService collegeCourseService;
	
	@Autowired
    private StreamService streamService;
	
	//Get Colleges Name By Stream Id
	@RequestMapping(value="/college/{streamId}", method= RequestMethod.GET)
	public @ResponseBody List<College> getColleges(@PathVariable int streamId){
		 List<College> list = collegeService.getSpecificColleges(streamId);
		 return list;
	}
	
	//Get Courses Name By Stream Id
	@RequestMapping(value="/courses/{streamId}", method= RequestMethod.GET)
	public @ResponseBody List<Course> getSpecificColleges(@PathVariable int streamId){
		 List<Course> list = courseService.getSpecificColleges(streamId);
		 return list;
	}
	
	//Get courses By college Name
	@RequestMapping(value="/college/{collegeName}/courses", method= RequestMethod.GET)
	public @ResponseBody List<Course> getCoursesFromColleges(@PathVariable String collegeName){
		 List<Course> list = collegeService.getCoursesFromColleges(collegeName);
		 return list;
	}
	
	
	@ExceptionHandler(Exception.class)
	public ResponseEntity<ErrorResponse> exceptionHandler(Exception ex) {
		ex.printStackTrace();
		ErrorResponse error = new ErrorResponse();
		error.setErrorCode(HttpStatus.PRECONDITION_FAILED.value());
		error.setMessage(ex.getMessage());
		return new ResponseEntity<ErrorResponse>(error, HttpStatus.OK);
	}
	
}

我怀疑当时的MySQL出了什么问题,我更倾向于认为连接过时,尽管我认为连接池应该解决这个问题。

任何人都遇到过这种情况并能够解决。请让我知道我还应该发布哪些其他相关信息。这是我的08:00:31.264 [pool-1-thread-1] ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure ... Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure ... Caused by: java.net.SocketTimeoutException: connect timed out ,当然没有安全信息:

application.properties

spring.datasource.url=jdbc:mysql://<server>:3306/<schema>?characterEncoding=UTF-8&useUnicode=true spring.datasource.username=<username> spring.datasource.password=<password> spring.datasource.driver-class-name=com.mysql.jdbc.Driver 的相关内容:

pom.xml

应用程序使用Spring Boot提供的... <properties> ... <mysql-connector-java.version>5.1.38</mysql-connector-java.version> <spring.boot.version>2.1.3.RELEASE</spring.boot.version> ... </properties> <dependencies> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector-java.version}</version> </dependency> ... </dependencies> ...

1 个答案:

答案 0 :(得分:0)

1)由于创建的数据库连接不活动而发生

为避免这种情况,只需在以下位置添加以下属性   application.properties。

spring.datasource.testWhileIdle = true
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.validationQuery = SELECT 1

这将通过简单地触发SELECT 1查询来结束活动。