How connect to PostGres with Reactive PostgreSQL Client

398 views
Skip to first unread message

Jim C

unread,
Nov 16, 2020, 7:34:36 PM11/16/20
to vert.x
I am following carefully https://vertx.io/docs/vertx-pg-client/kotlin/ and I got:

Cannot access class 'io.vertx.pgclient.PgConnectOptions'. Check your module classpath for missing or conflicting dependencies

Cannot access class 'io.vertx.sqlclient.PoolOptions'. Check your module classpath for missing or conflicting dependencies

Unresolved reference: PgPool

My whole application is nothing else than a copy from the tutorial and I can't build it

app.kt

import io.vertx.kotlin.pgclient.PgConnectOptions
import io.vertx.kotlin.sqlclient.PoolOptions


class app {

fun main(args: Array<String>) {

// Connect options
var connectOptions = PgConnectOptions(
port = 5432,
host = "the-host",
database = "the-db",
user = "user",
password = "secret")

// Pool options
var poolOptions = PoolOptions(
maxSize = 5)

// Create the client pool
var client = PgPool.pool(connectOptions, poolOptions)

// A simple query
client.query("SELECT * FROM users WHERE id='julien'").execute({ ar ->
if (ar.succeeded()) {
var result = ar.result()
println("Got ${result.size()} rows ")
} else {
println("Failure: ${ar.cause().getMessage()}")
}

// Now close the pool
client.close()
})
}
}

and build.gradle

plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.10'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
compile 'io.vertx:vertx-lang-kotlin:3.9.4'
}

Julien Viet

unread,
Nov 18, 2020, 5:53:02 AM11/18/20
to vert.x
Hi

did you add io.vertx:vertx-sql-client to your classpath ?

Julien

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/a8113f36-13a0-425b-b4df-d3b874d1c6b3n%40googlegroups.com.

Jim C

unread,
Nov 18, 2020, 8:56:24 AM11/18/20
to vert.x
As far as I understand, by adding 


dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
compile 'io.vertx:vertx-lang-kotlin:3.9.4'
}

it must be in classpath.

Here is the whole repository https://github.com/jimisdrpc/vertx-postgres

Julien Viet

unread,
Nov 19, 2020, 8:26:31 AM11/19/20
to vert.x
no it is not because the dependency is marked as optional

so you need to add it explicitly

Jim C

unread,
Nov 19, 2020, 11:51:33 AM11/19/20
to ve...@googlegroups.com
Julien, thanks. How do you such dependency is marked as optional (first time I heard the dependencies added in  dependencies {} in gradle.build are considered not mandatory for being included.
How would you add them explicitly?

You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/wBzCqPj5-xw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/CA%2BH-X49j2-EK%3DAnnUntBYM_JQcTXmGX4kRbzi6FmsrZytjM1Vg%40mail.gmail.com.

Julien Ponge

unread,
Nov 19, 2020, 12:14:45 PM11/19/20
to vert.x
You need to add it as an "implementation" dependency ("compile" is deprecated in Gradle land)

Julien Viet

unread,
Nov 19, 2020, 4:54:14 PM11/19/20
to vert.x

Jim C

unread,
Nov 26, 2020, 9:25:11 AM11/26/20
to ve...@googlegroups.com
Julien, thanks. Now I can build my small application. No issues with dependency. But I am still stuck trying to get the query executed with Vertex. It is my first time using Vertx and I want to use it in order to insert/select to Relational Databases (eg. Postgres or MySql) on a reactive approach. Well, last two years I have used Spring Data Reactive successfully and it hides the difficult making quite easy to accomplish with Spring Data Reactive similar code as we do with no reactive CRUD. This may explain why I am stuck: I lost some basic knowledge in Reactive world. I pushed again my code after your dependency suggestion and, as I told, I can compile but I never get the query really executed. Here is the repository https://github.com/jimisdrpc/vertx-postgres.
Also you will find I am struggling with same issue with MySql and Verticle in https://stackoverflow.com/questions/64885819/io-vertx-mysqlclient-mysqlpool-query-execute-is-never-really-executed-and-r/65009552. I don't care if it is MySql or PostGres as long as I can execute the query I can follow to the next step of my studies. Please, help me.

Julien Ponge

unread,
Nov 26, 2020, 12:58:31 PM11/26/20
to vert.x
How are you trying to execute the code?

I did a checkout of your project, loaded into IntelliJ and ran the main function.
Since I didn't have PostgreSQL running it failed, but the branch where ar.succeeded() is false was called.

I also see that Thomas gave you a great response on StackOverflow about how callbacks work, etc.

Jim C

unread,
Nov 26, 2020, 1:42:29 PM11/26/20
to ve...@googlegroups.com
I am debugging on IntelliJ. 

Thomas told me: "... The Reactive MySQL Client executes queries asynchronously... The callback is the piece of code invoked when the execute command completes. ". I believe I understand it. 
Also he gave this suggestion in order to "query select" after "query insert" is completed. So far I believe I understand also.

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

But the problem while debbuging I don't see "if (insert.succeded())" executed neither I see an execption neither I get one register insert in database.

Basically, if I add two breakpoints, one in each of these two lines, client.query runs and if(ar.succeeded() never is executed.

client.query("SELECT * FROM mytable").execute({ ar ->
if (ar.succeeded()) {


Reply all
Reply to author
Forward
0 new messages