H2 is a lightweight, fast, and open-source relational database that is particularly well-suited for use in development, testing, and small-scale production environments. It is fully written in Java, and can be embedded in Java applications or run as a standalone server.
-
In-Memory Database:
- H2 can operate entirely in memory, making it extremely fast and ideal for testing scenarios where persistence is not required.
-
Embedded Mode:
- H2 can be embedded directly into Java applications, allowing for easy integration without requiring a separate database server.
-
Server Mode:
- H2 can also run as a traditional database server, allowing multiple connections from different clients.
-
Compatibility:
- H2 supports a large subset of SQL and is compatible with databases like PostgreSQL and MySQL, making it easier to switch databases if needed.
-
Web Console:
- H2 comes with a built-in web-based console that allows you to interact with the database, execute SQL queries, and manage the database schema.
-
Small Footprint:
- H2 has a very small footprint (only a few MBs) and is designed to be lightweight while providing a rich set of features.
-
Development and Testing:
- H2 is often used in development environments for testing purposes, where it can be run in memory to speed up tests and reduce the need for a persistent database.
-
Prototyping:
- H2 is an excellent choice for quickly prototyping database-driven applications without the overhead of setting up a full-fledged database server.
-
Embedded Applications:
- H2 can be embedded in desktop or mobile applications where a lightweight, self-contained database is needed.
To use H2 in a Spring Boot application, add the H2 dependency to your pom.xml
:
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
</dependencies>
You can configure H2 using the application.properties
or application.yml
file.
Example with application.properties
:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Example with application.yml
:
spring:
datasource:
url: jdbc:h2:mem:testdb
driverClassName: org.h2.Driver
username: sa
password:
h2:
console:
enabled: true
path: /h2-console
jpa:
database-platform: org.hibernate.dialect.H2Dialect
With the H2 console enabled, you can access it by navigating to http://localhost:8080/h2-console
in your browser. You will need to enter the JDBC URL (jdbc:h2:mem:testdb
by default) and credentials to connect.
You can use the H2 console or your application code to create tables and run queries.
Example of creating a table:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL
);
Example of inserting data:
INSERT INTO users (username, email) VALUES ('john_doe', 'john@example.com');
Example of querying data:
SELECT * FROM users;
H2 can also be used as a file-based database where data is persisted to disk:
spring.datasource.url=jdbc:h2:file:/data/testdb
H2 supports "mixed mode," where an embedded database can be accessed as a server simultaneously:
spring.datasource.url=jdbc:h2:file:~/testdb;AUTO_SERVER=TRUE
H2 provides compatibility modes for other databases, such as MySQL or PostgreSQL, which allows you to use SQL dialects from these databases:
SET MODE MySQL;
H2 supports database-level encryption for securing sensitive data:
spring.datasource.url=jdbc:h2:mem:testdb;CIPHER=AES
H2 is a versatile and powerful database that is ideal for development, testing, and lightweight production use. Its in-memory and embedded capabilities, combined with ease of use and a rich feature set, make it an excellent choice for many Java applications, especially those built with Spring Boot.