Understanding Vert.x: working with SQL

Photo by Jack Anstey on Unsplash

Based on questions I regularly answer on StackOverflow, I feel that developers often misunderstand how Vert.x works with SQL databases.

Main concept that you need to understand, is that almost all RDBMS drivers work synchronously. Only exception I’m aware of currently is PostreSQL, which also explains why PostreSQL has a huge advantage in latest TechEmpowered benchmarks.

But Vert.x is asynchronous. When executing DB queries in Vert.x, you’re clearly passing a callback. So, it’s asynchronous then, no?

Well, not exactly. To understand a bit more, let’s look at queryWithParams method:

JDBCQuery is a simple object. The interesting part is the execute() method. Let’s go deeper, into AbstractJDBCAction:

It’s in the method’s name: executeBlocking()

Now, you may wonder where does this ctx comes from. It comes from JDBCClientImpl:

In the end, regular Vert.x JDBC client doesn’t have any magic in it.

It uses a simple data source under the hood: C3P0 by default, but you can also use Hikari if you like. You may want to set the pool size bigger though, as default is 15 connections.

Main goal of this implementation is simple — not to block the event loop (never block the event loop). And it’s actually pretty good for most use cases. Just don’t expect that it will do something magical with JDBC, like make it non-blocking.

Solutions Architect @Depop, author of “Hands-on Design Patterns with Kotlin” book and “Web Development with Kotlin” course

Solutions Architect @Depop, author of “Hands-on Design Patterns with Kotlin” book and “Web Development with Kotlin” course