H2 Database Engine - A very fast, open source, JDBC API database with embedded and server modes, transaction support, multi-version concurrency, browser-based console application, encrypted databases, fulltext search, and pure Java implementation with small footprint
npx @tessl/cli install tessl/maven-com-h2database--h2@2.3.00
# H2 Database Engine
1
2
H2 Database Engine is a very fast, open source, JDBC API database with embedded and server modes, transaction support, multi-version concurrency, browser-based console application, encrypted databases, fulltext search, and pure Java implementation with small footprint. H2 supports both in-memory and persistent databases, making it ideal for development, testing, embedded applications, and production systems requiring a lightweight yet powerful SQL database solution.
3
4
## Package Information
5
6
- **Package Name**: com.h2database:h2
7
- **Package Type**: Maven
8
- **Language**: Java
9
- **Installation**: Add to Maven `pom.xml`:
10
```xml
11
<dependency>
12
<groupId>com.h2database</groupId>
13
<artifactId>h2</artifactId>
14
<version>2.3.232</version>
15
</dependency>
16
```
17
- **Gradle**: `implementation 'com.h2database:h2:2.3.232'`
18
19
## Core Imports
20
21
```java
22
import org.h2.Driver;
23
import org.h2.jdbcx.JdbcDataSource;
24
```
25
26
For JDBC usage:
27
```java
28
import java.sql.*;
29
```
30
31
For tools and utilities:
32
```java
33
import org.h2.tools.*;
34
```
35
36
## Basic Usage
37
38
### Simple Embedded Database
39
40
```java
41
import java.sql.*;
42
43
public class H2Example {
44
public static void main(String[] args) throws SQLException {
45
// Register H2 driver (optional in modern Java)
46
Class.forName("org.h2.Driver");
47
48
// Create in-memory database connection
49
Connection conn = DriverManager.getConnection(
50
"jdbc:h2:mem:testdb", "sa", "");
51
52
// Create table and insert data
53
Statement stmt = conn.createStatement();
54
stmt.execute("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255))");
55
stmt.execute("INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob')");
56
57
// Query data
58
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
59
while (rs.next()) {
60
System.out.println("ID: " + rs.getInt("id") +
61
", Name: " + rs.getString("name"));
62
}
63
64
// Close connection
65
conn.close();
66
}
67
}
68
```
69
70
### Persistent Database
71
72
```java
73
// File-based database (creates testdb.mv.db file)
74
Connection conn = DriverManager.getConnection(
75
"jdbc:h2:~/testdb", "sa", "password");
76
```
77
78
### Using DataSource
79
80
```java
81
import org.h2.jdbcx.JdbcDataSource;
82
83
JdbcDataSource ds = new JdbcDataSource();
84
ds.setURL("jdbc:h2:mem:testdb");
85
ds.setUser("sa");
86
ds.setPassword("");
87
88
Connection conn = ds.getConnection();
89
```
90
91
## Architecture
92
93
H2 Database Engine is built around several key components:
94
95
- **JDBC Driver**: Standard JDBC 4.2 compliant driver for database connectivity
96
- **Database Engine**: Core SQL processing engine with full ACID transaction support
97
- **MVStore**: Advanced storage engine providing MVCC (Multi-Version Concurrency Control)
98
- **Server Components**: TCP server, Web console, and PostgreSQL compatibility server
99
- **Administrative Tools**: Command-line utilities for backup, restore, scripting, and maintenance
100
- **Extension APIs**: Interfaces for custom triggers, aggregates, table engines, and event listeners
101
102
## Capabilities
103
104
### JDBC Connectivity
105
106
Standard JDBC database connectivity with H2-specific enhancements for embedded and server modes. Supports connection pooling, XA transactions, and various authentication methods.
107
108
```java { .api }
109
public class Driver implements java.sql.Driver {
110
public Connection connect(String url, Properties info) throws SQLException;
111
public boolean acceptsURL(String url) throws SQLException;
112
public static Driver load();
113
}
114
```
115
116
```java { .api }
117
public class JdbcDataSource implements DataSource, XADataSource, ConnectionPoolDataSource {
118
public Connection getConnection() throws SQLException;
119
public Connection getConnection(String username, String password) throws SQLException;
120
public void setURL(String url);
121
public void setUser(String user);
122
public void setPassword(String password);
123
}
124
```
125
126
[JDBC Connectivity](./jdbc.md)
127
128
### Administrative Tools
129
130
Comprehensive set of command-line tools for database administration, maintenance, and data management operations.
131
132
```java { .api }
133
public class Server extends Tool implements Runnable {
134
public static void main(String... args);
135
public Server();
136
public Server(Service service, String... args);
137
}
138
```
139
140
```java { .api }
141
public class Script extends Tool {
142
public static void main(String... args);
143
}
144
```
145
146
```java { .api }
147
public class Backup extends Tool {
148
public static void main(String... args);
149
}
150
```
151
152
[Tools and Utilities](./tools.md)
153
154
### Embedded MVStore
155
156
High-performance key-value storage engine that can be used independently of the SQL database. Provides persistent maps with ACID transactions and MVCC.
157
158
```java { .api }
159
public class MVStore implements AutoCloseable {
160
public static MVStore open(String fileName);
161
public <K, V> MVMap<K, V> openMap(String name);
162
public Set<String> getMapNames();
163
public void close();
164
}
165
```
166
167
```java { .api }
168
public class MVMap<K, V> {
169
public V put(K key, V value);
170
public V get(Object key);
171
public V remove(Object key);
172
public long size();
173
}
174
```
175
176
[Embedded MVStore](./mvstore.md)
177
178
### Server Deployment
179
180
Server components for deploying H2 as a standalone database server with support for multiple client connections, web-based administration, and PostgreSQL protocol compatibility.
181
182
```java { .api }
183
public class TcpServer {
184
// TCP/IP database server implementation
185
}
186
```
187
188
```java { .api }
189
public class WebServer {
190
// HTTP server for H2 Console web interface
191
}
192
```
193
194
```java { .api }
195
public class PgServer {
196
// PostgreSQL protocol compatibility server
197
}
198
```
199
200
[Server Deployment](./server.md)
201
202
### Extension APIs
203
204
Interfaces for extending H2 Database functionality with custom triggers, aggregate functions, table engines, and event listeners.
205
206
```java { .api }
207
public interface Trigger {
208
int INSERT = 1;
209
int UPDATE = 2;
210
int DELETE = 4;
211
int SELECT = 8;
212
213
void fire(Connection conn, Object[] oldRow, Object[] newRow) throws SQLException;
214
default void init(Connection conn, String schemaName, String triggerName,
215
String tableName, boolean before, int type) throws SQLException;
216
}
217
```
218
219
```java { .api }
220
public interface AggregateFunction {
221
void add(Object value) throws SQLException;
222
Object getResult() throws SQLException;
223
int getType(int[] inputTypes) throws SQLException;
224
}
225
```
226
227
```java { .api }
228
public interface DatabaseEventListener extends EventListener {
229
default void exceptionThrown(SQLException e, String sql);
230
default void setProgress(int state, String name, long x, long max);
231
}
232
```
233
234
[Extension APIs](./extensions.md)
235
236
## Common Connection URLs
237
238
- **In-memory database**: `jdbc:h2:mem:dbname`
239
- **File database**: `jdbc:h2:~/dbname` or `jdbc:h2:./dbname`
240
- **Encrypted database**: `jdbc:h2:~/secure;CIPHER=AES`
241
- **TCP server**: `jdbc:h2:tcp://localhost/~/dbname`
242
- **SSL connection**: `jdbc:h2:ssl://localhost/~/dbname`
243
- **Mixed mode**: `jdbc:h2:~/dbname;AUTO_SERVER=TRUE`
244
- **Read-only**: `jdbc:h2:~/dbname;ACCESS_MODE_DATA=r`
245
246
## Error Handling
247
248
H2 uses standard SQLException hierarchy with H2-specific error codes defined in ErrorCode class:
249
250
```java { .api }
251
public class ErrorCode {
252
public static final int NO_DATA_AVAILABLE = 2000;
253
// ... numerous other error codes
254
}
255
```
256
257
Common patterns:
258
```java
259
try {
260
Connection conn = DriverManager.getConnection("jdbc:h2:mem:test", "sa", "");
261
// database operations
262
} catch (SQLException e) {
263
System.err.println("Error Code: " + e.getErrorCode());
264
System.err.println("SQL State: " + e.getSQLState());
265
System.err.println("Message: " + e.getMessage());
266
}
267
```