Spring Boot Data Jpa连接MySQL时HikariPool验证连接失败求助
HikariCP连接验证失败,调整maxLifetime后问题依旧
我已查阅该主题相关问题及解答,尝试所有建议均无效,因自身情况存在特殊性,故发帖求助。
错误信息
HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@296676c4 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
已尝试的解决方案
根据HikariCP文档建议,maxLifeTime需设置为比数据库连接时限短数秒。我的MySQL服务器wait_timeout为28800,因此将maxLifeTime设为28740000,但问题未解决;同时调整其他HikariCP属性,同样无效果。
错误复现步骤
- 首次向数据库发起请求
- 间隔4-5分钟以上不发起任何请求
- 再次发起请求时触发该错误
关联异常现象
使用MySQL客户端连接AWS MySQL数据库时,出现类似情况:
- 执行首次查询
- 间隔5分钟以上不发起请求
- MySQL客户端陷入卡顿
环境信息
- Spring Boot 3.4.1
- MySQL Server 8.0.40(AWS托管,实例类型db.t3.micro,2核1GB内存)
- Hibernate ORM 6.6.4
相关代码
控制器代码
@Controller public class MainController { private ProductService productService; private SessionService sessionService; private CartService cartService; @Autowired public MainController(ProductService productService, SessionService sessionService, CartService cartService) { this.productService = productService; this.sessionService = sessionService; this.cartService = cartService; } @GetMapping("/") public String main(Model model, HttpServletRequest request){ HttpSession session = request.getSession(false); if(sessionService.sessionExists(session)){ Cart cart = cartService.retrieveCartFromSession(session); if(cart.getQty()>0) model.addAttribute("cart_qty",cart.getQty()); } Product product = productService.findProductById(1L); model.addAttribute("product",product); return "main"; } }
服务层代码
@Service public class ProductService { ProductRepository productRepository; @Autowired public ProductService(ProductRepository productRepository) { this.productRepository = productRepository; } @Transactional(readOnly = true) public Product findProductById(Long id){ Optional<Product> optionalProduct = productRepository.findById(id); Product product = optionalProduct.get(); return product; } }
仓库代码
public interface ProductRepository extends JpaRepository<Product,Long> {}
内容的提问来源于stack exchange,提问作者Kode1000
相关产品推荐
相关产品推荐

