Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

Browser testing is essential if you have a website or web applications that users interact with. Manual testing can be very helpful to an extent, but given the multiple browsers available, not to mention versions and operating system, testing everything manually becomes time-consuming and repetitive.

To help automate this process, Selenium is a popular choice for developers, as an open-source tool with a large and active community. What's more, we can further scale our automation testing by running on theLambdaTest cloud-based testing platform.

Read more through our step-by-step tutorial on how to set up Selenium tests with Java and run them on LambdaTest:

>> Automated Browser Testing With Selenium

Partner – Orkes – NPI EA (cat=Java)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

1. Introduction

MariaDB is an alternative database engine to MySQL, written by the original developers of MySQL and compatible as a drop-in replacement. In this article, we’re going to explore the MariaDB4j library that allows us to treat MariaDB as if it was an embedded database within our Java application.

2. How Does It Work?

MariaDB4j works by having packaged ready-to-run native versions of the MariaDB database engine. It does this with a number of different dependencies. The majority of the functionality exists in the ch.vorburger.mariaDB4j:mariaDB4j-core artifact. There are then additional artifacts that each contain a copy of the MariaDB database engine that’s ready for use. Versions of this are available for Windows, Linux, and macOS.

When using the MariaDB4j library, it will automatically determine the current operating system and then attempt to extract the MariaDB database engine into a temporary working directory. This will then be executed as any other application. Once running, we can connect to it exactly the same as if it were a full standalone database.

The MariaDB4j library is then responsible for extracting, configuring, starting and stopping the database engine.

This is an alternative to solutions such as Testcontainers, which work by starting the database as a Docker container. This means that we’re able to use this database even in environments where Docker either isn’t available or may be less desirable to use.

3. Command Line Use

The first way we can use MariaDB4j is as a command-line wrapper. This only supports Windows and Linux but provides an easy way to run our database without needing a full install.

In order to use this, we need to download the uber mariaDB4j-app.jar JAR file that’s provided. We can find this in Maven Central. The latest available version is 3.1.0 at the time of writing. This contains the MariaDB4j core code and also database engines for both Windows and Linux.

Once we download this, we can run the JAR file like any other Java application:

$ java -jar mariaDB4j-app-3.1.0.jar
.....
2024-11-08T07:38:47.867Z  INFO 40 --- [pool-9-thread-2] ch.vorburger.exec.ManagedProcess         : mariadbd: 2024-11-08  7:38:47 0 [Note] /tmp/MariaDB4j/base/bin/mariadbd: ready for connections.
2024-11-08T07:38:47.868Z  INFO 40 --- [pool-9-thread-2] ch.vorburger.exec.ManagedProcess         : mariadbd: Version: '10.11.5-MariaDB'  socket: '/tmp/MariaDB4j.3307.sock'  port: 3307  MariaDB Server
2024-11-08T07:38:47.924Z  INFO 40 --- [           main] ch.vorburger.mariadb4j.DB                : Database startup complete.
2024-11-08T07:38:48.454Z  INFO 40 --- [           main] c.v.m.s.boot.MariaDB4jApplication        : Started MariaDB4jApplication in 6.589 seconds (process running for 8.281)

Hit Enter to quit...

Once this has finished loading, our database is ready for use.

If needed, we can also customize the execution with some system properties. The most useful of these is mariaDB4j.port, which will allow us to control the port number that the database listens on. We can also use mariaDB4j.baseDir to control where the MariaDB installation will be unpacked and mariaDB4j.dataDir to control where the data files will be stored:

$ java -DmariaDB4j.port=13306 -DmariaDB4j.baseDir=/tmp/db -DmariaDB4j.dataDir=/tmp/data -jar mariaDB4j-app-3.1.0.jar

4. Using From Java

More likely, we want to be able to run MariaDB automatically from within our application.

In order to do this, we need to add the main MariaDB4j dependency to our application. If we’re using Maven, we can include this dependency in our pom.xml file:

<dependency>
    <groupId>ch.vorburger.mariaDB4j</groupId>
    <artifactId>mariaDB4j</artifactId>
    <version>3.1.0</version>
</dependency>

At this point, we’re ready to start using it in our application. By default, this will include the dependencies needed for Windows and Linux, running the default supported version of MariaDB – 10.11.5. There’s also a macOS dependency that can be added. However, this doesn’t support the latest version yet and will need more work to run.

Once we install the dependency, we can start using it. The simplest way is to call DB.newEmbeddedDB(), providing a port number to listen on:

DB db = DB.newEmbeddedDB(0);

Using 0 here will cause the system to find an unused port. Using any real port number will cause it to use that instead.

Once we’ve done this we can start the database with db.start() and stop it with db.stop():

try {
    db.start();
} finally {
    db.stop();
}

At this point, our database is running and ready for use.

4.1. Configuring Our Database

Instead of simply running the database with default settings, we can also provide some amount of configuration. This is done using a DBConfigurationBuilder to generate the configuration for our database. For example, if we want to specify the port, data directory, and base directory for our database, we run:

DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder();
config.setPort(13306);
config.setDataDir("/tmp/data");
config.setBaseDir("/tmp/db");

DB db = DB.newEmbeddedDB(config.build());

Once we’ve created our DB instance, we can then use it exactly as before.

This also lets us control the version of the database used. We can specify the version using the setDatabaseVersion() method on our DBConfigurationBuilder object. However, if we do this, our application must include the correct dependencies for the database version.

4.2. Interacting With the Database

Once we’ve got the database running, we now need to be able to interact with it. MariaDB4j offers a few different ways to achieve this.

The simplest way is to simply run commands as if we were typing them into the MariaDB terminal. This can be done with the db.run() command. Using this command, we can achieve anything that the database terminal can – including running SQL commands but also more admin-focused tasks:

db.run("show databases");

Note, however, that nothing is returned from this. The command prints the output to the standard Java output instead:

[2024-11-08 09:37:40,470]-[main] INFO  ch.vorburger.mariadb4j.DB - Running a command: show databases
[2024-11-08 09:37:40,470]-[main] INFO  ch.vorburger.exec.ManagedProcess - Starting Program [/tmp/MariaDB4j/base/bin/mariadb, --default-character-set=utf8, --socket=/tmp/MariaDB4j.40315.sock] (in working directory /tmp/MariaDB4j/base)
[2024-11-08 09:37:40,535]-[CommonsExecStreamPumper-pool-17-thread-2] INFO  ch.vorburger.exec.ManagedProcess - mariadb: Database
[2024-11-08 09:37:40,536]-[CommonsExecStreamPumper-pool-17-thread-2] INFO  ch.vorburger.exec.ManagedProcess - mariadb: information_schema
[2024-11-08 09:37:40,536]-[CommonsExecStreamPumper-pool-17-thread-2] INFO  ch.vorburger.exec.ManagedProcess - mariadb: mysql
[2024-11-08 09:37:40,536]-[CommonsExecStreamPumper-pool-17-thread-2] INFO  ch.vorburger.exec.ManagedProcess - mariadb: performance_schema
[2024-11-08 09:37:40,536]-[CommonsExecStreamPumper-pool-17-thread-2] INFO  ch.vorburger.exec.ManagedProcess - mariadb: sys
[2024-11-08 09:37:40,536]-[CommonsExecStreamPumper-pool-17-thread-2] INFO  ch.vorburger.exec.ManagedProcess - mariadb: test
[2024-11-08 09:37:40,536]-[CommonsExecStreamPumper-pool-17-thread-2] INFO  ch.vorburger.exec.ManagedProcess - mariadb: testing
[2024-11-08 09:37:40,538]-[CommonsExecDefaultExecutorpool-14-thread-1] INFO  ch.vorburger.exec.LoggingExecuteResultHandler - Program [/tmp/MariaDB4j/base/bin/mariadb, --default-character-set=utf8, --socket=/tmp/MariaDB4j.40315.sock] (in working directory /tmp/MariaDB4j/base) just exited, with value 0

There are also some helper methods that just wrap db.run() but make it easier to perform some common tasks. For example, db.createDB() can be used to execute the command CREATE DATABASE IF NOT EXISTS:

db.createDB("testing", "testuser", "testpass");

There’s also db.source(), which we can use to execute an entire SQL script that exists on the classpath:

db.createDB("testing", "testuser", "testpass");
db.source("com/baeldung/data/mariadb4j/source.sql", "testuser", "testpass", "testing");

Instead of running a single command, this executes the entire provided file of commands, line by line.

4.3. Interacting With JDBC

Finally, perhaps the most useful way to interact with the database is using JDBC. Since this is a fully running database engine, we can connect to it as we would usually. All we need is an appropriate JDBC connection string and credentials.

We’ve already seen how we can create a database with credentials within our database engine – using db.createDB(). Once done, we can then call db.getConfiguration().getURL() to provide a connection string for talking to our database:

db.createDB("testing", "testuser", "testpass");
assertEquals("jdbc:mariadb://localhost:13306/testing", db.getConfiguration().getURL("testing"));

This generates the URL with the correct hostname and port, even if the port was randomly assigned.

Once we’ve done this, we can then use any standard JDBC utilities to communicate with the database:

String url = db.getConfiguration().getURL("testing");
try (Connection conn = DriverManager.getConnection(url, "testuser", "testpass")) {
    // Use JDBC Connection here.
}

We can do this equally well with any other JDBC-compatible tools – for example, JPA, JdbcTemplate, or anything else.

Note that this assumes that we have the MariaDB JDBC drivers available on the classpath, just as when connecting to any other MariaDB instance.

5. Using Within JUnit Tests

One very powerful use for MariaDB4j is the ability to integrate with a real, but ephemeral database from our tests while the production application points to a permanent database. MariaDB4j provides a JUnit4 rule for exactly this purpose.

Using the standard @Rule annotation starts the database before each test method runs and shuts it down afterwards:

@Rule
public MariaDB4jRule dbRule = new MariaDB4jRule(0);

We can then access the database from within our tests however we wish:

@Test
public void whenRunningATest_thenTheDbIsAvailable() throws Exception {
    try (Connection conn = DriverManager.getConnection(dbRule.getURL(), "root", "")) {
        // Use JDBC Connection here.
    }
}

This will start and stop the database around each individual test method, which gives us isolation but at the cost of performance. Instead, we can use the @ClassRule annotation:

@ClassRule
public static MariaDB4jRule dbRule = new MariaDB4jRule(0);

This starts the database once for the entire test class, instead of for each method. This gives us obvious performance improvements, but at the risk that we might have leakage between tests

6. Summary

This was a quick introduction to MariaDB4j. We’ve seen a few ways that it can be used to easily start up a MariaDB database for use locally. This library can do a lot more, including direct integration with Maven and easy use within Spring Boot.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)