codelessgenie blog

Wildfly 10 Datasource Connection Error: How to Fix 'Invalid Connection' (WFLYJCA0040/WFLYJCA0047) with MySQL

Wildfly (formerly JBoss) is a popular open-source application server used to deploy and run Java EE applications. A critical component of any Wildfly setup is the datasource, which manages database connections for your application. However, configuring datasources can sometimes lead to frustrating errors, especially when connecting to MySQL. Two common errors you might encounter are:

  • WFLYJCA0040: failed to invoke operation: WFLYJCA0047: Connection is invalid

These errors indicate issues with the database connection pool, often stemming from misconfiguration, network problems, or compatibility issues. In this blog, we’ll break down the causes of these errors and provide a step-by-step guide to resolve them, ensuring your Wildfly 10 server connects seamlessly to MySQL.

2026-01

Table of Contents#

  1. Understanding WFLYJCA0040 and WFLYJCA0047 Errors
  2. Common Causes of 'Invalid Connection' Errors
  3. Step-by-Step Troubleshooting & Fixes
  4. Testing the Datasource Connection
  5. Example: Correct Datasource Configuration
  6. References

Understanding WFLYJCA0040 and WFLYJCA0047 Errors#

Before diving into fixes, let’s decode the errors:

  • WFLYJCA0040: This error is logged when Wildfly’s Jakarta Connector Architecture (JCA) subsystem fails to invoke an operation (e.g., testing the datasource connection).
  • WFLYJCA0047: A sub-error indicating the root cause: the database connection is invalid. This can mean the connection is broken, timed out, or rejected by the database.

Together, they signal a problem with the datasource configuration or the underlying network/database setup.

Common Causes of 'Invalid Connection' Errors#

These errors typically arise from one or more of the following issues:

  • Incorrect JDBC URL: Malformed URL (e.g., wrong host, port, or database name).
  • Missing/Incorrect JDBC Driver: MySQL Connector/J not deployed or incompatible with Wildfly 10.
  • Invalid Credentials: Username/password mismatch with MySQL user permissions.
  • MySQL Server Unavailable: MySQL is down, or the host/port is unreachable.
  • Connection Pool Misconfiguration: Pool settings (e.g., timeouts, max connections) causing stale/invalid connections.
  • MySQL Max Connections Reached: MySQL’s max_connections limit is hit, blocking new connections.
  • Firewall/Network Issues: Port 3306 (default MySQL port) is blocked, preventing Wildfly from reaching MySQL.
  • Driver Compatibility: Using a newer MySQL Connector/J (e.g., 8.x) with Wildfly 10 (which expects older drivers).

Step-by-Step Troubleshooting & Fixes#

3.1 Verify JDBC URL Format#

The JDBC URL is critical for establishing a connection. A typical MySQL JDBC URL format is:

jdbc:mysql://<host>:<port>/<database_name>?<connection_parameters>

Common mistakes to avoid:

  • Omitting the port (default: 3306, but required if non-default).
  • Typos in the database name or host (e.g., localhost vs. 127.0.0.1).
  • Missing required parameters (e.g., useSSL=false for development, serverTimezone=UTC to avoid timezone warnings).

Example Valid URL:

jdbc:mysql://mysql-server:3306/mydb?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true

To check your URL in Wildfly:

  1. Open standalone/configuration/standalone.xml (or domain.xml for domain mode).
  2. Search for your datasource (e.g., <datasource jndi-name="java:jboss/datasources/MyDS" ...>).
  3. Verify the <connection-url> value matches the format above.

3.2 Ensure MySQL JDBC Driver is Deployed#

Wildfly requires the MySQL Connector/J driver to communicate with MySQL. Without it, connections fail.

Step 1: Download the Correct Driver#

Wildfly 10 is compatible with MySQL Connector/J 5.1.x (newer versions like 8.x may cause classloading issues). Download it from the MySQL Archives.

Step 2: Deploy the Driver to Wildfly#

There are two ways to deploy the driver:

  1. Create a module directory:
    mkdir -p $WILDFLY_HOME/modules/system/layers/base/com/mysql/main
  2. Copy the downloaded JAR (e.g., mysql-connector-java-5.1.49.jar) to this directory.
  3. Create a module.xml file in the same directory with:
    <?xml version="1.0" encoding="UTF-8"?>
    <module name="com.mysql" xmlns="urn:jboss:module:1.3">
      <resources>
        <resource-root path="mysql-connector-java-5.1.49.jar"/> <!-- Match JAR filename -->
      </resources>
      <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
      </dependencies>
    </module>
  4. Register the driver in standalone.xml under the datasources subsystem:
    <drivers>
      <driver name="mysql" module="com.mysql">
        <driver-class>com.mysql.jdbc.Driver</driver-class> <!-- Older driver class for 5.1.x -->
      </driver>
    </drivers>
Option 2: Deploy as a Deployment (Simpler)#

Copy the MySQL Connector/J JAR to $WILDFLY_HOME/standalone/deployments/. Wildfly will auto-deploy it, and you can reference it in the datasource as driver-name="mysql-connector-java-5.1.49.jar".

3.3 Validate Database Credentials#

Incorrect credentials are a frequent culprit. Ensure the username/password in Wildfly matches the MySQL user:

Step 1: Check Wildfly Credentials#

In standalone.xml, locate your datasource and verify the security block:

<security>
  <user-name>myuser</user-name>
  <password>mypassword</password>
</security>

Step 2: Verify MySQL User Permissions#

Log into MySQL and check if the user exists and has access to the database:

-- List users and their hosts
SELECT user, host FROM mysql.user;
 
-- Check permissions for 'myuser' (replace 'myuser' and 'wildfly-server-ip')
SHOW GRANTS FOR 'myuser'@'wildfly-server-ip';

Fix: If the user lacks permissions, grant access:

GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'wildfly-server-ip' IDENTIFIED BY 'mypassword';
FLUSH PRIVILEGES;

3.4 Check MySQL Server Availability#

If MySQL is down or unreachable, Wildfly cannot connect.

Step 1: Verify MySQL is Running#

On the MySQL server:

# Check status (systemd systems)
systemctl status mysql.service
 
# Or for SysVinit
service mysql status

Step 2: Test Network Connectivity#

From the Wildfly server, test if MySQL’s host/port is reachable:

# Ping the MySQL host (if ICMP is allowed)
ping mysql-server
 
# Test TCP connectivity to port 3306
telnet mysql-server 3306

If telnet fails, MySQL is either down or the port is blocked (see Section 3.7).

3.5 Adjust Connection Pool Settings#

Wildfly’s connection pool manages database connections. Misconfigured pools can lead to stale/invalid connections.

Key Pool Settings to Adjust (in standalone.xml):#

<datasource jndi-name="java:jboss/datasources/MyDS" pool-name="MyDS" enabled="true">
  <connection-url>jdbc:mysql://mysql-server:3306/mydb?useSSL=false</connection-url>
  <driver>mysql</driver>
  <pool>
    <min-pool-size>5</min-pool-size>
    <max-pool-size>20</max-pool-size> <!-- Avoid setting too high -->
    <blocking-timeout-wait-millis>30000</blocking-timeout-wait-millis> <!-- 30 seconds -->
    <idle-timeout-minutes>5</idle-timeout-minutes> <!-- Close idle connections after 5 mins -->
  </pool>
  <security>
    <user-name>myuser</user-name>
    <password>mypassword</password>
  </security>
</datasource>

Critical settings:

  • idle-timeout-minutes: Close idle connections before MySQL’s wait_timeout (default MySQL wait_timeout is 8 hours; set Wildfly’s idle timeout lower, e.g., 5–10 minutes).
  • max-pool-size: Avoid exceeding MySQL’s max_connections (see Section 3.6).
  • blocking-timeout-wait-millis: Time to wait for a connection from the pool (30–60 seconds is reasonable).

3.6 Resolve MySQL Max Connections Limits#

MySQL has a max_connections variable (default: 151) that limits concurrent connections. If this is reached, Wildfly cannot obtain new connections.

Step 1: Check Current MySQL Connections#

SHOW GLOBAL STATUS LIKE 'Threads_connected';
SHOW VARIABLES LIKE 'max_connections';

Step 2: Increase max_connections (Temporarily)#

SET GLOBAL max_connections = 500; -- Adjust based on your needs

Step 3: Persist the Change (Permanent)#

Edit my.cnf (or my.ini on Windows) and add:

[mysqld]
max_connections = 500

Restart MySQL for changes to take effect.

3.7 Fix Firewall/Network Blockages#

Firewalls (e.g., ufw, iptables) often block port 3306.

Step 1: Open Port 3306 on the MySQL Server#

For ufw (Ubuntu/Debian):

sudo ufw allow 3306/tcp
sudo ufw reload

For iptables (RHEL/CentOS):

sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
sudo service iptables save

Step 2: Verify with telnet#

From Wildfly, re-run telnet mysql-server 3306. You should see a MySQL welcome message.

3.8 Ensure Driver Compatibility#

Wildfly 10 uses older APIs (e.g., com.mysql.jdbc.Driver), while MySQL Connector/J 8.x uses com.mysql.cj.jdbc.Driver. Using 8.x may cause classloading errors.

Fix: Use MySQL Connector/J 5.1.x (e.g., 5.1.49) for Wildfly 10. Download it from the MySQL Archives.

Testing the Datasource Connection#

After making changes, test the datasource to confirm the fix:

via Wildfly Admin Console#

  1. Access the console at http://wildfly-server:9990 (enable it first with ./standalone.sh -bmanagement 0.0.0.0).
  2. Navigate to Datasources & Drivers → Select your datasource → Click Test Connection.

via Wildfly CLI#

Connect to the CLI and run:

/subsystem=datasources/data-source=MyDS:test-connection-in-pool()

If successful, you’ll see:
{"outcome" => "success"}

Example: Correct Datasource Configuration#

Here’s a complete, working datasource configuration for Wildfly 10 and MySQL:

<datasources>
  <datasource jndi-name="java:jboss/datasources/MyDS" pool-name="MyDS" enabled="true" use-java-context="true">
    <connection-url>jdbc:mysql://mysql-server:3306/mydb?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true</connection-url>
    <driver>mysql</driver>
    <pool>
      <min-pool-size>5</min-pool-size>
      <max-pool-size>20</max-pool-size>
      <blocking-timeout-wait-millis>30000</blocking-timeout-wait-millis>
      <idle-timeout-minutes>5</idle-timeout-minutes>
    </pool>
    <security>
      <user-name>myuser</user-name>
      <password>mypassword</password>
    </security>
    <validation>
      <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
      <background-validation>true</background-validation>
      <background-validation-millis>60000</background-validation-millis> <!-- Validate connections every 60s -->
    </validation>
  </datasource>
  <drivers>
    <driver name="mysql" module="com.mysql">
      <driver-class>com.mysql.jdbc.Driver</driver-class>
    </driver>
  </drivers>
</datasources>

References#

By following these steps, you should resolve the WFLYJCA0040/WFLYJCA0047 errors and ensure a stable connection between Wildfly 10 and MySQL. Let us know in the comments if you encountered other edge cases!