Search code examples
mysqlsqltimeoutapache-commons-dbcp

Java + Apache Commons DBCP connection issue with MySQL


I have some legacy Java code that uses an Apache Commons DBCP database connection pool (v 1.2.2), to connect to a MySQL database and make a single query when a user demands it.

99% of the time, the query is executed fine and the response from the MySQL db is instantaneous and correct.

Recently I've noticed some strange behavior: the query hangs indefinitely.

This is the event trace:

Java: get connection from DBCP pool
MySQL: connection is established from <Java machine IP>

      # User@Host: <java host>[<java ip>]  Id: <...>
      # Query_time: 0.000037  Lock_time: 0.000000 Rows_sent: 0
      Rows_examined: 0
      SET timestamp=1425575615;
      SET NAMES utf8;

Java: prepare statement
Java: statement.execute()
tcpdump: I can see packets transferred between Java machine and MySQL
MySQL: no query
Java: hangs (until the MySQL connection timeout kills the connection, then I get an EOF exception - expected)

When it works, I can see the query executing on MySQL after the "SET NAMES utf8" step.

I tried a bunch of DBCP options:

  • Eviction settings
  • Abandoned connection settings
  • Validation query

The version of DBCP is so old that the validationQueryTimeout parameter can't be set. Also, I cannot figure out how to set testOnCreate.

Any ideas?

Workarounds: The only solutions I have are to set a tighter timeout in MySQL to kill these hung connections, or to restart the MySQL server (which makes everything work again).

Thanks.


Solution

  • I had never investigated problems with the MySQL versions. The Java code played nice with older MySQL (5.5 and less) and had this mystery bug with MySQL 5.6+. Upgrading the JDBC driver to the latest fixed the issue.