You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarkus原生编译动态连接MySQL报错的排查与解决

Quarkus原生编译后MySQL动态连接失败的解决方法

问题描述

Quarkus项目原生编译后,通过MySQLDriver动态创建MySQL连接池的逻辑无法正常运行,抛出以下错误:

java.util.ServiceConfigurationError: No implementations of interface io.vertx.sqlclient.spi.Driver found that accept connection options io.vertx.mysqlclient.MySQLConnectOptions@58a8452

问题代码如下:

@Path("/hello")
public class GreetingResource {

    @Inject
    Vertx vertx;

    private Pool getPool() {
        var options = new MySQLConnectOptions()
                .setDatabase("mysql")
                .setHost("localhost")
                .setSslMode(SslMode.DISABLED)
                .setPort(3306)
                .setUser("root")
                .setPassword("password");

        return Pool.pool(vertx, options, new PoolOptions());
    }


    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Uni<String> hello() {
        return getPool().query("toto").execute().onItem().transform(unused -> "Hello, world!");
    }
}

尝试过的无效修复方案

  • 为类添加反射与代理注册注解:
@RegisterForReflection(targets = {MySQLDriver.class, MySQLConnectOptions.class, MySQLPool.class})
@RegisterForProxy(targets = {Driver.class})
  • 在pom.xml中配置原生编译运行时初始化参数:
<quarkus.native.additional-build-args>--initialize-at-run-time=io.vertx.mysqlclient.MySQLConnectOptions\,io.vertx.mysqlclient.spi.MySQLDriver\,io.vertx.sqlclient.spi.Driver\,io.vertx.mutiny.mysqlclient.MySQLPool</quarkus.native.additional-build-args>
  • 在src/main/resources目录下添加reflect-config.json反射配置文件:
[
  {
    "name": "io.vertx.mysqlclient.MySQLConnectOptions",
    "allDeclaredConstructors" : true,
    "allPublicConstructors" : true,
    "allDeclaredMethods" : true,
    "allPublicMethods" : true,
    "allDeclaredFields" : true,
    "allPublicFields" : true
  },
  {
    "name": "io.vertx.mysqlclient.spi.MySQLDriver",
    "allDeclaredConstructors" : true,
    "allPublicConstructors" : true,
    "allDeclaredMethods" : true,
    "allPublicMethods" : true,
    "allDeclaredFields" : true,
    "allPublicFields" : true
  },
  {
    "name": "io.vertx.sqlclient.spi.Driver",
    "allDeclaredConstructors" : true,
    "allPublicConstructors" : true,
    "allDeclaredMethods" : true,
    "allPublicMethods" : true,
    "allDeclaredFields" : true,
    "allPublicFields" : true
  },
  {
    "name": "io.vertx.mutiny.mysqlclient.MySQLPool",
    "allDeclaredConstructors" : true,
    "allPublicConstructors" : true,
    "allDeclaredMethods" : true,
    "allPublicMethods" : true,
    "allDeclaredFields" : true,
    "allPublicFields" : true
  }
]
  • 编写自定义BuildSteps类注册代理:
public class BuildSteps {

    @BuildStep
    NativeImageProxyDefinitionBuildItem proxies() {
        return new NativeImageProxyDefinitionBuildItem("io.vertx.mysqlclient.MySQLConnectOptions",
                "io.vertx.mysqlclient.spi.MySQLDriver", "io.vertx.sqlclient.spi.Driver", "io.vertx.mutiny.mysqlclient.MySQLPool");
    }
}

有效解决方案

重构连接池创建逻辑,使用MySQLBuilder替代Pool.pool()方法,修复后的代码如下:

private Pool getPool() {
    var options = new MySQLConnectOptions()
            .setDatabase("mysql")
            .setHost("localhost")
            .setSslMode(SslMode.DISABLED)
            .setPort(3306)
            .setUser("root")
            .setPassword("password");

    return MySQLBuilder.pool()
            .connectingTo(options)
            .using(vertx)
            .build();
}

内容的提问来源于stack exchange,提问作者Simon LEDUNOIS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 01:04:55