MySql超时 - 我应该在Spring应用程序中设置autoReconnect = true吗?

Geo*_*rge 3 java mysql spring database-connection jdbc

在我的网站(使用Spring 2.5和MySql)上不活动一段时间后,我收到以下错误:

org.springframework.dao.RecoverableDataAccessException: The last packet sent successfully to the server was 52,847,830 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.

根据这个问题链接的bug,我不应该只设置autoReconnect = true.这是否意味着我必须在我做的任何查询中捕获此异常,然后重试该事务?这个逻辑应该在数据访问层还是模型层?是否有一种简单的方法来处理这个而不是包装每个查询以捕获它?

ewe*_*nli 5

我建议您使用连接池.它们可以提高性能,并可以处理低级细节,例如超时后重新连接等.

一个好的是c3p0,但还有其他的.虽然我不知道所有的细节,但Spring对此有所支持.DataSourceSpring 的配置.