I am generating a value(6 alphanumeric letters using uuid java) and check it againts database if the value already exists. If the value already exists in db, I generate new value and check the existence of the value again (and so on). In synchronous way (java), I can do this:
String voucher = this.genereatevoucher();
while(this.valuexists(voucher )){
test = this.genereatevoucher();
}
Note that this.valuexists method check if the value exists in database then return true or false. But in vertx, the common way to query a database is:
client.getConnection(res -> {
if (res.succeeded()) {
SQLConnection connection = res.result();
connection.query("SELECT * FROM some_table", res2 -> {
if (res2.succeeded()) {
ResultSet rs = res2.result();
// Do something with results
}
});
} else {
// Failed to get connection - deal with it
}
});
I can't put the code above on my method because it executed asynchronously, so my method will always return true / false (based on initial assignment). How to (possibly) do database query loop in vertx?