java - HikariPool-1 - Failed to validate connection in Spring Boot Data Jpa with mysql - Stack Overflow

I know there are already similar questions and answers on the topic. I tried all the suggestions but no

I know there are already similar questions and answers on the topic. I tried all the suggestions but nothing worked for me. I decided to post this question because my case seems slightly different.

I keep getting this error:

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.

The HikariCP documentation suggests to set the maxLifeTime value to be "several seconds shorter than any database or infrastructure imposed connection time limit". Since the mysql server wait_timeout is 28800 then I set maxLifeTime to be 28740000. It didn't fix the problem. I also tried to tweak other HikariCP properties without success.

I realized that the error repeats itself when I follow this pattern:

  1. I make the first request to the database
  2. I don't make any request for some time ( 4-5 minutes and more)
  3. I make another request and the error shows up

I also noticed that when I connect to the aws mysql database from my mysql client tool a similar scenario occurs:

  1. I perform the first query

  2. I don't make any request for some time ( 5 minutes and more )

  3. The mysql client gets stuck.....

A this point I don't know if the problem is the aws database, my ubuntu system tcp keep alive or something like that...

Some information about my environment:

  • Spring Boot 3.4.1
  • mysql server 8.0.40, running on aws (instance class db.t3.micro, vCPU 2, RAM 1 GB )
  • Hibernate ORM 6.6.4

Here's the spring jpa code I that triggers the query to fetch a product from the database with id 1.

Here's my controller:

@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";
    }

}

Here's my service:

@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;
    }
}

Here's my repo:

public interface ProductRepository extends JpaRepository<Product,Long> {}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744955704a4603180.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信