io.vertx.mysqlclient.MySQLPool.query(“").execute从未真正执行过,什么也不返回 [英] io.vertx.mysqlclient.MySQLPool.query ("").execute is never really executed and return nothing
问题描述
我正在尝试一个非常简单的尝试,第一次使用vertx插入MySql并从中进行选择.对我来说,我可以构建和调试该应用程序很有趣,但是我从来没有成功或失败过.
I am trying a very simple tentative to insert and select to/from MySql using vertx for the first time. It is funny to me that I can build and debug the application but I never get either ar.succeeded or failed.
如何执行插入并选择波纹管?我是否必须以某种方式封装client.query并订阅它?我完全被困住了.我在互联网上看到的所有示例都遵循这种方法.
How to execute the insert and select bellow? Do I have to enclose somehow client.query and subscribe it? I am complete stuck. All examples on internet I see follow this approach.
package com.mybank
import io.vertx.kotlin.mysqlclient.mySQLConnectOptionsOf
import io.vertx.kotlin.sqlclient.poolOptionsOf
import io.vertx.mysqlclient.MySQLClient
import io.vertx.mysqlclient.MySQLPool
fun main(args: Array<String>) {
var connectOptions = mySQLConnectOptionsOf(database = "mydb",
host = "127.0.0.1",
password = "q", port = 6603,
user = "root")
var poolOptions = poolOptionsOf(maxSize = 5)
var client = MySQLPool.pool(connectOptions, poolOptions)
client.query("INSERT INTO mytable('id','name') VALUES ('2','Jimis2')").execute { ar ->
if (ar.succeeded()) {
var rows = ar.result()
var lastInsertId = rows.property(MySQLClient.LAST_INSERTED_ID)
println("Last inserted id is: ${lastInsertId}")
} else {
println("Failure: ${ar.cause().printStackTrace()}")
}
}
client.query("SELECT * FROM mytable WHERE id=1").execute { ar ->
if (ar.succeeded()) {
var result = ar.result()
println("Got ${result.size()} rows ")
} else {
println("Failure: ${ar.cause().stackTrace}")
}
// Now close the pool
client.close()
}
Thread.sleep(5000)
}
我在buld.gradle中添加了
In buld.gradle I added
...
dependencies {
...
implementation("io.micronaut.sql:micronaut-vertx-mysql-client")
compile 'io.vertx:vertx-lang-kotlin:3.9.4'
当我调试并在"if(ar.succeeded()){"中放置断点时,它永远不会达到这条线.如果将断点放在client.query(...).execute上方一行,则运行该断点;如果更深一层地调试,则可以看到已执行了execute方法,但"if(ar.succeeded())"行却没有.
When I debug and place a break point in "if (ar.succeeded()) {" it never reaches this line. If I place a breakpoint one line above client.query(...).execute it runs and If debug one level deeper I can see execute method is evoked but the line "if (ar.succeeded()) isn't.
并且完全没有显示错误.我照着指南做.我还有其他概念吗?我必须以某种方式订阅吗?如果可以,怎么办?
And there is no error showed at all. I followed the guide as it is. Is there any other concept I have lost? Do I have to subscribe somehow? If so, how?
***已编辑
我仍然困住了
我尝试添加垂直线:
package com.mybank
import io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.kotlin.mysqlclient.mySQLConnectOptionsOf
import io.vertx.kotlin.sqlclient.poolOptionsOf
import io.vertx.mysqlclient.MySQLClient
import io.vertx.mysqlclient.MySQLPool
internal class BaseVerticle : AbstractVerticle() {
override fun start() {
var connectOptions = mySQLConnectOptionsOf(database = "mydb",
host = "127.0.0.1",
password = "password1!", port = 6603,
user = "root")
var poolOptions = poolOptionsOf(maxSize = 5)
var client = MySQLPool.pool(vertx, connectOptions, poolOptions)
client.query("INSERT INTO mytable('id','name') VALUES ('2','Jimis2')").execute { ar ->
if (ar.succeeded()) {
var rows = ar.result()
var lastInsertId = rows.property(MySQLClient.LAST_INSERTED_ID)
println("Last inserted id is: ${lastInsertId}")
} else {
println("Failure: ${ar.cause().printStackTrace()}")
}
}
client.query("SELECT * FROM mytable WHERE id=1").execute { ar ->
if (ar.succeeded()) {
var result = ar.result()
println("Got ${result.size()} rows ")
} else {
println("Failure: ${ar.cause().stackTrace}")
}
// Now close the pool
client.close()
}
}
override fun stop() {
println("BasicVerticle stopped")
}
}
val vertx = Vertx.vertx()
fun main() {
//val vertx = Vertx.vertx()
vertx.deployVerticle(BaseVerticle())
}
推荐答案
Reactive MySQL Client异步执行查询.因此,如果要阅读已插入的行,则必须在回调中完成该操作:
The Reactive MySQL Client executes queries asynchronously. So if you want to read the lines you have inserted, you must do it in the callback:
client.query("INSERT INTO mytable('id','name') VALUES ('2','Jimis2')").execute { insert ->
if (insert.succeeded()) {
var rows = insert.result()
var lastInsertId = rows.property(MySQLClient.LAST_INSERTED_ID)
println("Last inserted id is: ${lastInsertId}")
client.query("SELECT * FROM mytable WHERE id=1").execute { select ->
if (select.succeeded()) {
var result = select.result()
println("Got ${result.size()} rows ")
} else {
select.cause().printStackTrace()
}
client.close()
}
} else {
insert.cause().printStackTrace()
client.close()
}
}
这篇关于io.vertx.mysqlclient.MySQLPool.query(“").execute从未真正执行过,什么也不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!