H2 Database FAQ

Installation

Q: How to download H2 database?

A: Visit https://www.h2database.com/html/download.html to download the version suitable for your platform. There are two options: Windows Installer and cross-platform ZIP package.

Q: What Java version does H2 require?

A: H2 requires Java 11 or higher.

Q: How to use H2 in Maven project?

A: Add dependency in pom.xml:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>2.2.224</version>
</dependency>

Q: How to run H2 after extracting ZIP?

A: Run in command line:

java -jar h2.jar

This will start the H2 Web Console.

Connection

Q: How to connect to H2 database?

A: Use JDBC connection:

Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:mem:testdb", "sa", "");

Q: What is H2's default username and password?

A: Default username is "sa", password is empty.

Q: How to connect to file database?

A:

String url = "jdbc:h2:file:./data/testdb";
Connection conn = DriverManager.getConnection(url, "sa", "password");

Q: How to start H2 TCP server?

A:

import org.h2.tools.Server;
Server server = Server.createTcpServer().start();

Q: What if port is already in use?

A: You can specify port in connection URL:

jdbc:h2:tcp://localhost:9093/testdb

Data Issues

Q: Will in-memory database data be lost?

A: Yes, in-memory database (jdbc:h2:mem:) data will be lost after all connections close. For persistence, use file database mode.

Q: How to view database file location?

A: Specify path in connection URL:

jdbc:h2:file:/path/to/database

Q: What to do if database file is corrupted?

A: Try recovery mode:

jdbc:h2:file:./testdb;LOG=0

Or restore from backup.

Q: How to encrypt database?

A:

jdbc:h2:file:./encrypted;CIPHER=AES
Connection conn = DriverManager.getConnection(url, "sa", "password");

Performance

Q: How to improve batch insert performance?

A: Use batch operations and disable auto-commit:

conn.setAutoCommit(false);
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO users (name) VALUES (?)");
for (int i = 0; i < 1000; i++) {
    pstmt.setString(1, "User" + i);
    pstmt.addBatch();
}
pstmt.executeBatch();
conn.commit();

Q: What if queries are slow?

A:

  1. Create indexes for common query conditions
  2. Use EXPLAIN to analyze query plan
  3. Avoid SELECT *, only query needed columns

Q: How to handle high memory usage?

A: Adjust JVM heap size and use connection pool properly.

SQL Compatibility

Q: Which database modes does H2 support?

A: H2 can simulate multiple databases:

  • MySQL
  • PostgreSQL
  • Oracle
  • MS SQL Server
  • DB2

Usage:

jdbc:h2:mem:testdb;MODE=MySQL

Q: How to handle auto-increment IDs?

A: H2 supports multiple methods:

-- Method 1: IDENTITY
CREATE TABLE t1 (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100));

-- Method 2: SEQUENCE
CREATE SEQUENCE seq_id START WITH 1;

Transaction

Q: What if transaction is not committed?

A: Uncommitted transactions will be automatically rolled back when connection closes.

Q: How to handle deadlocks?

A: H2 automatically detects deadlocks and rolls back one transaction. It is recommended to shorten transaction scope to avoid holding locks for long periods.

Security

Q: Is H2 database secure?

A: H2 is suitable for development and testing environments. For production:

  1. Use encrypted database
  2. Use strong passwords
  3. Restrict network access
  4. Use SSL connections

Q: How to prevent SQL injection?

A: Use PreparedStatement instead of string concatenation:

// Correct
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM users WHERE name = ?");
pstmt.setString(1, name);

// Wrong
Statement stmt = conn.createStatement();
stmt.executeQuery("SELECT * FROM users WHERE name = '" + name + "'");

Tools

Q: How to use H2 Console?

A: Run java -jar h2.jar -web then visit http://localhost:8082

Q: How to use H2 Shell?

A: Run java -jar h2.jar -shell or java -jar h2.jar -tool

Q: How to check H2 version?

A:

java -jar h2.jar -version

Or in SQL: SELECT H2VERSION();

Migration

Q: How to migrate from MySQL to H2?

A:

  1. Set H2 compatibility mode: jdbc:h2:file:db;MODE=MySQL
  2. Export MySQL data as SQL script
  3. Run script in H2

Q: What to note when upgrading H2 version?

A: Upgrading from H2 1.x to 2.x requires special attention. Please refer to official migration guide: https://www.h2database.com/html/migration-to-v2.html