Docker Community Forums

Share and learn in the Docker community.

Cannot connect to multiple databases in the same Mysql container from JDBC (Tomcat)

I’m trying to connect to multiple databases in the same mysql container through Tomcat JDBC pooling. My composer config for the database part looks like this:

db:
  image: mysql:5.6
  container_name: mysql5-opencms
  restart: unless-stopped
  environment:
    MYSQL_ROOT_PASSWORD: ######
  volumes:
    - db-mysql5-opencms:/var/lib/mysql
  ports:
    - "3306:3306"
  command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--max_allowed_packet=67108864']

volumes:
  db-mysql5-opencms:

Tomcat is able to connect to the first two databases but get’s a timeout for any other database following these two. I tried several combinations and configurations, but it’s always the third pool that starts to fail. The very same Tomcat/JDBC configuration works fine with many databases on a non-docker server.

Resource pools : default idaba_om idaba_intern
Init. JDBC pool : opencms:default (jdbc:mysql://db:3306/opencms?characterEncoding=UTF-8)
Init. JDBC pool : opencms:idaba_om (jdbc:mysql://db:3306/idaba_om?characterEncoding=ISO8859_1)
Wait for DB : opencms:idaba_intern (jdbc:mysql://db:3306/idaba_intern), attempt 1, wait 5,000 ms.
Wait for DB : opencms:idaba_intern (jdbc:mysql://db:3306/idaba_intern), attempt 2, wait 5,000 ms.
Wait for DB : opencms:idaba_intern (jdbc:mysql://db:3306/idaba_intern), attempt 3, wait 5,000 ms.
Wait for DB : opencms:idaba_intern (jdbc:mysql://db:3306/idaba_intern), attempt 4, wait 5,000 ms.
Wait for DB : opencms:idaba_intern (jdbc:mysql://db:3306/idaba_intern), attempt 5, wait 5,000 ms.

Any idea what’s happening here?

I found the solution:

The official Tomcat docker container has no default character encoding set. Therefore every JDBC pooling connection needs the characterEncoding property set. e.g.:

jdbcUrl.params=?characterEncoding\=UTF-8

After changing that, everything works flawlessly.