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");