How to install DB2 Express-C on Docker and set up the JDBC connection properties

Imagine having a tool that can automatically detect JPA and Hibernate performance issues. Wouldn’t that be just awesome?

Well, Hypersistence Optimizer is that tool! And it works with Spring Boot, Spring Framework, Jakarta EE, Java EE, Quarkus, or Play Framework.

So, enjoy spending your time on the things you love rather than fixing performance issues in your production system on a Saturday night!

Introduction

While developing Hibernate, I need to test the code base against a plethora of relational database systems: Oracle, SQL Server, PostgreSQL, MySQL, MariaDB, Informix, and of course DB2.

However, having all these databases installed on my system is far from ideal, so I rely a lot on Docker for this task. In this article, I’m going to show how easily you can install DB2 on Docker and set up the JDBC connection so that you can run Hibernate tests on DB2.

Installing the DB2 Docker container

Nowadays, all major RDBMS providers offer official Docker images on DockerHub, and IBM is no different. The Express-C edition can be fetched automatically during Docker container set up.

To create a new DB2 Docker container on Windows, you need to run the following command:

docker run ^
 --name db2 ^
 -p 50000:50000 ^
 -e DB2INST1_PASSWORD=db2inst1-pwd ^
 -e LICENSE=accept ^
 -d ibmcom/db2express-c db2start

On Linux, you need to use the \ multiline separator:

docker run \
 --name db2 \
 -p 50000:50000 \
 -e DB2INST1_PASSWORD=db2inst1-pwd \
 -e LICENSE=accept \
 -d ibmcom/db2express-c db2start

Now, you can see the newly installed Docker container by running docker ps -as

docker ps -as
CONTAINER ID  IMAGE                COMMAND                  CREATED       STATUS        PORTS                              NAMES  SIZE
731eee777310  ibmcom/db2express-c  "/entrypoint.sh db..."   2 minutes ago Up 2 minutes  22/tcp, 0.0.0.0:50000->50000/tcp   db2    3.64 MB (virtual 1.71 GB)

When you want to stop DB2, you can run:

docker stop db2

To start it again, you have to run the following command:

docker start db2

Setting up the DB2 Docker container

To set up the DB2 Docker container, you need to open a bash terminal inside the DB2 container:

docker exec -i -t db2 /bin/bash

Now, you need to switch to the db2inst1 Linux user which is set up to run DB2:

su - db2inst1

We are going to create a new database called hiber8. Strangely, DB2 limits the database name length to 8 bytes only.

db2 create db hibern8

Setting up the JDBC conection

As I explained in this article, you can now get the DB2 JDBC Driver from Maven Central using the following dependency:

⠀<dependency>
⠀    <groupId>com.ibm.db2</groupId>
⠀   <artifactId>jcc</artifactId>
⠀   <version>${db2.version}</version>
⠀</dependency>

To connect to the newly installed DB2 Docker container, you need the following connection properties:

db2 : [
    'db.dialect' : 'org.hibernate.dialect.DB2Dialect',
    'jdbc.driver': 'com.ibm.db2.jcc.DB2Driver',
    'jdbc.user'  : 'db2inst1',
    'jdbc.pass'  : 'db2inst1-pwd',
    'jdbc.url'   : 'jdbc:db2://127.0.0.1:50000/hibern8'
]

That’s it!

I'm running an online workshop on the 20-21 and 23-24 of November about High-Performance Java Persistence.

If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.

Conclusion

Docker makes database testing a breeze. You can have as many database systems you need set up in Docker without having to bloat your currently running operating system.

Another great advantage of having your testing databases on Docker is that you can simply copy the Docker machine file from one system to another, which is ideal when you switch to a new notebook or when you have to share the default database containers with the rest of your team.

Transactions and Concurrency Control eBook

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.