Javalin: More elaborate tutorial for getting started with Javalin (database, logging, auth, metrics)?

Created on 9 Apr 2020  路  10Comments  路  Source: tipsy/javalin

Are there any tutorial/guides about how to use a database with Javalin?

FEATURE REQUEST INFO QUESTION TUTORIALWEBSITE

Most helpful comment

I understand and it makes sense, but I was thinking still maybe simple tutorial like here's how you add JDBI and how you have a controller that make a random select from a DB.

Or maybe someone that has an opensource app developed with Javalin has it on a Github repo somewhere.

Anyway, thanks. I'll dig more into seeing how I can setup a dependency injection framework.

All 10 comments

Real world backend API built in Kotlin

And you lost me 馃槥

Thank you, but any idea if we have something similar in Java?

Hi @danechitoaie ! Javalin has no "support" or "built in" functionality when it comes to databases. I usually use https://jdbi.org, and I've also used Hikari for connection pooling: https://github.com/brettwooldridge/HikariCP. Any DB library should work fine though.

Yes, I get this. I'm just new to java web dev and not sure where/how to setup the DB connection in the context of Javalin that's why I was looking for some guide/example. Like where do I setup this connection, then how do I pass it to the routes so that they can use it in a safe (thread safe?) etc way.

This will depend on the library you use, but JDBI instances are thread safe at least. From their docs: "Jdbi instances are thread-safe and do not own any database resources.". I recommend following the tutorial/docs they have, they're quite good. I want to avoid having a database-tutorial on the Javalin website, as it's not technically related to Javalin, and it will get outdated. You can either make the JDBI instance static, or declare it in your main class and pass it to all your controllers. Or you can use dependency injection.

I understand and it makes sense, but I was thinking still maybe simple tutorial like here's how you add JDBI and how you have a controller that make a random select from a DB.

Or maybe someone that has an opensource app developed with Javalin has it on a Github repo somewhere.

Anyway, thanks. I'll dig more into seeing how I can setup a dependency injection framework.

Javalin allows for futures to be set as the resul

you could use the vertx postgres client and use the coroutine suspending bindings [or reactive] if you want to avoid callback hell

val scope = CoroutineScope(Dispatchers.IO)
app.get("/") {
    val futureResponse = scope.future { // use the vertx postgres/mongo/cassandra client }
    it.result(futureResponse)
}

@danechitoaie

Just as an update: I intend to write a "Getting started with Javalin" which covers quite a bit more than the basic examples, but I don't know when that will be 馃

Anyway, thanks. I'll dig more into seeing how I can setup a dependency injection framework.

FYI: I'm the main contributor of https://dinject which is dagger2 like DI. DI as source code generation like Dagger2 but for server side use (lifecycle support etc). You can find examples of that with Javalin use at:
https://github.com/dinject/examples

Docs:

FYI: I'm also the main contributor of Ebean ORM - https://ebean.io ... I need to put up a full stack example somewhere but haven't done that yet.

Is this covered by Kotlin Admin Template ? Should I write that up as a tutorial?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bvicenzo picture bvicenzo  路  3Comments

joshdurbin picture joshdurbin  路  3Comments

ShikaSD picture ShikaSD  路  5Comments

TobiasWalle picture TobiasWalle  路  3Comments

maxemann96 picture maxemann96  路  5Comments