InsaneProgramming

Ramblings of a Java developer and OSS fanatic

Using HA-JDBC With Spring Boot

Sometimes you want a bit more resilience in your application and not depend on a single database instance. There are different ways you can do this, but recently I’ve come across a really simple way of enabling failover and load-balancing to any Java backend using JDBC. The library is called HA-JDBC and allows you to transparently route calls to multiple datasources. It will automatically replicate all write calls (using a master-slave mechanism) and load-balance all read calls. If a datasource gets dropped, it can also rebuild a datasource when it gets back up.

I thought it would be nice to see whether I could add Spring Boot support which transparently enables HA-JDBC support. And thanks to the simplicity of Spring Boot, it wasn’t that hard.

The UUID Discussion

Those who are familiar with databases and persistence have probably had the same discussion like I have had a couple of times over the last couple of years:

Do you use numeric or String values as an UUID?

Having done a lot of distributed, multi-tenant development, my stance on this is very clear: I prefer String. Or better said, I prefer UUIDs.

Autoloading PhantomJS With Geb

When it comes to building integration tests for web applications, nothing beats Geb for me. The concise and readable syntax, together with excellent Spock support, makes testing a web application a joy. For those not acquainted with Geb, it’s a Groovy library on top of Selenium that provides a more easier API to interact with the browser from your unit tests.

Using GeoJSON With Spring Data for MongoDB and Spring Boot

In my previous articles I compared 4 frameworks commonly used in communicating with MongoDB from the JVM and found out that in that use-case, Spring Data for MongoDB was the easiest solution. However I did make the remark that it doesn’t use the GeoJSON format to store geolocation coordinates and geometries.

I tried to add GeoJSON support before, but couldn’t get the conversion to work propertly. But after some extensive searching I found out that the reason for it not working was my use of Spring Boot: its autoconfiguration for MongoDB does not support custom conversion out of the box.

A Simple Use-case Comparison of JVM Libraries for MongoDB

MongoDB is one of my favorite data stores when it comes to storing document-based JSON data. Communicating with MongoDB with JVM languages can be done in a lot of ways. I thought it would be a nice exercise to take 4 of the most popular solutions and implement a simple use case in each of those solutions. The use case: create a REST service that can get a list of cities and get the nearest city for a given city with the distance to that city.

The 4 approaches I’ll compare are using the standard MongoDB Java Driver, Jongo, Morphia and finally Spring Data for MongoDB. All the code is written in Groovy for brevity and I’ll be using Spring Boot to minimize boilerplate code to provide the REST layer.