Sombriks Has A Plan



IBM DB2 quick overview

In this article we showcase a really quick and dirty db2 usage. Because.

The database for big business?

I always tought about IBM DB2 as the database for the big iron, the corporative beast holding the world economy.

On the other hand, also amazes me how little material exists showing off DB2 outside this walled garden.

This might explain why there is no official knex support for DB2.

Running a DB2 instance

In order to get a working DB2, one easy way is to go into ibm cloud and provision one.

The other option is to spin up a container.

NOTE: due to recent docker hub changes that image will vanish. Test this one while you still can.

A handy docker-compose.yml file would look like this:

# docker-compose-development.yml
# this compose spins up a db2 database for development purposes
version: "3"
services:
  db2:
    image: ibmcom/db2:11.5.8.0
    privileged: true
    expose:
      - 50000
    ports:
      - "50000:50000"
    environment:
      LICENSE: accept
      DBNAME: sample
      DB2INSTANCE: db2inst1
      DB2INST1_PASSWORD: change-me-please

Connect into it from IDE

Once (it takes time!) the container finishes to spin up, we're good to connect:

sample-connection.png

Again, it might take some time.

Connect it from the application

In this example we'll connect into it using Kotlin, a modern language running on top of the java virtual machine.

package me.sombriks

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import io.javalin.Javalin
import io.javalin.apibuilder.ApiBuilder.*
import me.sombriks.config.InitConfig
import me.sombriks.controller.TodoController
import me.sombriks.service.TodoService
import javax.sql.DataSource

fun main(args: Array<String>) {

    val dataSource: DataSource = HikariDataSource(
        HikariConfig(
            "/datasource.properties"
        )
    )

    InitConfig.initDb(dataSource)

    val service: TodoService = TodoService(dataSource)
    val controller: TodoController = TodoController(service)

    val app = Javalin.create {
        // config
    }

    app.routes {
        path("/todos") {
            get(controller::listTodos)
            post(controller::insertTodo)
            path("/{id}") {
                get(controller::findTodo)
                put(controller::updateTodo)
                delete(controller::delTodo)
            }
        }
    }

    app.start(7070)
}

This code samples HikariCP as datasource provider and Javalin as our service provider (it has a very beautiful and modern way to build java api's)

Other technologies participate too, but those two are the noteworthy.

The datasource configuration itself comes from a file from classpath (the starting '/' makes Hikari search classpath for this file) called datasource.properties:

# hikari pool properties
# https://github.com/brettwooldridge/HikariCP#gear-configuration-knobs-baby
jdbcUrl=jdbc:db2://127.0.0.1:50000/sample
driverClassName=com.ibm.db2.jcc.DB2Driver
username=db2inst1
password=change-me-please
minimumIdle=2
maximumPoolSize=10

Miscellaneous

One quirk encountered was the chose of the right jdbc driver. There are a few ones, but this is the right one.

Aside the propaganda, DB2 is... one of the database engines ever made.

One funny thing noteworthy is the db2 features the longest auto increment primary column syntax i ever saw.

Conclusion

The source code for this article can be found here.