Apache H2 Database driver for Go
Apache H2 is a light weight and high performance SQL Database written in Java.
It’s (almost) 100% ANSI SQL-99 compatible and it can store the data in files or in-memory. You can embed in your Java project or using with client/server pattern.
Some months ago, I worked in a “tech debt” project where a lot of business logic was coded as SQL sentences.
The goal was significantly improve the performance and scalability.
We chosen Go as programming language for its performance and flexibility balance. It’s ideal for time-to-market adn/or deadline projects.
The project has a lot of SQL sentences that we need keep them and use a very fast database a local “SQL cache” and “business logic processador”.
After we studied the state-of-art of in-memory databases we chosen H2. Its ANSI SQL compatibility and its very high speed were the key factors.
But until now, the only H2 interface for Go was the Postgres interface (pq driver).
We had to face a number of challenges when using the Postgres driver. The main one was not being able to get the correct error messages when executing some SQL statements. Its debugging process were tricky.
We had the need to have a “native” Go SQL Driver for the H2 Server protocol.
Let’s do it
It have been my first Go SQL driver.
I have developed for Go version 1.15 and I implemented the new recommended
For its development I had need to study the Java source code and some research work, inspecting traffic between a JDBC client and the H2 server using Wireshark.
To develop and to serve the package, I’ll use GitHub. I also use the new GitHub feature, Actions, for CI. Testing stage by the moment.
The result is a “MVP” Go SQL driver tagging as version 0.5.0 (in the halfway) that support the H2 1.4.200 (current stable) or above version.
Hello world, H2!
The following example demonstrate how to use the H2 SQL Go driver.
Here is the respository: https://github.com/jmrobles/h2go
- Support of basic types: string, float, double, short, long, date, time, timestamp, timezones, …
- Basic testing with CI (GitHub Action)
- Prepared Statements
- Parametrized DML statements(parameter symbol: ?)
- Transaction support.
- Logging with different levels.
- Implementation of complex types: UUID, JSON, Blob, etc.
- MultiResultSet support.
- Older Go versions compatibility.
- Other H2 version support.
- More tests.
- Benchmark tests.
The development of the driver has been very didactic. The most difficult part was analyzing and implementing the protocol due to the lack of internal documentation.
I hope this driver helps other colleagues that we need to use H2 in theirs Go projects.
If you want to contribute it, PRs are welcome! :)
Extract JSON from captured HTTP traffic with Wireshark
Get JSON objects from HTTP network traffic easily
Please, if you liked it, give it a round of applause. And if you want to know more about DevOps, Kubernetes, Docker, etc … follow me :)