io.vertx.mysqlclient.MySQLPool.query (“”).execute is never really executed and return nothing

穿精又带淫゛_ 提交于 2020-12-13 03:32:27

问题


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.

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)

}

In buld.gradle I added

...
dependencies {
...
implementation("io.micronaut.sql:micronaut-vertx-mysql-client")  
compile 'io.vertx:vertx-lang-kotlin:3.9.4'

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?

*** edited

I am stillo stuck

I tried add verticle:

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())
}

回答1:


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()
    }
}


来源:https://stackoverflow.com/questions/64885819/io-vertx-mysqlclient-mysqlpool-query-execute-is-never-really-executed-and-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!