Skip to content

Latest commit

 

History

History
155 lines (106 loc) · 4.65 KB

overview.md

File metadata and controls

155 lines (106 loc) · 4.65 KB

🗄️ H2 Database Overview

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.

🛠️ Key Features of H2 Database

  1. In-Memory Database:

    • H2 can operate entirely in memory, making it extremely fast and ideal for testing scenarios where persistence is not required.
  2. Embedded Mode:

    • H2 can be embedded directly into Java applications, allowing for easy integration without requiring a separate database server.
  3. Server Mode:

    • H2 can also run as a traditional database server, allowing multiple connections from different clients.
  4. 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.
  5. 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.
  6. 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.

🧩 Common Use Cases

  1. 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.
  2. Prototyping:

    • H2 is an excellent choice for quickly prototyping database-driven applications without the overhead of setting up a full-fledged database server.
  3. Embedded Applications:

    • H2 can be embedded in desktop or mobile applications where a lightweight, self-contained database is needed.

📘 Getting Started with H2

1. Adding H2 to Your Project

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>

2. Configuring H2 Database

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

3. Accessing the H2 Console

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.

4. Creating and Querying Tables

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;

🔧 Advanced H2 Features

1. File-based Database

H2 can also be used as a file-based database where data is persisted to disk:

spring.datasource.url=jdbc:h2:file:/data/testdb

2. Mixed Mode

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

3. Compatibility Modes

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;

4. Database Encryption

H2 supports database-level encryption for securing sensitive data:

spring.datasource.url=jdbc:h2:mem:testdb;CIPHER=AES

📜 Summary

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.