Saman Salehi
Saman Salehi

Reputation: 1034

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();

Upvotes: 1

Views: 2290

Answers (1)

Saman Salehi
Saman Salehi

Reputation: 1034

It seem that when hibernate open session with getcurrentSession(), close it after transaction commit, but the tcp connection is still open with status of "TIME_WAIT" so when i run netstat -natp i got the huge number of connection to 3306 (mysql port). so i have two solution:

  1. change the design of my connection to database that not close and reopen connection in each transaction.
  2. change the time of connection wait in status "time_wait" with:

echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout

In addition i found that change the tcp_fin_timeout is not good solution in my case. i change the dataSource from DriverManagerDataSource to HikariDataSource and used the maximumPoolSize to manage the connection to mysql database. so the problem of huge connection to mysql database gone away. ;)

good luck

Upvotes: 2

Related Questions