alibaba/druid · error · UnsupportedOperationException

Not supported by DruidDataSource

Error message

Not supported by DruidDataSource

What it means

DruidXADataSource.getXAConnection(String user, String password) is deliberately unimplemented — it always throws UnsupportedOperationException. DruidXADataSource builds XA connections from its pooled physical connections, and the pool is initialized with a single fixed credential set; per-call credential XA connections are not supported. Use the no-arg getXAConnection().

Source

Thrown at core/src/main/java/com/alibaba/druid/pool/xa/DruidXADataSource.java:99

                return PGUtils.createXAConnection(physicalConn);
            case h2:
                return H2Utils.createXAConnection(h2Factory, physicalConn);
            case jtds:
                return new JtdsXAConnection(physicalConn);
            case dm:
                return new DmdbXAConnection(physicalConn);
            case oceanbase:
            case oceanbase_oracle:
                return new OceanBaseXaConnection((OceanBaseConnection) physicalConn);
            default:
                throw new SQLException("xa not support dbType : " + this.dbTypeName);

        }
    }

    @Override
    public XAConnection getXAConnection(String user, String password) throws SQLException {
        throw new UnsupportedOperationException("Not supported by DruidDataSource");
    }

}

View on GitHub (pinned to fa8dc99126)

Solutions

  1. Use the no-arg getXAConnection() and configure username/password on the DruidXADataSource itself.
  2. If per-user XA connections are required, use the database vendor's native XADataSource instead of DruidXADataSource.
  3. Check your transaction manager / connection manager configuration to ensure it calls the no-arg variant.

Example fix

// before
XAConnection xa = druidXaDataSource.getXAConnection("alice", "pw"); // throws

// after
druidXaDataSource.setUsername("alice");
druidXaDataSource.setPassword("pw");
druidXaDataSource.init();
XAConnection xa = druidXaDataSource.getXAConnection();
Defensive patterns

Strategy: validation

Validate before calling

// never call getXAConnection(user, pwd) on DruidXADataSource;
// set credentials on the datasource itself.
if (ds instanceof DruidXADataSource) {
    throw new IllegalArgumentException("DruidXADataSource only supports the no-arg getXAConnection()");
}

Type guard

public static boolean supportsCredentialedXA(DataSource ds) {
    return !(ds instanceof DruidXADataSource);
}

Try / catch

try {
    xa = ((XADataSource) ds).getXAConnection(user, pass);
} catch (UnsupportedOperationException e) {
    if (e.getMessage().contains("DruidDataSource")) {
        xa = ((XADataSource) ds).getXAConnection();
    } else throw e;
}

Prevention

When it happens

Trigger: Calling druidXADataSource.getXAConnection(user, password) directly, or via a JTA/XA framework that resolves per-subject credentials and invokes the two-argument XA method.

Common situations: App-server managed XA with per-user credentials; migrating from a vendor XADataSource (which supports the two-arg form) to DruidXADataSource without removing the per-call credentials; XAResource recovery code that reconnects with explicit credentials.

Related errors


AI-assisted analysis of alibaba/druid@fa8dc99126 (2026-08-14). Data as JSON: /api/errors/7edd04fa1ed8f1aa. Report an issue: GitHub.