vertx的数据库驱动建立与mysql的数据连接时会复用连接,执行完PreparedStatement后不会主动关闭,而且目前也没有提供手工关闭接口,因此如果大量执行预编译sql的话会造成资源泄漏,报错Can't create more than max_prepared_stmt_count statements,造成无法再执行任何语句。
要解决这个问题,要么把预编译的sql转换成普通sql,要么通过配置缓存PreparedStatement来解决。
缓存配置默认是不设置的,需要代码里显示声明,代码如下
MySQLConnectOptions connectOptions = new MySQLConnectOptions()
//如果不设置缓存的话会导致大量创建PreparedStatement造成资源泄漏
.setCachePreparedStatements(true)
.setPreparedStatementCacheMaxSize(100)
.setConnectTimeout(5_000)
.setPort(port)
.setHost(host)
.setDatabase(database)
.setUser(user)
.setPassword(pass)
.setCharset("utf8mb4")
.addProperty("useUnicode", "true")
.addProperty("allowMultiQueries", "true")
.addProperty("serverTimezone", "Hongkong");