首页 > 解决方案 > Caused by: java.net.NoRouteToHostException: Cannot assign requested address (Address not available)

问题描述

I have java application that connect to mysql on docker. when i run load test to save and update data to mysql, first it run corectly but, after a while i got the

Caused by: java.net.NoRouteToHostException: Cannot assign requested address (Address not available)

and program exit immediately.

my program connect to mysql by hibernate and i used the following code to interact with database:

 Session curentSession = sessionFactory.getCurrentSession();
        curentTransaction = curentSession.beginTransaction();
        curentSession.update(entity);
        curentTransaction.commit();

标签: javamysqlhibernatedocker

解决方案


似乎当使用getcurrentSession()休眠打开会话时,在事务提交后将其关闭,但 tcp 连接仍以“TIME_WAIT”状态打开,所以当我运行时,netstat -natp我得到了大量到 3306(mysql 端口)的连接。所以我有两个解决方案:

  1. 更改我与数据库的连接设计,该设计不会在每个事务中关闭并重新打开连接。
  2. 更改状态“time_wait”中的连接等待时间:

回声 30 > /proc/sys/net/ipv4/tcp_fin_timeout

此外,我发现在我的情况下更改tcp_fin_timeout并不是一个好的解决方案。我将DriverManagerDataSource更改为dataSourceHikariDataSource使用maximumPoolSize来管理与 mysql 数据库的连接。所以与mysql数据库的巨大连接问题消失了。;)

祝你好运


推荐阅读