development

9 posts

Type safe data access with jOOQ and Kotlin

Type safe data access with jOOQ and Kotlin

jOOQ is an ORM library that sticks close to SQL, allowing you to write fast and efficient queries while providing a nice, type safe API for that. Before I started using it in my work, I was mostly relying on Spring Data + Hibernate combo. I was pretty skeptical, when I first learned about jOOQ. With it you write DDL first, create schema and tables, and then generate the classes for accessing those tables. With Hibernate you declare models in your code first, and then you can generate DDL out of them, while Spring Data can generate all the repositories for you, so in the perfect case scenario you only have to declare the models and repository interfaces. Seems like it’s almost a no-brainer, right? With Spring Data and Hibernate you write less code, so it’s less code to maintain and that’s always good. Unfortunately, in my experience that perfect case scenario was never the case. Almost in every project I had to either write JPQL or even plain SQL queries. And if you have have to write plain SQL anyway, why not embrace it and take full control of how you access the data in your DB? Let’s see how we can build a convenient and type safe data access layer with jOOQ and Kotlin.

Continue reading
How to build a good API with Kotlin

How to build a good API with Kotlin

Designing a type-safe, sane API that prevents consumers from misusing it could be crucial for further implementation of that API. Frustrated consumers, necessity for extra validations, delayed feedback and convoluted, hard to maintain code are just a few things you might have to pay with for poor design decisions during early development stages. Thankfully, Kotlin provides a plenty of tools to design a great API. Let’s have a look at how to build a good API with Kotlin to make your life and life of your API consumers easier.

Continue reading
Spring Data MongoDB MappingInstantiationException

Polymorphic fields with MongoDB and Spring Data

Depending on your needs using polymorphism might benefit different aspects of the project. For example, having a limited class hierarchy might make your code cleaner and more expressive in contrast to a single class having nullable fields or a type label. And it only seems natural to use it with document-oriented databases like MongoDB. But if you want to have a document with polymorphic fields using MongoDB and Spring Data you might face an exception similar to this: MappingInstantiationException: Failed to instantiate ...FieldType using constructor NO_CONSTRUCTOR with arguments. Let’s see why this happens and how to fix that issue properly.

Continue reading
reduce Java docker image size

How to reduce Java docker image size

If you’ve been using Java for a while, you might have noticed that starting with Java 11, Java Runtime Environment (JRE) doesn’t have a separate distribution anymore, it is only distributed as a part of Java Development Kit (JDK). As a result of this change, many official Docker images don’t offer a JRE-only image, e.g.: official openjdk images, Amazon corretto images. In my case using such an image was resulting in an app image of 414MB, where the app itself was only taking around 60MB. What a waste of space!

But fear not, as I’m going to show you how to reduce Java docker image size dramatically.

Continue reading
Using Liquibase with Kubernetes

Using Liquibase with Kubernetes

If you’re using Liquibase for database versioning with Kubernetes to deploy your app, you might have faced an issue when a migration gets stuck because Liquibase can’t acquire lock. It might look somewhat like this:

liquibase.exception.LockException: Could not acquire change log lock.  Currently locked by LockOwner ...
        at liquibase.lockservice.StandardLockService.waitForLock(StandardLockService.java:236)
        at liquibase.Liquibase.update(Liquibase.java:184)
        at liquibase.Liquibase.update(Liquibase.java:179)Code language: Properties (properties)
Continue reading