Spring启动和hibernate mysql数据库连接不断断开连接

时间:2016-08-14 08:59:17

标签: mysql spring hibernate tomcat spring-boot

我的网络应用程序现在运行完美,并将数据保存到mysql数据库。只有我无法理解连接的一件事是,它在一段时间后丢弃了数据库上的连接,我需要继续停止并再次启动Tomcat以保持与应用程序的连接活动。 我需要在tomcat web服务器或mysql数据库中设置任何会话超时吗?

下面是我在spring boot中的application.properties。

debug=true

spring.datasource.url=jdbc:mysql://localhost/database
spring.datasource.username=username
spring.datasource.password=pass
spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true

logging.level.org.hibernate.SQL=DEBUG


Below is the exception:

Exception: org.springframework.dao.DataAccessResourceFailureException: could not prepare statement; nested exception is org.hibernate.exception.JDBCConnectionException: could not prepare statement

感谢您的帮助。

唐纳德

1 个答案:

答案 0 :(得分:0)

这对我有用。只需添加到您的application.properties

spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1