| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 2 |
<!-- |
| 3 |
Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 4 |
and the EPL 1.0 (https://h2database.com/html/license.html). |
| 5 |
Initial Developer: H2 Group |
| 6 |
--> |
| 7 |
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> |
| 8 |
<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> |
| 9 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 10 |
<title> |
| 11 |
Features |
| 12 |
</title> |
| 13 |
<link rel="stylesheet" type="text/css" href="stylesheet.css" /> |
| 14 |
<!-- [search] { --> |
| 15 |
<script type="text/javascript" src="navigation.js"></script> |
| 16 |
</head><body onload="frameMe();"> |
| 17 |
<table class="content"><tr class="content"><td class="content"><div class="contentDiv"> |
| 18 |
<!-- } --> |
| 19 |
|
| 20 |
<h1>Features</h1> |
| 21 |
|
| 22 |
<a href="#feature_list"> |
| 23 |
Feature List</a><br /> |
| 24 |
<a href="#products_work_with"> |
| 25 |
H2 in Use</a><br /> |
| 26 |
<a href="#connection_modes"> |
| 27 |
Connection Modes</a><br /> |
| 28 |
<a href="#database_url"> |
| 29 |
Database URL Overview</a><br /> |
| 30 |
<a href="#embedded_databases"> |
| 31 |
Connecting to an Embedded (Local) Database</a><br /> |
| 32 |
<a href="#in_memory_databases"> |
| 33 |
In-Memory Databases</a><br /> |
| 34 |
<a href="#file_encryption"> |
| 35 |
Database Files Encryption</a><br /> |
| 36 |
<a href="#database_file_locking"> |
| 37 |
Database File Locking</a><br /> |
| 38 |
<a href="#database_only_if_exists"> |
| 39 |
Opening a Database Only if it Already Exists</a><br /> |
| 40 |
<a href="#closing_a_database"> |
| 41 |
Closing a Database</a><br /> |
| 42 |
<a href="#ignore_unknown_settings"> |
| 43 |
Ignore Unknown Settings</a><br /> |
| 44 |
<a href="#other_settings"> |
| 45 |
Changing Other Settings when Opening a Connection</a><br /> |
| 46 |
<a href="#custom_access_mode"> |
| 47 |
Custom File Access Mode</a><br /> |
| 48 |
<a href="#multiple_connections"> |
| 49 |
Multiple Connections</a><br /> |
| 50 |
<a href="#database_file_layout"> |
| 51 |
Database File Layout</a><br /> |
| 52 |
<a href="#logging_recovery"> |
| 53 |
Logging and Recovery</a><br /> |
| 54 |
<a href="#compatibility"> |
| 55 |
Compatibility</a><br /> |
| 56 |
<a href="#auto_reconnect"> |
| 57 |
Auto-Reconnect</a><br /> |
| 58 |
<a href="#auto_mixed_mode"> |
| 59 |
Automatic Mixed Mode</a><br /> |
| 60 |
<a href="#page_size"> |
| 61 |
Page Size</a><br /> |
| 62 |
<a href="#trace_options"> |
| 63 |
Using the Trace Options</a><br /> |
| 64 |
<a href="#other_logging"> |
| 65 |
Using Other Logging APIs</a><br /> |
| 66 |
<a href="#read_only"> |
| 67 |
Read Only Databases</a><br /> |
| 68 |
<a href="#database_in_zip"> |
| 69 |
Read Only Databases in Zip or Jar File</a><br /> |
| 70 |
<a href="#generated_columns"> |
| 71 |
Generated Columns (Computed Columns) / Function Based Index</a><br /> |
| 72 |
<a href="#multi_dimensional"> |
| 73 |
Multi-Dimensional Indexes</a><br /> |
| 74 |
<a href="#user_defined_functions"> |
| 75 |
User-Defined Functions and Stored Procedures</a><br /> |
| 76 |
<a href="#pluggable_tables"> |
| 77 |
Pluggable or User-Defined Tables</a><br /> |
| 78 |
<a href="#triggers"> |
| 79 |
Triggers</a><br /> |
| 80 |
<a href="#compacting"> |
| 81 |
Compacting a Database</a><br /> |
| 82 |
<a href="#cache_settings"> |
| 83 |
Cache Settings</a><br /> |
| 84 |
<a href="#external_authentication"> |
| 85 |
External Authentication (Experimental)</a><br /> |
| 86 |
|
| 87 |
<h2 id="feature_list">Feature List</h2> |
| 88 |
<h3>Main Features</h3> |
| 89 |
<ul> |
| 90 |
<li>Very fast database engine |
| 91 |
</li><li>Open source |
| 92 |
</li><li>Written in Java |
| 93 |
</li><li>Supports standard SQL, JDBC API |
| 94 |
</li><li>Embedded and Server mode, Clustering support |
| 95 |
</li><li>Strong security features |
| 96 |
</li><li>The PostgreSQL ODBC driver can be used |
| 97 |
</li><li>Multi version concurrency |
| 98 |
</li></ul> |
| 99 |
|
| 100 |
<h3>Additional Features</h3> |
| 101 |
<ul> |
| 102 |
<li>Disk based or in-memory databases and tables, read-only database support, temporary tables |
| 103 |
</li><li>Transaction support (read uncommitted, read committed, repeatable read, snapshot), 2-phase-commit |
| 104 |
</li><li>Multiple connections, row-level locking |
| 105 |
</li><li>Cost based optimizer, using a genetic algorithm for complex queries, zero-administration |
| 106 |
</li><li>Scrollable and updatable result set support, large result set, external result sorting, |
| 107 |
functions can return a result set |
| 108 |
</li><li>Encrypted database (AES), SHA-256 password encryption, encryption functions, SSL |
| 109 |
</li></ul> |
| 110 |
|
| 111 |
<h3>SQL Support</h3> |
| 112 |
<ul> |
| 113 |
<li>Support for multiple schemas, information schema |
| 114 |
</li><li>Referential integrity / foreign key constraints with cascade, check constraints |
| 115 |
</li><li>Inner and outer joins, subqueries, read only views and inline views |
| 116 |
</li><li>Triggers and Java functions / stored procedures |
| 117 |
</li><li>Many built-in functions, including XML and lossless data compression |
| 118 |
</li><li>Wide range of data types including large objects (BLOB/CLOB) and arrays |
| 119 |
</li><li>Sequences and identity columns, generated columns (can be used for function based indexes) |
| 120 |
</li><li>ORDER BY, GROUP BY, HAVING, UNION, OFFSET / FETCH (including PERCENT and WITH TIES), LIMIT, TOP, |
| 121 |
DISTINCT / DISTINCT ON (...) |
| 122 |
</li><li>Window functions |
| 123 |
</li><li>Collation support, including support for the ICU4J library |
| 124 |
</li><li>Support for users and roles |
| 125 |
</li><li>Compatibility modes for IBM DB2, Apache Derby, HSQLDB, |
| 126 |
MS SQL Server, MySQL, Oracle, and PostgreSQL. |
| 127 |
</li></ul> |
| 128 |
|
| 129 |
<h3>Security Features</h3> |
| 130 |
<ul> |
| 131 |
<li>Includes a solution for the SQL injection problem |
| 132 |
</li><li>User password authentication uses SHA-256 and salt |
| 133 |
</li><li>For server mode connections, user passwords are never transmitted in plain text over the network |
| 134 |
(even when using insecure connections; this only applies to the TCP server and not to the H2 Console however; |
| 135 |
it also doesn't apply if you set the password in the database URL) |
| 136 |
</li><li>All database files (including script files that can be used to backup data) can be |
| 137 |
encrypted using the AES-128 encryption algorithm |
| 138 |
</li><li>The remote JDBC driver supports TCP/IP connections over TLS |
| 139 |
</li><li>The built-in web server supports connections over TLS |
| 140 |
</li><li>Passwords can be sent to the database using char arrays instead of Strings |
| 141 |
</li></ul> |
| 142 |
|
| 143 |
<h3>Other Features and Tools</h3> |
| 144 |
<ul> |
| 145 |
<li>Small footprint (around 2.5 MB), low memory requirements |
| 146 |
</li><li>Multiple index types (b-tree, tree, hash) |
| 147 |
</li><li>Support for multi-dimensional indexes |
| 148 |
</li><li>CSV (comma separated values) file support |
| 149 |
</li><li>Support for linked tables, and a built-in virtual 'range' table |
| 150 |
</li><li>Supports the <code>EXPLAIN PLAN</code> statement; sophisticated trace options |
| 151 |
</li><li>Database closing can be delayed or disabled to improve the performance |
| 152 |
</li><li>Web-based Console application (translated to many languages) with autocomplete |
| 153 |
</li><li>The database can generate SQL script files |
| 154 |
</li><li>Contains a recovery tool that can dump the contents of the database |
| 155 |
</li><li>Support for variables (for example to calculate running totals) |
| 156 |
</li><li>Automatic re-compilation of prepared statements |
| 157 |
</li><li>Uses a small number of database files |
| 158 |
</li><li>Uses a checksum for each record and log entry for data integrity |
| 159 |
</li><li>Well tested (high code coverage, randomized stress tests) |
| 160 |
</li></ul> |
| 161 |
|
| 162 |
<h2 id="products_work_with">H2 in Use</h2> |
| 163 |
<p> |
| 164 |
For a list of applications that work with or use H2, see: |
| 165 |
<a href="https://h2database.com/html/links.html">Links</a>. |
| 166 |
</p> |
| 167 |
|
| 168 |
<h2 id="connection_modes">Connection Modes</h2> |
| 169 |
<p> |
| 170 |
The following connection modes are supported: |
| 171 |
</p> |
| 172 |
<ul> |
| 173 |
<li>Embedded mode (local connections using JDBC) |
| 174 |
</li><li>Server mode (remote connections using JDBC or ODBC over TCP/IP) |
| 175 |
</li><li>Mixed mode (local and remote connections at the same time) |
| 176 |
</li></ul> |
| 177 |
|
| 178 |
<h3>Embedded Mode</h3> |
| 179 |
<p> |
| 180 |
In embedded mode, an application opens a database from within the same JVM using JDBC. |
| 181 |
This is the fastest and easiest connection mode. |
| 182 |
The disadvantage is that a database may only be open in one virtual machine (and class loader) at any time. |
| 183 |
As in all modes, both persistent and in-memory databases are supported. |
| 184 |
There is no limit on the number of database open concurrently, |
| 185 |
or on the number of open connections. |
| 186 |
</p> |
| 187 |
<p> |
| 188 |
In embedded mode I/O operations can be performed by application's threads that execute a SQL command. |
| 189 |
The application may not interrupt these threads, it can lead to database corruption, |
| 190 |
because JVM closes I/O handle during thread interruption. |
| 191 |
Consider other ways to control execution of your application. |
| 192 |
When interrupts are possible the <a href="advanced.html#file_system"><code>async:</code></a> |
| 193 |
file system can be used as a workaround, but full safety is not guaranteed. |
| 194 |
It's recommended to use the client-server model instead, the client side may interrupt own threads. |
| 195 |
</p> |
| 196 |
<img src="images/connection-mode-embedded-2.png" |
| 197 |
width="208" height="259" |
| 198 |
alt="The database is embedded in the application" /> |
| 199 |
|
| 200 |
<h3>Server Mode</h3> |
| 201 |
<p> |
| 202 |
When using the server mode (sometimes called remote mode or client/server mode), |
| 203 |
an application opens a database remotely using the JDBC or ODBC API. |
| 204 |
A server needs to be started within the same or another virtual machine, or on another computer. |
| 205 |
Many applications can connect to the same database at the same time, by connecting to this server. |
| 206 |
Internally, the server process opens the database(s) in embedded mode. |
| 207 |
</p> |
| 208 |
<p> |
| 209 |
The server mode is slower than the embedded mode, because all data is transferred over TCP/IP. |
| 210 |
As in all modes, both persistent and in-memory databases are supported. |
| 211 |
There is no limit on the number of database open concurrently per server, |
| 212 |
or on the number of open connections. |
| 213 |
</p> |
| 214 |
<img src="images/connection-mode-remote-2.png" |
| 215 |
width="376" height="218" |
| 216 |
alt="The database is running in a server; the application connects to the server" /> |
| 217 |
|
| 218 |
<h3>Mixed Mode</h3> |
| 219 |
<p> |
| 220 |
The mixed mode is a combination of the embedded and the server mode. |
| 221 |
The first application that connects to a database does that in embedded mode, but also starts |
| 222 |
a server so that other applications (running in different processes or virtual machines) can |
| 223 |
concurrently access the same data. The local connections are as fast as if |
| 224 |
the database is used in just the embedded mode, while the remote |
| 225 |
connections are a bit slower. |
| 226 |
</p><p> |
| 227 |
The server can be started and stopped from within the application (using the server API), |
| 228 |
or automatically (automatic mixed mode). When using the <a href="#auto_mixed_mode">automatic mixed mode</a>, |
| 229 |
all clients that want to connect to the database (no matter if |
| 230 |
it's an local or remote connection) can do so using the exact same database URL. |
| 231 |
</p> |
| 232 |
<img src="images/connection-mode-mixed-2.png" |
| 233 |
width="403" height="240" |
| 234 |
alt="Database, server, and application run in one JVM; an application connects" /> |
| 235 |
|
| 236 |
<h2 id="database_url">Database URL Overview</h2> |
| 237 |
<p> |
| 238 |
This database supports multiple connection modes and connection settings. |
| 239 |
This is achieved using different database URLs. Settings in the URLs are not case sensitive. |
| 240 |
</p> |
| 241 |
<table class="main"> |
| 242 |
<tr><th>Topic</th><th>URL Format and Examples</th></tr> |
| 243 |
<tr> |
| 244 |
<td><a href="#embedded_databases">Embedded (local) connection</a></td> |
| 245 |
<td class="notranslate"> |
| 246 |
jdbc:h2:[file:][<path>]<databaseName><br /> |
| 247 |
jdbc:h2:~/test<br /> |
| 248 |
jdbc:h2:file:/data/sample<br /> |
| 249 |
jdbc:h2:file:C:/data/sample (Windows only)<br /> |
| 250 |
</td> |
| 251 |
</tr> |
| 252 |
<tr> |
| 253 |
<td><a href="#in_memory_databases">In-memory (private)</a></td> |
| 254 |
<td class="notranslate">jdbc:h2:mem:</td> |
| 255 |
</tr> |
| 256 |
<tr> |
| 257 |
<td><a href="#in_memory_databases">In-memory (named)</a></td> |
| 258 |
<td class="notranslate"> |
| 259 |
jdbc:h2:mem:<databaseName><br /> |
| 260 |
jdbc:h2:mem:test_mem |
| 261 |
</td> |
| 262 |
</tr> |
| 263 |
<tr> |
| 264 |
<td><a href="tutorial.html#using_server">Server mode (remote connections)<br /> using TCP/IP</a></td> |
| 265 |
<td class="notranslate"> |
| 266 |
jdbc:h2:tcp://<server>[:<port>]/[<path>]<databaseName><br /> |
| 267 |
jdbc:h2:tcp://localhost/~/test<br /> |
| 268 |
jdbc:h2:tcp://dbserv:8084/~/sample<br /> |
| 269 |
jdbc:h2:tcp://localhost/mem:test<br /> |
| 270 |
</td> |
| 271 |
</tr> |
| 272 |
<tr> |
| 273 |
<td><a href="advanced.html#tls_connections">Server mode (remote connections)<br /> using TLS</a></td> |
| 274 |
<td class="notranslate"> |
| 275 |
jdbc:h2:ssl://<server>[:<port>]/[<path>]<databaseName><br /> |
| 276 |
jdbc:h2:ssl://localhost:8085/~/sample; |
| 277 |
</td> |
| 278 |
</tr> |
| 279 |
<tr> |
| 280 |
<td><a href="#file_encryption">Using encrypted files</a></td> |
| 281 |
<td class="notranslate"> |
| 282 |
jdbc:h2:<url>;CIPHER=AES<br /> |
| 283 |
jdbc:h2:ssl://localhost/~/test;CIPHER=AES<br /> |
| 284 |
jdbc:h2:file:~/secure;CIPHER=AES<br /> |
| 285 |
</td> |
| 286 |
</tr> |
| 287 |
<tr> |
| 288 |
<td><a href="#database_file_locking">File locking methods</a></td> |
| 289 |
<td class="notranslate"> |
| 290 |
jdbc:h2:<url>;FILE_LOCK={FILE|SOCKET|FS|NO}<br /> |
| 291 |
jdbc:h2:file:~/private;CIPHER=AES;FILE_LOCK=SOCKET<br /> |
| 292 |
</td> |
| 293 |
</tr> |
| 294 |
<tr> |
| 295 |
<td><a href="#database_only_if_exists">Only open if it already exists</a></td> |
| 296 |
<td class="notranslate"> |
| 297 |
jdbc:h2:<url>;IFEXISTS=TRUE<br /> |
| 298 |
jdbc:h2:file:~/sample;IFEXISTS=TRUE<br /> |
| 299 |
</td> |
| 300 |
</tr> |
| 301 |
<tr> |
| 302 |
<td><a href="#do_not_close_on_exit">Don't close the database when the VM exits</a></td> |
| 303 |
<td class="notranslate"> |
| 304 |
jdbc:h2:<url>;DB_CLOSE_ON_EXIT=FALSE |
| 305 |
</td> |
| 306 |
</tr> |
| 307 |
<tr> |
| 308 |
<td><a href="#execute_sql_on_connection">Execute SQL on connection</a></td> |
| 309 |
<td class="notranslate"> |
| 310 |
jdbc:h2:<url>;INIT=RUNSCRIPT FROM '~/create.sql'<br /> |
| 311 |
jdbc:h2:file:~/sample;INIT=RUNSCRIPT FROM '~/create.sql'\;RUNSCRIPT FROM '~/populate.sql'<br /> |
| 312 |
</td> |
| 313 |
</tr> |
| 314 |
<tr> |
| 315 |
<td><a href="advanced.html#passwords">User name and/or password</a></td> |
| 316 |
<td class="notranslate"> |
| 317 |
jdbc:h2:<url>[;USER=<username>][;PASSWORD=<value>]<br /> |
| 318 |
jdbc:h2:file:~/sample;USER=sa;PASSWORD=123<br /> |
| 319 |
</td> |
| 320 |
</tr> |
| 321 |
<tr> |
| 322 |
<td><a href="#trace_options">Debug trace settings</a></td> |
| 323 |
<td class="notranslate"> |
| 324 |
jdbc:h2:<url>;TRACE_LEVEL_FILE=<level 0..3><br /> |
| 325 |
jdbc:h2:file:~/sample;TRACE_LEVEL_FILE=3<br /> |
| 326 |
</td> |
| 327 |
</tr> |
| 328 |
<tr> |
| 329 |
<td><a href="#ignore_unknown_settings">Ignore unknown settings</a></td> |
| 330 |
<td class="notranslate"> |
| 331 |
jdbc:h2:<url>;IGNORE_UNKNOWN_SETTINGS=TRUE<br /> |
| 332 |
</td> |
| 333 |
</tr> |
| 334 |
<tr> |
| 335 |
<td><a href="#custom_access_mode">Custom file access mode</a></td> |
| 336 |
<td class="notranslate"> |
| 337 |
jdbc:h2:<url>;ACCESS_MODE_DATA=rws<br /> |
| 338 |
</td> |
| 339 |
</tr> |
| 340 |
<tr> |
| 341 |
<td><a href="#database_in_zip">Database in a zip file</a></td> |
| 342 |
<td class="notranslate"> |
| 343 |
jdbc:h2:zip:<zipFileName>!/<databaseName><br /> |
| 344 |
jdbc:h2:zip:~/db.zip!/test |
| 345 |
</td> |
| 346 |
</tr> |
| 347 |
<tr> |
| 348 |
<td><a href="#compatibility">Compatibility mode</a></td> |
| 349 |
<td class="notranslate"> |
| 350 |
jdbc:h2:<url>;MODE=<databaseType><br /> |
| 351 |
jdbc:h2:~/test;MODE=MYSQL;DATABASE_TO_LOWER=TRUE |
| 352 |
</td> |
| 353 |
</tr> |
| 354 |
<tr> |
| 355 |
<td><a href="#auto_reconnect">Auto-reconnect</a></td> |
| 356 |
<td class="notranslate"> |
| 357 |
jdbc:h2:<url>;AUTO_RECONNECT=TRUE<br /> |
| 358 |
jdbc:h2:tcp://localhost/~/test;AUTO_RECONNECT=TRUE |
| 359 |
</td> |
| 360 |
</tr> |
| 361 |
<tr> |
| 362 |
<td><a href="#auto_mixed_mode">Automatic mixed mode</a></td> |
| 363 |
<td class="notranslate"> |
| 364 |
jdbc:h2:<url>;AUTO_SERVER=TRUE<br /> |
| 365 |
jdbc:h2:~/test;AUTO_SERVER=TRUE |
| 366 |
</td> |
| 367 |
</tr> |
| 368 |
<tr> |
| 369 |
<td><a href="#page_size">Page size</a></td> |
| 370 |
<td class="notranslate"> |
| 371 |
jdbc:h2:<url>;PAGE_SIZE=512<br /> |
| 372 |
</td> |
| 373 |
</tr> |
| 374 |
<tr> |
| 375 |
<td><a href="#other_settings">Changing other settings</a></td> |
| 376 |
<td class="notranslate"> |
| 377 |
jdbc:h2:<url>;<setting>=<value>[;<setting>=<value>...]<br /> |
| 378 |
jdbc:h2:file:~/sample;TRACE_LEVEL_SYSTEM_OUT=3<br /> |
| 379 |
</td> |
| 380 |
</tr> |
| 381 |
</table> |
| 382 |
|
| 383 |
<h2 id="embedded_databases">Connecting to an Embedded (Local) Database</h2> |
| 384 |
<p> |
| 385 |
The database URL for connecting to a local database is |
| 386 |
<code>jdbc:h2:[file:][<path>]<databaseName></code>. |
| 387 |
The prefix <code>file:</code> is optional. If no or only a relative path is used, then the current working |
| 388 |
directory is used as a starting point. The case sensitivity of the path and database name depend on the |
| 389 |
operating system, however it is recommended to use lowercase letters only. |
| 390 |
The database name must be at least three characters long |
| 391 |
(a limitation of <code>File.createTempFile</code>). |
| 392 |
The database name must not contain a semicolon. |
| 393 |
To point to the user home directory, use <code>~/</code>, as in: <code>jdbc:h2:~/test</code>. |
| 394 |
</p> |
| 395 |
|
| 396 |
<h2 id="in_memory_databases">In-Memory Databases</h2> |
| 397 |
<p> |
| 398 |
For certain use cases (for example: rapid prototyping, testing, high performance |
| 399 |
operations, read-only databases), it may not be required to persist data, or persist changes to the data. |
| 400 |
This database supports the in-memory mode, where the data is not persisted. |
| 401 |
</p><p> |
| 402 |
In some cases, only one connection to a in-memory database is required. |
| 403 |
This means the database to be opened is private. In this case, the database URL is |
| 404 |
<code>jdbc:h2:mem:</code> Opening two connections within the same virtual machine |
| 405 |
means opening two different (private) databases. |
| 406 |
</p><p> |
| 407 |
Sometimes multiple connections to the same in-memory database are required. |
| 408 |
In this case, the database URL must include a name. Example: <code>jdbc:h2:mem:db1</code>. |
| 409 |
Accessing the same database using this URL only works within the same virtual machine and |
| 410 |
class loader environment. |
| 411 |
</p><p> |
| 412 |
To access an in-memory database from another process or from another computer, |
| 413 |
you need to start a TCP server in the same process as the in-memory database was created. |
| 414 |
The other processes then need to access the database over TCP/IP or TLS, |
| 415 |
using a database URL such as: <code>jdbc:h2:tcp://localhost/mem:db1</code>. |
| 416 |
</p><p> |
| 417 |
By default, closing the last connection to a database closes the database. |
| 418 |
For an in-memory database, this means the content is lost. |
| 419 |
To keep the database open, add <code>;DB_CLOSE_DELAY=-1</code> to the database URL. |
| 420 |
To keep the content of an in-memory database as long as the virtual machine is alive, use |
| 421 |
<code>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</code>. |
| 422 |
This may create a memory leak, when you need to remove the database, use |
| 423 |
the <a href="commands.html#shutdown">SHUTDOWN</a> command. |
| 424 |
</p> |
| 425 |
|
| 426 |
<h2 id="file_encryption">Database Files Encryption</h2> |
| 427 |
<p> |
| 428 |
The database files can be encrypted. |
| 429 |
Three encryption algorithms are supported: |
| 430 |
</p> |
| 431 |
<ul> |
| 432 |
<li>"AES" - also known as Rijndael, only AES-128 is implemented.</li> |
| 433 |
<li>"XTEA" - the 32 round version.</li> |
| 434 |
<li>"FOG" - pseudo-encryption only useful for hiding data from a text editor.</li> |
| 435 |
</ul> |
| 436 |
<p> |
| 437 |
To use file encryption, you need to specify the encryption algorithm (the 'cipher') |
| 438 |
and the file password (in addition to the user password) when connecting to the database. |
| 439 |
</p> |
| 440 |
|
| 441 |
<h3>Creating a New Database with File Encryption</h3> |
| 442 |
<p> |
| 443 |
By default, a new database is automatically created if it does not exist yet |
| 444 |
when the <a href="#database_url">embedded</a> url is used. |
| 445 |
To create an encrypted database, connect to it as it would already exist locally using the embedded URL. |
| 446 |
</p> |
| 447 |
|
| 448 |
<h3>Connecting to an Encrypted Database</h3> |
| 449 |
<p> |
| 450 |
The encryption algorithm is set in the database URL, and the file password is specified in the password field, |
| 451 |
before the user password. A single space separates the file password |
| 452 |
and the user password; the file password itself may not contain spaces. File passwords |
| 453 |
and user passwords are case sensitive. Here is an example to connect to a |
| 454 |
password-encrypted database: |
| 455 |
</p> |
| 456 |
<pre> |
| 457 |
String url = "jdbc:h2:~/test;CIPHER=AES"; |
| 458 |
String user = "sa"; |
| 459 |
String pwds = "filepwd userpwd"; |
| 460 |
conn = DriverManager. |
| 461 |
getConnection(url, user, pwds); |
| 462 |
</pre> |
| 463 |
|
| 464 |
<h3>Encrypting or Decrypting a Database</h3> |
| 465 |
<p> |
| 466 |
To encrypt an existing database, use the <code>ChangeFileEncryption</code> tool. |
| 467 |
This tool can also decrypt an encrypted database, or change the file encryption key. |
| 468 |
The tool is available from within the H2 Console in the tools section, or you can run it from the command line. |
| 469 |
The following command line will encrypt the database <code>test</code> in the user home directory |
| 470 |
with the file password <code>filepwd</code> and the encryption algorithm AES: |
| 471 |
</p> |
| 472 |
<pre> |
| 473 |
java -cp h2*.jar org.h2.tools.ChangeFileEncryption -dir ~ -db test -cipher AES -encrypt filepwd |
| 474 |
</pre> |
| 475 |
|
| 476 |
<h2 id="database_file_locking">Database File Locking</h2> |
| 477 |
<p> |
| 478 |
Whenever a database is opened, a lock file is created to signal other processes |
| 479 |
that the database is in use. If database is closed, or if the process that opened |
| 480 |
the database terminates, this lock file is deleted. |
| 481 |
</p><p> |
| 482 |
The following file locking methods are implemented: |
| 483 |
</p> |
| 484 |
<ul> |
| 485 |
<li>The default method is <code>FILE</code> and uses a watchdog thread to |
| 486 |
protect the database file. The watchdog reads the lock file each second. |
| 487 |
</li><li>The second method is <code>SOCKET</code> and opens a server socket. |
| 488 |
The socket method does not require reading the lock file every second. |
| 489 |
The socket method should only be used if the database files |
| 490 |
are only accessed by one (and always the same) computer. |
| 491 |
</li><li>The third method is <code>FS</code>. |
| 492 |
This will use native file locking using <code>FileChannel.lock</code>. |
| 493 |
</li><li>It is also possible to open the database without file locking; |
| 494 |
in this case it is up to the application to protect the database files. |
| 495 |
Failing to do so will result in a corrupted database. |
| 496 |
Using the method <code>NO</code> forces the database to not create a lock file at all. |
| 497 |
Please note that this is unsafe as another process is able to open the same database, |
| 498 |
possibly leading to data corruption.</li></ul> |
| 499 |
<p> |
| 500 |
To open the database with a different file locking method, use the parameter |
| 501 |
<code>FILE_LOCK</code>. |
| 502 |
The following code opens the database with the 'socket' locking method: |
| 503 |
</p> |
| 504 |
<pre> |
| 505 |
String url = "jdbc:h2:~/test;FILE_LOCK=SOCKET"; |
| 506 |
</pre> |
| 507 |
<p> |
| 508 |
For more information about the algorithms, see |
| 509 |
<a href="advanced.html#file_locking_protocols">Advanced / File Locking Protocols</a>. |
| 510 |
</p> |
| 511 |
|
| 512 |
<h2 id="database_only_if_exists">Opening a Database Only if it Already Exists</h2> |
| 513 |
<p> |
| 514 |
By default, when an application calls <code>DriverManager.getConnection(url, ...)</code> |
| 515 |
with <a href="features.html#database_url">embedded</a> URL and the database specified in the URL does not yet exist, |
| 516 |
a new (empty) database is created. |
| 517 |
In some situations, it is better to restrict creating new databases, and only allow to open |
| 518 |
existing databases. To do this, add <code>;IFEXISTS=TRUE</code> |
| 519 |
to the database URL. In this case, if the database does not already exist, an exception is thrown when |
| 520 |
trying to connect. The connection only succeeds when the database already exists. |
| 521 |
The complete URL may look like this: |
| 522 |
</p> |
| 523 |
<pre> |
| 524 |
String url = "jdbc:h2:/data/sample;IFEXISTS=TRUE"; |
| 525 |
</pre> |
| 526 |
|
| 527 |
<h2 id="closing_a_database">Closing a Database</h2> |
| 528 |
|
| 529 |
<h3>Delayed Database Closing</h3> |
| 530 |
<p> |
| 531 |
Usually, a database is closed when the last connection to it is closed. In some situations |
| 532 |
this slows down the application, for example when it is not possible to keep at least one connection open. |
| 533 |
The automatic closing of a database can be delayed or disabled with the SQL statement |
| 534 |
<code>SET DB_CLOSE_DELAY <seconds></code>. |
| 535 |
The parameter <seconds> specifies the number of seconds to keep |
| 536 |
a database open after the last connection to it was closed. The following statement |
| 537 |
will keep a database open for 10 seconds after the last connection was closed: |
| 538 |
</p> |
| 539 |
<pre> |
| 540 |
SET DB_CLOSE_DELAY 10 |
| 541 |
</pre> |
| 542 |
<p> |
| 543 |
The value -1 means the database is not closed automatically. |
| 544 |
The value 0 is the default and means the database is closed when the last connection is closed. |
| 545 |
This setting is persistent and can be set by an administrator only. |
| 546 |
It is possible to set the value in the database URL: <code>jdbc:h2:~/test;DB_CLOSE_DELAY=10</code>. |
| 547 |
</p> |
| 548 |
|
| 549 |
<h3 id="do_not_close_on_exit">Don't Close a Database when the VM Exits</h3> |
| 550 |
<p> |
| 551 |
By default, a database is closed when the last connection is closed. However, if it is never closed, |
| 552 |
the database is closed when the virtual machine exits normally, using a shutdown hook. |
| 553 |
In some situations, the database should not be closed in this case, for example because the |
| 554 |
database is still used at virtual machine shutdown (to store the shutdown process in the database for example). |
| 555 |
For those cases, the automatic closing of the database can be disabled in the database URL. |
| 556 |
The first connection (the one that is opening the database) needs to |
| 557 |
set the option in the database URL (it is not possible to change the setting afterwards). |
| 558 |
The database URL to disable database closing on exit is: |
| 559 |
</p> |
| 560 |
<pre> |
| 561 |
String url = "jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE"; |
| 562 |
</pre> |
| 563 |
|
| 564 |
<h2 id="execute_sql_on_connection">Execute SQL on Connection</h2> |
| 565 |
<p> |
| 566 |
Sometimes, particularly for in-memory databases, it is useful to be able to execute DDL or DML |
| 567 |
commands automatically when a client connects to a database. This functionality is enabled via |
| 568 |
the INIT property. Note that multiple commands may be passed to INIT, but the semicolon delimiter |
| 569 |
must be escaped, as in the example below. |
| 570 |
</p> |
| 571 |
<pre> |
| 572 |
String url = "jdbc:h2:mem:test;INIT=runscript from '~/create.sql'\\;runscript from '~/init.sql'"; |
| 573 |
</pre> |
| 574 |
<p> |
| 575 |
Please note the double backslash is only required in a Java or properties file. |
| 576 |
In a GUI, or in an XML file, only one backslash is required: |
| 577 |
</p> |
| 578 |
<pre> |
| 579 |
<property name="url" value= |
| 580 |
"jdbc:h2:mem:test;INIT=create schema if not exists test\;runscript from '~/sql/init.sql'" |
| 581 |
/> |
| 582 |
</pre> |
| 583 |
<p> |
| 584 |
Backslashes within the init script (for example within a runscript statement, to specify the folder names in Windows) |
| 585 |
need to be escaped as well (using a second backslash). It might be simpler to avoid backslashes in folder names for this reason; |
| 586 |
use forward slashes instead. |
| 587 |
</p> |
| 588 |
|
| 589 |
<h2 id="ignore_unknown_settings">Ignore Unknown Settings</h2> |
| 590 |
<p> |
| 591 |
Some applications (for example OpenOffice.org Base) pass some additional parameters |
| 592 |
when connecting to the database. Why those parameters are passed is unknown. |
| 593 |
The parameters <code>PREFERDOSLIKELINEENDS</code> and |
| 594 |
<code>IGNOREDRIVERPRIVILEGES</code> are such examples; |
| 595 |
they are simply ignored to improve the compatibility with OpenOffice.org. If an application |
| 596 |
passes other parameters when connecting to the database, usually the database throws an exception |
| 597 |
saying the parameter is not supported. It is possible to ignored such parameters by adding |
| 598 |
<code>;IGNORE_UNKNOWN_SETTINGS=TRUE</code> to the database URL. |
| 599 |
</p> |
| 600 |
|
| 601 |
<h2 id="other_settings">Changing Other Settings when Opening a Connection</h2> |
| 602 |
<p> |
| 603 |
In addition to the settings already described, |
| 604 |
other database settings can be passed in the database URL. |
| 605 |
Adding <code>;setting=value</code> at the end of a database URL is the |
| 606 |
same as executing the statement <code>SET setting value</code> just after |
| 607 |
connecting. For a list of supported settings, see <a href="grammar.html">SQL Grammar</a> |
| 608 |
or the <a href="https://h2database.com/javadoc/org/h2/engine/DbSettings.html">DbSettings</a> javadoc. |
| 609 |
</p> |
| 610 |
|
| 611 |
<h2 id="custom_access_mode">Custom File Access Mode</h2> |
| 612 |
<p> |
| 613 |
Usually, the database opens the database file with the access mode |
| 614 |
<code>rw</code>, meaning read-write (except for read only databases, |
| 615 |
where the mode <code>r</code> is used). |
| 616 |
To open a database in read-only mode if the database file is not read-only, use |
| 617 |
<code>ACCESS_MODE_DATA=r</code>. |
| 618 |
Also supported are <code>rws</code> and <code>rwd</code>. |
| 619 |
This setting must be specified in the database URL: |
| 620 |
</p> |
| 621 |
<pre> |
| 622 |
String url = "jdbc:h2:~/test;ACCESS_MODE_DATA=rws"; |
| 623 |
</pre> |
| 624 |
<p> |
| 625 |
For more information see <a href="advanced.html#durability_problems">Durability Problems</a>. |
| 626 |
On many operating systems the access mode <code>rws</code> does not guarantee that the data is written to the disk. |
| 627 |
</p> |
| 628 |
|
| 629 |
<h2 id="multiple_connections">Multiple Connections</h2> |
| 630 |
|
| 631 |
<h3>Opening Multiple Databases at the Same Time</h3> |
| 632 |
<p> |
| 633 |
An application can open multiple databases at the same time, including multiple |
| 634 |
connections to the same database. The number of open database is only limited by the memory available. |
| 635 |
</p> |
| 636 |
|
| 637 |
<h3>Multiple Connections to the Same Database: Client/Server</h3> |
| 638 |
<p> |
| 639 |
If you want to access the same database at the same time from different processes or computers, |
| 640 |
you need to use the client / server mode. In this case, one process acts as the server, and the |
| 641 |
other processes (that could reside on other computers as well) connect to the server via TCP/IP |
| 642 |
(or TLS over TCP/IP for improved security). |
| 643 |
</p> |
| 644 |
|
| 645 |
<h3>Multithreading Support</h3> |
| 646 |
<p> |
| 647 |
This database is multithreading-safe. |
| 648 |
If an application is multi-threaded, it does not need to worry about synchronizing access to the database. |
| 649 |
An application should normally use one connection per thread. |
| 650 |
This database synchronizes access to the same connection, but other databases may not do this. |
| 651 |
To get higher concurrency, you need to use multiple connections. |
| 652 |
</p> |
| 653 |
<p> |
| 654 |
An application can use multiple threads that access the same database at the same time. |
| 655 |
Threads that use different connections can use the database concurrently. |
| 656 |
</p> |
| 657 |
|
| 658 |
<h3>Locking, Lock-Timeout, Deadlocks</h3> |
| 659 |
<p> |
| 660 |
Usually, <code>SELECT</code> statements will generate read locks. This includes subqueries. |
| 661 |
Statements that modify data use write locks on the modified rows. |
| 662 |
It is also possible to issue write locks without modifying data, |
| 663 |
using the statement <code>SELECT ... FOR UPDATE</code>. |
| 664 |
Data definition statements may issue exclusive locks on tables. |
| 665 |
The statements <code>COMMIT</code> and |
| 666 |
<code>ROLLBACK</code> releases all open locks. |
| 667 |
The commands <code>SAVEPOINT</code> and |
| 668 |
<code>ROLLBACK TO SAVEPOINT</code> don't affect locks. |
| 669 |
The locks are also released when the autocommit mode changes, and for connections with |
| 670 |
autocommit set to true (this is the default), locks are released after each statement. |
| 671 |
The following statements generate locks: |
| 672 |
</p> |
| 673 |
<table class="main"> |
| 674 |
<tr> |
| 675 |
<th>Type of Lock</th> |
| 676 |
<th>SQL Statement</th> |
| 677 |
</tr> |
| 678 |
<tr> |
| 679 |
<td>Read</td> |
| 680 |
<td class="notranslate">SELECT * FROM TEST;<br /> |
| 681 |
CALL SELECT MAX(ID) FROM TEST;<br /> |
| 682 |
SCRIPT;</td> |
| 683 |
</tr> |
| 684 |
<tr> |
| 685 |
<td>Write (row-level)</td> |
| 686 |
<td class="notranslate">SELECT * FROM TEST WHERE 1=0 FOR UPDATE;</td> |
| 687 |
</tr> |
| 688 |
<tr> |
| 689 |
<td>Write (row-level)</td> |
| 690 |
<td class="notranslate">INSERT INTO TEST VALUES(1, 'Hello');<br /> |
| 691 |
INSERT INTO TEST SELECT * FROM TEST;<br /> |
| 692 |
UPDATE TEST SET NAME='Hi';<br /> |
| 693 |
DELETE FROM TEST;</td> |
| 694 |
</tr> |
| 695 |
<tr> |
| 696 |
<td>Exclusive</td> |
| 697 |
<td class="notranslate">ALTER TABLE TEST ...;<br /> |
| 698 |
CREATE INDEX ... ON TEST ...;<br /> |
| 699 |
DROP INDEX ...;</td> |
| 700 |
</tr> |
| 701 |
</table> |
| 702 |
<p> |
| 703 |
The number of seconds until a lock timeout exception is thrown can be |
| 704 |
set separately for each connection using the SQL command |
| 705 |
<code>SET LOCK_TIMEOUT <milliseconds></code>. |
| 706 |
The initial lock timeout (that is the timeout used for new connections) can be set using the SQL command |
| 707 |
<code>SET DEFAULT_LOCK_TIMEOUT <milliseconds></code>. The default lock timeout is persistent. |
| 708 |
</p> |
| 709 |
|
| 710 |
<h2 id="database_file_layout">Database File Layout</h2> |
| 711 |
<p> |
| 712 |
The following files are created for persistent databases: |
| 713 |
</p> |
| 714 |
<table class="main"> |
| 715 |
<tr><th>File Name</th><th>Description</th><th>Number of Files</th></tr> |
| 716 |
<tr><td class="notranslate"> |
| 717 |
test.mv.db |
| 718 |
</td><td> |
| 719 |
Database file.<br /> |
| 720 |
Contains the transaction log, indexes, and data for all tables.<br /> |
| 721 |
Format: <code><database>.mv.db</code> |
| 722 |
</td><td> |
| 723 |
1 per database |
| 724 |
</td></tr> |
| 725 |
<tr><td class="notranslate"> |
| 726 |
test.newFile |
| 727 |
</td><td> |
| 728 |
Temporary file for database compaction.<br /> |
| 729 |
Contains the new MVStore file.<br /> |
| 730 |
Format: <code><database>.newFile</code> |
| 731 |
</td><td> |
| 732 |
0 or 1 per database |
| 733 |
</td></tr> |
| 734 |
<tr><td class="notranslate"> |
| 735 |
test.tempFile |
| 736 |
</td><td> |
| 737 |
Temporary file for database compaction.<br /> |
| 738 |
Contains the temporary MVStore file.<br /> |
| 739 |
Format: <code><database>.tempFile</code> |
| 740 |
</td><td> |
| 741 |
0 or 1 per database |
| 742 |
</td></tr> |
| 743 |
<tr><td class="notranslate"> |
| 744 |
test.lock.db |
| 745 |
</td><td> |
| 746 |
Database lock file.<br /> |
| 747 |
Automatically (re-)created while the database is in use.<br /> |
| 748 |
Format: <code><database>.lock.db</code> |
| 749 |
</td><td> |
| 750 |
1 per database (only if in use) |
| 751 |
</td></tr> |
| 752 |
<tr><td class="notranslate"> |
| 753 |
test.trace.db |
| 754 |
</td><td> |
| 755 |
Trace file (if the trace option is enabled).<br /> |
| 756 |
Contains trace information.<br /> |
| 757 |
Format: <code><database>.trace.db</code><br /> |
| 758 |
Renamed to <code><database>.trace.db.old</code> if too big. |
| 759 |
</td><td> |
| 760 |
0 or 1 per database |
| 761 |
</td></tr> |
| 762 |
<tr><td class="notranslate"> |
| 763 |
test.123.temp.db |
| 764 |
</td><td> |
| 765 |
Temporary file.<br /> |
| 766 |
Contains a temporary blob or a large result set.<br /> |
| 767 |
Format: <code><database>.<id>.temp.db</code> |
| 768 |
</td><td> |
| 769 |
1 per object |
| 770 |
</td></tr> |
| 771 |
</table> |
| 772 |
|
| 773 |
<h3>Moving and Renaming Database Files</h3> |
| 774 |
<p> |
| 775 |
Database name and location are not stored inside the database files. |
| 776 |
</p><p> |
| 777 |
While a database is closed, the files can be moved to another directory, and they can |
| 778 |
be renamed as well (as long as all files of the same database start with the same |
| 779 |
name and the respective extensions are unchanged). |
| 780 |
</p><p> |
| 781 |
As there is no platform specific data in the files, they can be moved to other operating systems |
| 782 |
without problems. |
| 783 |
</p> |
| 784 |
|
| 785 |
<h3>Backup</h3> |
| 786 |
<p> |
| 787 |
When the database is closed, it is possible to backup the database files. |
| 788 |
</p><p> |
| 789 |
To backup data while the database is running, |
| 790 |
the SQL commands <code>SCRIPT</code> and <code>BACKUP</code> can be used. |
| 791 |
</p> |
| 792 |
|
| 793 |
<h2 id="logging_recovery">Logging and Recovery</h2> |
| 794 |
<p> |
| 795 |
Whenever data is modified in the database and those changes are committed, the changes are written |
| 796 |
to the transaction log (except for in-memory objects). The changes to the main data area itself are usually written |
| 797 |
later on, to optimize disk access. If there is a power failure, the main data area is not up-to-date, |
| 798 |
but because the changes are in the transaction log, the next time the database is opened, the changes |
| 799 |
are re-applied automatically. |
| 800 |
</p> |
| 801 |
|
| 802 |
<h2 id="compatibility">Compatibility</h2> |
| 803 |
<p> |
| 804 |
All database engines behave a little bit different. Where possible, H2 supports the ANSI SQL standard, |
| 805 |
and tries to be compatible to other databases. There are still a few differences however: |
| 806 |
</p> |
| 807 |
<p> |
| 808 |
In MySQL text columns are case insensitive by default, while in H2 they are case sensitive. However |
| 809 |
H2 supports case insensitive columns as well. To create the tables with case insensitive texts, append |
| 810 |
<code>IGNORECASE=TRUE</code> to the database URL |
| 811 |
(example: <code>jdbc:h2:~/test;IGNORECASE=TRUE</code>). |
| 812 |
</p> |
| 813 |
|
| 814 |
<h3 id="compatibility_modes">Compatibility Modes</h3> |
| 815 |
<p> |
| 816 |
For certain features, this database can emulate the behavior of specific databases. |
| 817 |
However, only a small subset of the differences between databases are implemented in this way. |
| 818 |
Here is the list of currently supported modes and the differences to the regular mode: |
| 819 |
</p> |
| 820 |
|
| 821 |
<h3>REGULAR Compatibility mode</h3> |
| 822 |
<p> |
| 823 |
This mode is used by default. |
| 824 |
</p> |
| 825 |
<ul><li>Empty IN predicate is allowed. |
| 826 |
</li><li>TOP clause in SELECT is allowed. |
| 827 |
</li><li>OFFSET/LIMIT clauses are allowed. |
| 828 |
</li><li>MINUS can be used instead of EXCEPT. |
| 829 |
</li><li>IDENTITY can be used as a data type. |
| 830 |
</li><li>Legacy SERIAL and BIGSERIAL data types are supported. |
| 831 |
</li><li>AUTO_INCREMENT clause can be used instead of GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY. |
| 832 |
</li></ul> |
| 833 |
|
| 834 |
<h3>STRICT Compatibility Mode</h3> |
| 835 |
<p> |
| 836 |
To use the STRICT mode, use the database URL <code>jdbc:h2:~/test;MODE=STRICT</code> |
| 837 |
or the SQL statement <code>SET MODE STRICT</code>. |
| 838 |
In this mode some deprecated features are disabled. |
| 839 |
</p> |
| 840 |
<p> |
| 841 |
If your application or library uses only the H2 or it generates different SQL for different database systems |
| 842 |
it is recommended to use this compatibility mode in unit tests |
| 843 |
to reduce possibility of accidental misuse of such features. |
| 844 |
This mode cannot be used as SQL validator, however. |
| 845 |
</p> |
| 846 |
<p> |
| 847 |
It is not recommended to enable this mode in production builds of libraries, |
| 848 |
because this mode may become more restrictive in future releases of H2 that may break your library |
| 849 |
if it will be used together with newer version of H2. |
| 850 |
</p> |
| 851 |
<ul><li>Empty IN predicate is disallowed. |
| 852 |
</li><li>TOP and OFFSET/LIMIT clauses are disallowed, only OFFSET/FETCH can be used. |
| 853 |
</li><li>MINUS cannot be used instead of EXCEPT. |
| 854 |
</li><li>IDENTITY cannot be used as a data type and AUTO_INCREMENT clause cannot be specified. |
| 855 |
Use GENERATED BY DEFAULT AS IDENTITY clause instead. |
| 856 |
</li><li>SERIAL and BIGSERIAL data types are disallowed. |
| 857 |
Use INTEGER GENERATED BY DEFAULT AS IDENTITY or BIGINT GENERATED BY DEFAULT AS IDENTITY instead. |
| 858 |
</li></ul> |
| 859 |
|
| 860 |
<h3>LEGACY Compatibility Mode</h3> |
| 861 |
<p> |
| 862 |
To use the LEGACY mode, use the database URL <code>jdbc:h2:~/test;MODE=LEGACY</code> |
| 863 |
or the SQL statement <code>SET MODE LEGACY</code>. |
| 864 |
In this mode some compatibility features for applications written for H2 1.X are enabled. |
| 865 |
This mode doesn't provide full compatibility with H2 1.X. |
| 866 |
</p> |
| 867 |
<ul><li>Empty IN predicate is allowed. |
| 868 |
</li><li>TOP clause in SELECT is allowed. |
| 869 |
</li><li>OFFSET/LIMIT clauses are allowed. |
| 870 |
</li><li>MINUS can be used instead of EXCEPT. |
| 871 |
</li><li>IDENTITY can be used as a data type. |
| 872 |
</li><li>MS SQL Server-style IDENTITY clause is supported. |
| 873 |
</li><li>Legacy SERIAL and BIGSERIAL data types are supported. |
| 874 |
</li><li>AUTO_INCREMENT clause can be used instead of GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY. |
| 875 |
</li><li>If a value for identity column was specified in an INSERT command |
| 876 |
the base value of sequence generator of this column is updated if current value of generator was smaller |
| 877 |
(larger for generators with negative increment) than the inserted value. |
| 878 |
</li><li>Identity columns have implicit DEFAULT ON NULL clause. |
| 879 |
It means a NULL value may be specified for this column in INSERT command and it will be treated as DEFAULT. |
| 880 |
</li><li>Oracle-style CURRVAL and NEXTVAL can be used on sequences. |
| 881 |
</li><li>TOP clause can be used in DELETE and UPDATE. |
| 882 |
</li><li>Non-standard Oracle-style WHERE clause can be used in standard MERGE command. |
| 883 |
</li><li>Attempt to reference a non-unique set of columns from a referential constraint |
| 884 |
will create an UNIQUE constraint on them automatically. |
| 885 |
</li><li>Unsafe comparison operators between numeric and boolean values are allowed. |
| 886 |
</li><li>IDENTITY() and SCOPE_IDENTITY() are supported, but both are implemented like SCOPE_IDENTITY() |
| 887 |
</li><li>SYSDATE, SYSTIMESTAMP, and TODAY are supported. |
| 888 |
</li></ul> |
| 889 |
|
| 890 |
<h3>DB2 Compatibility Mode</h3> |
| 891 |
<p> |
| 892 |
To use the IBM DB2 mode, use the database URL <code>jdbc:h2:~/test;MODE=DB2;DEFAULT_NULL_ORDERING=HIGH</code> |
| 893 |
or the SQL statement <code>SET MODE DB2</code>. |
| 894 |
</p> |
| 895 |
<ul><li>For aliased columns, <code>ResultSetMetaData.getColumnName()</code> |
| 896 |
returns the alias name and <code>getTableName()</code> returns |
| 897 |
<code>null</code>. |
| 898 |
</li><li>Support the pseudo-table SYSIBM.SYSDUMMY1. |
| 899 |
</li><li>Timestamps with dash between date and time are supported. |
| 900 |
</li><li>Datetime value functions return the same value within a command. |
| 901 |
</li><li>Second and third arguments of TRANSLATE() function are swapped. |
| 902 |
</li><li>SEQUENCE.NEXTVAL and SEQUENCE.CURRVAL are supported |
| 903 |
</li><li>LIMIT / OFFSET clauses are supported. |
| 904 |
</li><li>MINUS can be used instead of EXCEPT. |
| 905 |
</li><li>Unsafe comparison operators between numeric and boolean values are allowed. |
| 906 |
</li></ul> |
| 907 |
|
| 908 |
<h3>Derby Compatibility Mode</h3> |
| 909 |
<p> |
| 910 |
To use the Apache Derby mode, use the database URL <code>jdbc:h2:~/test;MODE=Derby;DEFAULT_NULL_ORDERING=HIGH</code> |
| 911 |
or the SQL statement <code>SET MODE Derby</code>. |
| 912 |
</p> |
| 913 |
<ul><li>For aliased columns, <code>ResultSetMetaData.getColumnName()</code> |
| 914 |
returns the alias name and <code>getTableName()</code> returns |
| 915 |
<code>null</code>. |
| 916 |
</li><li>For unique indexes, <code>NULL</code> is distinct. |
| 917 |
That means only one row with <code>NULL</code> in one of the columns is allowed. |
| 918 |
</li><li>Support the pseudo-table SYSIBM.SYSDUMMY1. |
| 919 |
</li><li>Datetime value functions return the same value within a command. |
| 920 |
</li></ul> |
| 921 |
|
| 922 |
<h3>HSQLDB Compatibility Mode</h3> |
| 923 |
<p> |
| 924 |
To use the HSQLDB mode, use the database URL <code>jdbc:h2:~/test;MODE=HSQLDB;DEFAULT_NULL_ORDERING=FIRST</code> |
| 925 |
or the SQL statement <code>SET MODE HSQLDB</code>. |
| 926 |
</p> |
| 927 |
<ul><li>Text can be concatenated using '+'. |
| 928 |
</li><li>NULL value works like DEFAULT value is assignments to identity columns. |
| 929 |
</li><li>Datetime value functions return the same value within a command. |
| 930 |
</li><li>TOP clause in SELECT is supported. |
| 931 |
</li><li>LIMIT / OFFSET clauses are supported. |
| 932 |
</li><li>MINUS can be used instead of EXCEPT. |
| 933 |
</li><li>Unsafe comparison operators between numeric and boolean values are allowed. |
| 934 |
</li><li>SYSDATE and TODAY are supported. |
| 935 |
</li></ul> |
| 936 |
|
| 937 |
<h3>MS SQL Server Compatibility Mode</h3> |
| 938 |
<p> |
| 939 |
To use the MS SQL Server mode, use the database URL |
| 940 |
<code>jdbc:h2:~/test;MODE=MSSQLServer;DATABASE_TO_UPPER=FALSE;CASE_INSENSITIVE_IDENTIFIERS=TRUE</code>. |
| 941 |
Do not change value of DATABASE_TO_LOWER and CASE_INSENSITIVE_IDENTIFIERS after creation of database. |
| 942 |
</p> |
| 943 |
<ul><li>For aliased columns, <code>ResultSetMetaData.getColumnName()</code> |
| 944 |
returns the alias name and <code>getTableName()</code> returns |
| 945 |
<code>null</code>. |
| 946 |
</li><li>Identifiers may be quoted using square brackets as in <code>[Test]</code>. |
| 947 |
</li><li>For unique indexes, <code>NULL</code> is distinct. |
| 948 |
That means only one row with <code>NULL</code> in one of the columns is allowed. |
| 949 |
</li><li>Text can be concatenated using '+'. |
| 950 |
</li><li>Arguments of LOG() function are swapped. |
| 951 |
</li><li>MONEY data type is treated like NUMERIC(19, 4) data type. SMALLMONEY data type is treated like NUMERIC(10, 4) |
| 952 |
data type. |
| 953 |
</li><li><code>IDENTITY</code> can be used for automatic id generation on column level. |
| 954 |
</li><li>Table hints are discarded. Example: <code>SELECT * FROM table WITH (NOLOCK)</code>. |
| 955 |
</li><li>Datetime value functions return the same value within a command. |
| 956 |
</li><li>0x literals are parsed as binary string literals. |
| 957 |
</li><li>TRUNCATE TABLE restarts next values of generated columns. |
| 958 |
</li><li>TOP clause in SELECT, UPDATE, and DELETE is supported. |
| 959 |
</li><li>Unsafe comparison operators between numeric and boolean values are allowed. |
| 960 |
</li></ul> |
| 961 |
|
| 962 |
<h3>MariaDB Compatibility Mode</h3> |
| 963 |
<p> |
| 964 |
To use the MariaDB mode, use the database URL <code>jdbc:h2:~/test;MODE=MariaDB;DATABASE_TO_LOWER=TRUE</code>. |
| 965 |
When case-insensitive identifiers are needed append <code>;CASE_INSENSITIVE_IDENTIFIERS=TRUE</code> to URL. |
| 966 |
Do not change value of DATABASE_TO_LOWER after creation of database. |
| 967 |
</p> |
| 968 |
<ul><li>Creating indexes in the <code>CREATE TABLE</code> statement is allowed using |
| 969 |
<code>INDEX(..)</code> or <code>KEY(..)</code>. |
| 970 |
Example: <code>create table test(id int primary key, name varchar(255), key idx_name(name));</code> |
| 971 |
</li><li>When converting a floating point number to an integer, the fractional |
| 972 |
digits are not truncated, but the value is rounded. |
| 973 |
</li><li>ON DUPLICATE KEY UPDATE is supported in INSERT statements, due to this feature VALUES has special non-standard |
| 974 |
meaning is some contexts. |
| 975 |
</li><li>INSERT IGNORE is partially supported and may be used to skip rows with duplicate keys if ON DUPLICATE KEY |
| 976 |
UPDATE is not specified. |
| 977 |
</li><li>REPLACE INTO is partially supported. |
| 978 |
</li><li>Spaces are trimmed from the right side of CHAR values. |
| 979 |
</li><li>REGEXP_REPLACE() uses \ for back-references. |
| 980 |
</li><li>Datetime value functions return the same value within a command. |
| 981 |
</li><li>0x literals are parsed as binary string literals. |
| 982 |
</li><li>Unrelated expressions in ORDER BY clause of DISTINCT queries are allowed. |
| 983 |
</li><li>Some MariaDB-specific ALTER TABLE commands are partially supported. |
| 984 |
</li><li>TRUNCATE TABLE restarts next values of generated columns. |
| 985 |
</li><li>NEXT VALUE FOR returns different values when invoked multiple times within the same row. |
| 986 |
</li><li>If value of an identity column was manually specified, its sequence is updated to generate values after |
| 987 |
inserted. |
| 988 |
</li><li>NULL value works like DEFAULT value is assignments to identity columns. |
| 989 |
</li><li>LIMIT / OFFSET clauses are supported. |
| 990 |
</li><li>AUTO_INCREMENT clause can be used. |
| 991 |
</li><li>YEAR data type is treated like SMALLINT data type. |
| 992 |
</li><li>GROUP BY clause can contain 1-based positions of expressions from the SELECT list. |
| 993 |
</li><li>Unsafe comparison operators between numeric and boolean values are allowed. |
| 994 |
</li><li>Accepts non-standard JSON_OBJECT and JSON_OBJECTAGG syntax using comma as key/value separator. |
| 995 |
</li></ul> |
| 996 |
<p> |
| 997 |
Text comparison in MariaDB is case insensitive by default, while in H2 it is case sensitive (as in most other databases). |
| 998 |
H2 does support case insensitive text comparison, but it needs to be set separately, |
| 999 |
using <code>SET IGNORECASE TRUE</code>. |
| 1000 |
This affects comparison using <code>=, LIKE, REGEXP</code>. |
| 1001 |
</p> |
| 1002 |
|
| 1003 |
<h3>MySQL Compatibility Mode</h3> |
| 1004 |
<p> |
| 1005 |
To use the MySQL mode, use the database URL <code>jdbc:h2:~/test;MODE=MySQL;DATABASE_TO_LOWER=TRUE</code>. |
| 1006 |
When case-insensitive identifiers are needed append <code>;CASE_INSENSITIVE_IDENTIFIERS=TRUE</code> to URL. |
| 1007 |
Do not change value of DATABASE_TO_LOWER after creation of database. |
| 1008 |
</p> |
| 1009 |
<ul><li>Creating indexes in the <code>CREATE TABLE</code> statement is allowed using |
| 1010 |
<code>INDEX(..)</code> or <code>KEY(..)</code>. |
| 1011 |
Example: <code>create table test(id int primary key, name varchar(255), key idx_name(name));</code> |
| 1012 |
</li><li>When converting a floating point number to an integer, the fractional |
| 1013 |
digits are not truncated, but the value is rounded. |
| 1014 |
</li><li>ON DUPLICATE KEY UPDATE is supported in INSERT statements, due to this feature VALUES has special non-standard |
| 1015 |
meaning is some contexts. |
| 1016 |
</li><li>INSERT IGNORE is partially supported and may be used to skip rows with duplicate keys if ON DUPLICATE KEY |
| 1017 |
UPDATE is not specified. |
| 1018 |
</li><li>REPLACE INTO is partially supported. |
| 1019 |
</li><li>Spaces are trimmed from the right side of CHAR values. |
| 1020 |
</li><li>REGEXP_REPLACE() uses \ for back-references. |
| 1021 |
</li><li>Datetime value functions return the same value within a command. |
| 1022 |
</li><li>0x literals are parsed as binary string literals. |
| 1023 |
</li><li>Unrelated expressions in ORDER BY clause of DISTINCT queries are allowed. |
| 1024 |
</li><li>Some MySQL-specific ALTER TABLE commands are partially supported. |
| 1025 |
</li><li>TRUNCATE TABLE restarts next values of generated columns. |
| 1026 |
</li><li>If value of an identity column was manually specified, its sequence is updated to generate values after |
| 1027 |
inserted. |
| 1028 |
</li><li>NULL value works like DEFAULT value is assignments to identity columns. |
| 1029 |
</li><li>Referential constraints don't require an existing primary key or unique constraint on referenced columns |
| 1030 |
and create a unique constraint automatically if such constraint doesn't exist. |
| 1031 |
</li><li>LIMIT / OFFSET clauses are supported. |
| 1032 |
</li><li>AUTO_INCREMENT clause can be used. |
| 1033 |
</li><li>YEAR data type is treated like SMALLINT data type. |
| 1034 |
</li><li>GROUP BY clause can contain 1-based positions of expressions from the SELECT list. |
| 1035 |
</li><li>Unsafe comparison operators between numeric and boolean values are allowed. |
| 1036 |
</li><li>Accepts non-standard JSON_OBJECT and JSON_OBJECTAGG syntax using comma as key/value separator. |
| 1037 |
</li></ul> |
| 1038 |
<p> |
| 1039 |
Text comparison in MySQL is case insensitive by default, while in H2 it is case sensitive (as in most other databases). |
| 1040 |
H2 does support case insensitive text comparison, but it needs to be set separately, |
| 1041 |
using <code>SET IGNORECASE TRUE</code>. |
| 1042 |
This affects comparison using <code>=, LIKE, REGEXP</code>. |
| 1043 |
</p> |
| 1044 |
|
| 1045 |
<h3>Oracle Compatibility Mode</h3> |
| 1046 |
<p> |
| 1047 |
To use the Oracle mode, use the database URL <code>jdbc:h2:~/test;MODE=Oracle;DEFAULT_NULL_ORDERING=HIGH</code> |
| 1048 |
or the SQL statement <code>SET MODE Oracle</code>. |
| 1049 |
</p> |
| 1050 |
<ul><li>For aliased columns, <code>ResultSetMetaData.getColumnName()</code> |
| 1051 |
returns the alias name and <code>getTableName()</code> returns |
| 1052 |
<code>null</code>. |
| 1053 |
</li><li>When using unique indexes, multiple rows with <code>NULL</code> |
| 1054 |
in all columns are allowed, however it is not allowed to have multiple rows with the |
| 1055 |
same values otherwise. |
| 1056 |
</li><li>Empty strings are treated like <code>NULL</code> values, concatenating <code>NULL</code> with another value |
| 1057 |
results in the other value. |
| 1058 |
</li><li>REGEXP_REPLACE() uses \ for back-references. |
| 1059 |
</li><li>RAWTOHEX() converts character strings to hexadecimal representation of their UTF-8 encoding. |
| 1060 |
</li><li>HEXTORAW() decodes a hexadecimal character string to a binary string. |
| 1061 |
</li><li>DATE data type is treated like TIMESTAMP(0) data type. |
| 1062 |
</li><li>Datetime value functions return the same value within a command. |
| 1063 |
</li><li>ALTER TABLE MODIFY COLUMN command is partially supported. |
| 1064 |
</li><li>SEQUENCE.NEXTVAL and SEQUENCE.CURRVAL are supported and return values with DECIMAL/NUMERIC data type. |
| 1065 |
</li><li>Merge when matched clause may have WHERE clause. |
| 1066 |
</li><li>MINUS can be used instead of EXCEPT. |
| 1067 |
</li><li>SYSDATE and SYSTIMESTAMP are supported. |
| 1068 |
</li></ul> |
| 1069 |
|
| 1070 |
<h3>PostgreSQL Compatibility Mode</h3> |
| 1071 |
<p> |
| 1072 |
To use the PostgreSQL mode, use the database URL |
| 1073 |
<code>jdbc:h2:~/test;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH</code>. |
| 1074 |
Do not change value of DATABASE_TO_LOWER after creation of database. |
| 1075 |
</p> |
| 1076 |
<ul><li>For aliased columns, <code>ResultSetMetaData.getColumnName()</code> |
| 1077 |
returns the alias name and <code>getTableName()</code> returns |
| 1078 |
<code>null</code>. |
| 1079 |
</li><li>When converting a floating point number to an integer, the fractional |
| 1080 |
digits are not be truncated, but the value is rounded. |
| 1081 |
</li><li>The system columns <code>ctid</code> and |
| 1082 |
<code>oid</code> are supported. |
| 1083 |
</li><li>LOG(x) is base 10 in this mode. |
| 1084 |
</li><li>REGEXP_REPLACE(): |
| 1085 |
<ul> |
| 1086 |
<li>uses \ for back-references;</li> |
| 1087 |
<li>does not throw an exception when the <code>flagsString</code> parameter contains a 'g';</li> |
| 1088 |
<li>replaces only the first matched substring in the absence of the 'g' flag in the <code>flagsString</code> parameter.</li> |
| 1089 |
</ul> |
| 1090 |
</li><li>LIMIT / OFFSET clauses are supported. |
| 1091 |
</li><li>Legacy SERIAL and BIGSERIAL data types are supported. |
| 1092 |
</li><li>ON CONFLICT DO NOTHING is supported in INSERT statements. |
| 1093 |
</li><li>Spaces are trimmed from the right side of CHAR values, but CHAR values in result sets are right-padded with |
| 1094 |
spaces to the declared length. |
| 1095 |
</li><li>NUMERIC and DECIMAL/DEC data types without parameters are treated like DECFLOAT data type. |
| 1096 |
</li><li>MONEY data type is treated like NUMERIC(19, 2) data type. |
| 1097 |
</li><li>Datetime value functions return the same value within a transaction. |
| 1098 |
</li><li>ARRAY_SLICE() out of bounds parameters are silently corrected. |
| 1099 |
</li><li>EXTRACT function with DOW field returns (0-6), Sunday is 0. |
| 1100 |
</li><li>UPDATE with FROM is partially supported. |
| 1101 |
</li><li>GROUP BY clause can contain 1-based positions of expressions from the SELECT list. |
| 1102 |
</li></ul> |
| 1103 |
|
| 1104 |
<h2 id="auto_reconnect">Auto-Reconnect</h2> |
| 1105 |
<p> |
| 1106 |
The auto-reconnect feature causes the JDBC driver to reconnect to |
| 1107 |
the database if the connection is lost. The automatic re-connect only |
| 1108 |
occurs when auto-commit is enabled; if auto-commit is disabled, an exception is thrown. |
| 1109 |
To enable this mode, append <code>;AUTO_RECONNECT=TRUE</code> to the database URL. |
| 1110 |
</p> |
| 1111 |
<p> |
| 1112 |
Re-connecting will open a new session. After an automatic re-connect, |
| 1113 |
variables and local temporary tables definitions (excluding data) are re-created. |
| 1114 |
The contents of the system table <code>INFORMATION_SCHEMA.SESSION_STATE</code> |
| 1115 |
contains all client side state that is re-created. |
| 1116 |
</p> |
| 1117 |
<p> |
| 1118 |
If another connection uses the database in exclusive mode (enabled using <code>SET EXCLUSIVE 1</code> |
| 1119 |
or <code>SET EXCLUSIVE 2</code>), then this connection will try to re-connect until the exclusive mode ends. |
| 1120 |
</p> |
| 1121 |
|
| 1122 |
<h2 id="auto_mixed_mode">Automatic Mixed Mode</h2> |
| 1123 |
<p> |
| 1124 |
Multiple processes can access the same database without having to start the server manually. |
| 1125 |
To do that, append <code>;AUTO_SERVER=TRUE</code> to the database URL. |
| 1126 |
You can use the same database URL independent of whether the database is already open or not. |
| 1127 |
This feature doesn't work with in-memory databases. Example database URL: |
| 1128 |
</p> |
| 1129 |
<pre> |
| 1130 |
jdbc:h2:/data/test;AUTO_SERVER=TRUE |
| 1131 |
</pre> |
| 1132 |
<p> |
| 1133 |
Use the same URL for all connections to this database. Internally, when using this mode, |
| 1134 |
the first connection to the database is made in embedded mode, and additionally a server |
| 1135 |
is started internally (as a daemon thread). If the database is already open in another process, |
| 1136 |
the server mode is used automatically. The IP address and port of the server are stored in the file |
| 1137 |
<code>.lock.db</code>, that's why in-memory databases can't be supported. |
| 1138 |
</p> |
| 1139 |
<p> |
| 1140 |
The application that opens the first connection to the database uses the embedded mode, |
| 1141 |
which is faster than the server mode. Therefore the main application should open |
| 1142 |
the database first if possible. The first connection automatically starts a server on a random port. |
| 1143 |
This server allows remote connections, however only to this database (to ensure that, |
| 1144 |
the client reads <code>.lock.db</code> file and sends the random key that is stored there to the server). |
| 1145 |
When the first connection is closed, the server stops. If other (remote) connections are still |
| 1146 |
open, one of them will then start a server (auto-reconnect is enabled automatically). |
| 1147 |
</p> |
| 1148 |
<p> |
| 1149 |
All processes need to have access to the database files. |
| 1150 |
If the first connection is closed (the connection that started the server), open transactions of other connections will be rolled back |
| 1151 |
(this may not be a problem if you don't disable autocommit). |
| 1152 |
Explicit client/server connections (using <code>jdbc:h2:tcp://</code> or <code>ssl://</code>) are not supported. |
| 1153 |
This mode is not supported for in-memory databases. |
| 1154 |
</p> |
| 1155 |
<p> |
| 1156 |
Here is an example how to use this mode. Application 1 and 2 are not necessarily started |
| 1157 |
on the same computer, but they need to have access to the database files. Application 1 |
| 1158 |
and 2 are typically two different processes (however they could run within the same process). |
| 1159 |
</p> |
| 1160 |
<pre> |
| 1161 |
// Application 1: |
| 1162 |
DriverManager.getConnection("jdbc:h2:/data/test;AUTO_SERVER=TRUE"); |
| 1163 |
|
| 1164 |
// Application 2: |
| 1165 |
DriverManager.getConnection("jdbc:h2:/data/test;AUTO_SERVER=TRUE"); |
| 1166 |
</pre> |
| 1167 |
<p> |
| 1168 |
When using this feature, by default the server uses any free TCP port. |
| 1169 |
The port can be set manually using <code>AUTO_SERVER_PORT=9090</code>. |
| 1170 |
</p> |
| 1171 |
|
| 1172 |
<h2 id="page_size">Page Size</h2> |
| 1173 |
<p> |
| 1174 |
The page size for new databases is 4 KiB (4096 bytes), unless the page size is set |
| 1175 |
explicitly in the database URL using <code>PAGE_SIZE=</code> when |
| 1176 |
the database is created. The page size of existing databases can not be changed, |
| 1177 |
so this property needs to be set when the database is created. |
| 1178 |
The page size of encrypted databases must be a multiple of 4096 (4096, 8192, …). |
| 1179 |
</p> |
| 1180 |
|
| 1181 |
<h2 id="trace_options">Using the Trace Options</h2> |
| 1182 |
<p> |
| 1183 |
To find problems in an application, it is sometimes good to see what database operations |
| 1184 |
where executed. This database offers the following trace features: |
| 1185 |
</p> |
| 1186 |
<ul> |
| 1187 |
<li>Trace to <code>System.out</code> and/or to a file |
| 1188 |
</li><li>Support for trace levels <code>OFF, ERROR, INFO, DEBUG</code> |
| 1189 |
</li><li>The maximum size of the trace file can be set |
| 1190 |
</li><li>It is possible to generate Java source code from the trace file |
| 1191 |
</li><li>Trace can be enabled at runtime by manually creating a file |
| 1192 |
</li></ul> |
| 1193 |
|
| 1194 |
<h3>Trace Options</h3> |
| 1195 |
<p> |
| 1196 |
The simplest way to enable the trace option is setting it in the database URL. |
| 1197 |
There are two settings, one for <code>System.out</code> |
| 1198 |
(<code>TRACE_LEVEL_SYSTEM_OUT</code>) tracing, |
| 1199 |
and one for file tracing (<code>TRACE_LEVEL_FILE</code>). |
| 1200 |
The trace levels are |
| 1201 |
0 for <code>OFF</code>, |
| 1202 |
1 for <code>ERROR</code> (the default), |
| 1203 |
2 for <code>INFO</code>, and |
| 1204 |
3 for <code>DEBUG</code>. |
| 1205 |
A database URL with both levels set to <code>DEBUG</code> is: |
| 1206 |
</p> |
| 1207 |
<pre> |
| 1208 |
jdbc:h2:~/test;TRACE_LEVEL_FILE=3;TRACE_LEVEL_SYSTEM_OUT=3 |
| 1209 |
</pre> |
| 1210 |
<p> |
| 1211 |
The trace level can be changed at runtime by executing the SQL command |
| 1212 |
<code>SET TRACE_LEVEL_SYSTEM_OUT level</code> (for <code>System.out</code> tracing) |
| 1213 |
or <code>SET TRACE_LEVEL_FILE level</code> (for file tracing). |
| 1214 |
Example: |
| 1215 |
</p> |
| 1216 |
<pre> |
| 1217 |
SET TRACE_LEVEL_SYSTEM_OUT 3 |
| 1218 |
</pre> |
| 1219 |
|
| 1220 |
<h3>Setting the Maximum Size of the Trace File</h3> |
| 1221 |
<p> |
| 1222 |
When using a high trace level, the trace file can get very big quickly. |
| 1223 |
The default size limit is 16 MB, if the trace file exceeds this limit, it is renamed to |
| 1224 |
<code>.old</code> and a new file is created. |
| 1225 |
If another such file exists, it is deleted. |
| 1226 |
To limit the size to a certain number of megabytes, use |
| 1227 |
<code>SET TRACE_MAX_FILE_SIZE mb</code>. |
| 1228 |
Example: |
| 1229 |
</p> |
| 1230 |
<pre> |
| 1231 |
SET TRACE_MAX_FILE_SIZE 1 |
| 1232 |
</pre> |
| 1233 |
|
| 1234 |
<h3>Java Code Generation</h3> |
| 1235 |
<p> |
| 1236 |
When setting the trace level to <code>INFO</code> or <code>DEBUG</code>, |
| 1237 |
Java source code is generated as well. This simplifies reproducing problems. The trace file looks like this: |
| 1238 |
</p> |
| 1239 |
<pre> |
| 1240 |
... |
| 1241 |
12-20 20:58:09 jdbc[0]: |
| 1242 |
/**/dbMeta3.getURL(); |
| 1243 |
12-20 20:58:09 jdbc[0]: |
| 1244 |
/**/dbMeta3.getTables(null, "", null, new String[]{"BASE TABLE", "VIEW"}); |
| 1245 |
... |
| 1246 |
</pre> |
| 1247 |
<p> |
| 1248 |
To filter the Java source code, use the <code>ConvertTraceFile</code> tool as follows: |
| 1249 |
</p> |
| 1250 |
<pre> |
| 1251 |
java -cp h2*.jar org.h2.tools.ConvertTraceFile |
| 1252 |
-traceFile "~/test.trace.db" -javaClass "Test" |
| 1253 |
</pre> |
| 1254 |
<p> |
| 1255 |
The generated file <code>Test.java</code> will contain the Java source code. |
| 1256 |
The generated source code may be too large to compile (the size of a Java method is limited). |
| 1257 |
If this is the case, the source code needs to be split in multiple methods. |
| 1258 |
The password is not listed in the trace file and therefore not included in the source code. |
| 1259 |
</p> |
| 1260 |
|
| 1261 |
<h2 id="other_logging">Using Other Logging APIs</h2> |
| 1262 |
<p> |
| 1263 |
By default, this database uses its own native 'trace' facility. This facility is called 'trace' and not |
| 1264 |
'log' within this database to avoid confusion with the transaction log. Trace messages can be |
| 1265 |
written to both file and <code>System.out</code>. |
| 1266 |
In most cases, this is sufficient, however sometimes it is better to use the same |
| 1267 |
facility as the application, for example Log4j. To do that, this database support SLF4J. |
| 1268 |
</p> |
| 1269 |
<p> |
| 1270 |
<a href="https://www.slf4j.org/">SLF4J</a> is a simple facade for various logging APIs |
| 1271 |
and allows to plug in the desired implementation at deployment time. |
| 1272 |
SLF4J supports implementations such as Logback, Log4j, Jakarta Commons Logging (JCL), |
| 1273 |
Java logging, x4juli, and Simple Log. |
| 1274 |
</p> |
| 1275 |
<p> |
| 1276 |
To enable SLF4J, set the file trace level to 4 in the database URL: |
| 1277 |
</p> |
| 1278 |
<pre> |
| 1279 |
jdbc:h2:~/test;TRACE_LEVEL_FILE=4 |
| 1280 |
</pre> |
| 1281 |
<p> |
| 1282 |
Changing the log mechanism is not possible after the database is open, that means |
| 1283 |
executing the SQL statement <code>SET TRACE_LEVEL_FILE 4</code> |
| 1284 |
when the database is already open will not have the desired effect. |
| 1285 |
To use SLF4J, all required jar files need to be in the classpath. |
| 1286 |
The logger name is <code>h2database</code>. |
| 1287 |
If it does not work, check the file <code><database>.trace.db</code> for error messages. |
| 1288 |
</p> |
| 1289 |
|
| 1290 |
<h2 id="read_only">Read Only Databases</h2> |
| 1291 |
<p> |
| 1292 |
If the database files are read-only, then the database is read-only as well. |
| 1293 |
It is not possible to create new tables, add or modify data in this database. |
| 1294 |
Only <code>SELECT</code> and <code>CALL</code> statements are allowed. |
| 1295 |
To create a read-only database, close the database. |
| 1296 |
Then, make the database file read-only. |
| 1297 |
When you open the database now, it is read-only. |
| 1298 |
There are two ways an application can find out whether database is read-only: |
| 1299 |
by calling <code>Connection.isReadOnly()</code> |
| 1300 |
or by executing the SQL statement <code>CALL READONLY()</code>. |
| 1301 |
</p> |
| 1302 |
<p> |
| 1303 |
Using the <a href="#custom_access_mode">Custom Access Mode</a> <code>r</code> |
| 1304 |
the database can also be opened in read-only mode, even if the database file is not read only. |
| 1305 |
</p> |
| 1306 |
|
| 1307 |
<h2 id="database_in_zip">Read Only Databases in Zip or Jar File</h2> |
| 1308 |
<p> |
| 1309 |
To create a read-only database in a zip file, first create a regular persistent database, and then create a backup. |
| 1310 |
The database must not have pending changes, that means you need to close all connections to the database first. |
| 1311 |
To speed up opening the read-only database and running queries, the database should be closed using <code>SHUTDOWN DEFRAG</code>. |
| 1312 |
If you are using a database named <code>test</code>, an easy way to create a zip file is using the |
| 1313 |
<code>Backup</code> tool. You can start the tool from the command line, or from within the |
| 1314 |
H2 Console (Tools - Backup). Please note that the database must be closed when the backup |
| 1315 |
is created. Therefore, the SQL statement <code>BACKUP TO</code> can not be used. |
| 1316 |
</p> |
| 1317 |
<p> |
| 1318 |
When the zip file is created, you can open the database in the zip file using the following database URL: |
| 1319 |
</p> |
| 1320 |
<pre> |
| 1321 |
jdbc:h2:zip:~/data.zip!/test |
| 1322 |
</pre> |
| 1323 |
<p> |
| 1324 |
Databases in zip files are read-only. The performance for some queries will be slower than when using |
| 1325 |
a regular database, because random access in zip files is not supported (only streaming). How much this |
| 1326 |
affects the performance depends on the queries and the data. The database |
| 1327 |
is not read in memory; therefore large databases are supported as well. The same indexes are used as when using |
| 1328 |
a regular database. |
| 1329 |
</p> |
| 1330 |
<p> |
| 1331 |
If the database is larger than a few megabytes, performance is much better if the database file is split into multiple smaller files, |
| 1332 |
because random access in compressed files is not possible. |
| 1333 |
See also the sample application <a href="https://github.com/h2database/h2database/tree/master/h2/src/test/org/h2/samples/ReadOnlyDatabaseInZip.java">ReadOnlyDatabaseInZip</a>. |
| 1334 |
</p> |
| 1335 |
|
| 1336 |
<h3>Opening a Corrupted Database</h3> |
| 1337 |
<p> |
| 1338 |
If a database cannot be opened because the boot info (the SQL script that is run at startup) |
| 1339 |
is corrupted, then the database can be opened by specifying a database event listener. |
| 1340 |
The exceptions are logged, but opening the database will continue. |
| 1341 |
</p> |
| 1342 |
|
| 1343 |
<h2 id="generated_columns">Generated Columns (Computed Columns) / Function Based Index</h2> |
| 1344 |
<p> |
| 1345 |
Each column is either a base column or a generated column. |
| 1346 |
A generated column is a column whose value is calculated before storing and cannot be assigned directly. |
| 1347 |
The formula is evaluated when the row is inserted, and re-evaluated every time the row is updated. |
| 1348 |
One use case is to automatically update the last-modification time: |
| 1349 |
</p> |
| 1350 |
<pre> |
| 1351 |
CREATE TABLE TEST( |
| 1352 |
ID INT, |
| 1353 |
NAME VARCHAR, |
| 1354 |
LAST_MOD TIMESTAMP WITH TIME ZONE |
| 1355 |
GENERATED ALWAYS AS CURRENT_TIMESTAMP |
| 1356 |
); |
| 1357 |
</pre> |
| 1358 |
<p> |
| 1359 |
Function indexes are not directly supported by this database, but they can be emulated |
| 1360 |
by using generated columns. For example, if an index on the upper-case version of |
| 1361 |
a column is required, create a generated column with the upper-case version of the original column, |
| 1362 |
and create an index for this column: |
| 1363 |
</p> |
| 1364 |
<pre> |
| 1365 |
CREATE TABLE ADDRESS( |
| 1366 |
ID INT PRIMARY KEY, |
| 1367 |
NAME VARCHAR, |
| 1368 |
UPPER_NAME VARCHAR GENERATED ALWAYS AS UPPER(NAME) |
| 1369 |
); |
| 1370 |
CREATE INDEX IDX_U_NAME ON ADDRESS(UPPER_NAME); |
| 1371 |
</pre> |
| 1372 |
<p> |
| 1373 |
When inserting data, it is not required (and not allowed) to specify a value for the upper-case |
| 1374 |
version of the column, because the value is generated. But you can use the |
| 1375 |
column when querying the table: |
| 1376 |
</p> |
| 1377 |
<pre> |
| 1378 |
INSERT INTO ADDRESS(ID, NAME) VALUES(1, 'Miller'); |
| 1379 |
SELECT * FROM ADDRESS WHERE UPPER_NAME='MILLER'; |
| 1380 |
</pre> |
| 1381 |
|
| 1382 |
<h2 id="multi_dimensional">Multi-Dimensional Indexes</h2> |
| 1383 |
<p> |
| 1384 |
A tool is provided to execute efficient multi-dimension (spatial) range queries. |
| 1385 |
This database does not support a specialized spatial index (R-Tree or similar). |
| 1386 |
Instead, the B-Tree index is used. For each record, the multi-dimensional key |
| 1387 |
is converted (mapped) to a single dimensional (scalar) value. |
| 1388 |
This value specifies the location on a space-filling curve. |
| 1389 |
</p><p> |
| 1390 |
Currently, Z-order (also called N-order or Morton-order) is used; |
| 1391 |
Hilbert curve could also be used, but the implementation is more complex. |
| 1392 |
The algorithm to convert the multi-dimensional value is called bit-interleaving. |
| 1393 |
The scalar value is indexed using a B-Tree index (usually using a generated column). |
| 1394 |
</p><p> |
| 1395 |
The method can result in a drastic performance improvement |
| 1396 |
over just using an index on the first column. Depending on the |
| 1397 |
data and number of dimensions, the improvement is usually higher than factor 5. |
| 1398 |
The tool generates a SQL query from a specified multi-dimensional range. |
| 1399 |
The method used is not database dependent, and the tool can easily be ported to other databases. |
| 1400 |
For an example how to use the tool, please have a look at the sample code provided |
| 1401 |
in <code>TestMultiDimension.java</code>. |
| 1402 |
</p> |
| 1403 |
|
| 1404 |
<h2 id="user_defined_functions">User-Defined Functions and Stored Procedures</h2> |
| 1405 |
<p> |
| 1406 |
In addition to the built-in functions, this database supports user-defined Java functions. |
| 1407 |
In this database, Java functions can be used as stored procedures as well. |
| 1408 |
A function must be declared (registered) before it can be used. |
| 1409 |
A function can be defined using source code, or as a reference to |
| 1410 |
a compiled class that is available in the classpath. By default, the |
| 1411 |
function aliases are stored in the current schema. |
| 1412 |
</p> |
| 1413 |
|
| 1414 |
<h3>Referencing a Compiled Method</h3> |
| 1415 |
<p> |
| 1416 |
When referencing a method, the class must already be compiled and |
| 1417 |
included in the classpath where the database is running. |
| 1418 |
Only static Java methods are supported; both the class and the method must be public. |
| 1419 |
Example Java class: |
| 1420 |
</p> |
| 1421 |
<pre> |
| 1422 |
package acme; |
| 1423 |
import java.math.*; |
| 1424 |
public class Function { |
| 1425 |
public static boolean isPrime(int value) { |
| 1426 |
return new BigInteger(String.valueOf(value)).isProbablePrime(100); |
| 1427 |
} |
| 1428 |
} |
| 1429 |
</pre> |
| 1430 |
<p> |
| 1431 |
The Java function must be registered in the database by calling <code>CREATE ALIAS ... FOR</code>: |
| 1432 |
</p> |
| 1433 |
<pre> |
| 1434 |
CREATE ALIAS IS_PRIME FOR "acme.Function.isPrime"; |
| 1435 |
</pre> |
| 1436 |
<p> |
| 1437 |
For a complete sample application, see <code>src/test/org/h2/samples/Function.java</code>. |
| 1438 |
</p> |
| 1439 |
|
| 1440 |
<h3>Declaring Functions as Source Code</h3> |
| 1441 |
<p> |
| 1442 |
When defining a function alias with source code, the database tries to compile |
| 1443 |
the source code using the Java compiler (the class <code>javax.tool.ToolProvider.getSystemJavaCompiler()</code>) |
| 1444 |
if it is in the classpath. If not, <code>javac</code> is run as a separate process. |
| 1445 |
Only the source code is stored in the database; the class is compiled each time the database is re-opened. |
| 1446 |
Source code can be passed as dollar quoted text (<code>$$source code$$</code>) to avoid escaping problems. |
| 1447 |
If you use some third-party script processing tool, use standard single quotes instead and don't forget to repeat |
| 1448 |
each single quotation mark twice within the source code. |
| 1449 |
Example: |
| 1450 |
</p> |
| 1451 |
<pre> |
| 1452 |
CREATE ALIAS NEXT_PRIME AS ' |
| 1453 |
String nextPrime(String value) { |
| 1454 |
return new BigInteger(value).nextProbablePrime().toString(); |
| 1455 |
} |
| 1456 |
'; |
| 1457 |
</pre> |
| 1458 |
<p> |
| 1459 |
By default, the three packages <code>java.util, java.math, java.sql</code> are imported. |
| 1460 |
The method name (<code>nextPrime</code> in the example above) is ignored. |
| 1461 |
Method overloading is not supported when declaring functions as source code, that means only one method may be declared for an alias. |
| 1462 |
If different import statements are required, they must be declared at the beginning |
| 1463 |
and separated with the tag <code>@CODE</code>: |
| 1464 |
</p> |
| 1465 |
<pre> |
| 1466 |
CREATE ALIAS IP_ADDRESS AS ' |
| 1467 |
import java.net.*; |
| 1468 |
@CODE |
| 1469 |
String ipAddress(String host) throws Exception { |
| 1470 |
return InetAddress.getByName(host).getHostAddress(); |
| 1471 |
} |
| 1472 |
'; |
| 1473 |
</pre> |
| 1474 |
<p> |
| 1475 |
The following template is used to create a complete Java class: |
| 1476 |
</p> |
| 1477 |
<pre> |
| 1478 |
package org.h2.dynamic; |
| 1479 |
< import statements before the tag @CODE; if not set: |
| 1480 |
import java.util.*; |
| 1481 |
import java.math.*; |
| 1482 |
import java.sql.*; |
| 1483 |
> |
| 1484 |
public class <aliasName> { |
| 1485 |
public static <sourceCode> |
| 1486 |
} |
| 1487 |
</pre> |
| 1488 |
|
| 1489 |
<h3>Method Overloading</h3> |
| 1490 |
<p> |
| 1491 |
Multiple methods may be bound to a SQL function if the class is already compiled and included in the classpath. |
| 1492 |
Each Java method must have a different number of arguments. |
| 1493 |
Method overloading is not supported when declaring functions as source code. |
| 1494 |
</p> |
| 1495 |
|
| 1496 |
<h3>Function Data Type Mapping</h3> |
| 1497 |
<p> |
| 1498 |
Functions that accept non-nullable parameters such as <code>int</code> |
| 1499 |
will not be called if one of those parameters is <code>NULL</code>. |
| 1500 |
Instead, the result of the function is <code>NULL</code>. |
| 1501 |
If the function should be called if a parameter is <code>NULL</code>, you need |
| 1502 |
to use <code>java.lang.Integer</code> instead. |
| 1503 |
</p> |
| 1504 |
<p> |
| 1505 |
SQL types are mapped to Java classes and vice-versa as in the JDBC API. For details, see <a href="datatypes.html">Data Types</a>. |
| 1506 |
There are a few special cases: <code>java.lang.Object</code> is mapped to |
| 1507 |
<code>OTHER</code> (a serialized object). Therefore, |
| 1508 |
<code>java.lang.Object</code> can not be used |
| 1509 |
to match all SQL types (matching all SQL types is not supported). The second special case is <code>Object[]</code>: |
| 1510 |
arrays of any class are mapped to <code>ARRAY</code>. |
| 1511 |
Objects of type <code>org.h2.value.Value</code> (the internal value class) are passed through without conversion. |
| 1512 |
</p> |
| 1513 |
|
| 1514 |
<h3>Functions That Require a Connection</h3> |
| 1515 |
<p> |
| 1516 |
If the first parameter of a Java function is a <code>java.sql.Connection</code>, then the connection |
| 1517 |
to database is provided. This connection does not need to be closed before returning. |
| 1518 |
When calling the method from within the SQL statement, this connection parameter |
| 1519 |
does not need to be (can not be) specified. |
| 1520 |
</p> |
| 1521 |
|
| 1522 |
<h3>Functions Throwing an Exception</h3> |
| 1523 |
<p> |
| 1524 |
If a function throws an exception, then the current statement is rolled back |
| 1525 |
and the exception is thrown to the application. |
| 1526 |
SQLException are directly re-thrown to the calling application; |
| 1527 |
all other exceptions are first converted to a SQLException. |
| 1528 |
</p> |
| 1529 |
|
| 1530 |
<h3>Functions Returning a Result Set</h3> |
| 1531 |
<p> |
| 1532 |
Functions may returns a result set. Such a function can be called with the <code>CALL</code> statement: |
| 1533 |
</p> |
| 1534 |
<pre> |
| 1535 |
public static ResultSet query(Connection conn, String sql) throws SQLException { |
| 1536 |
return conn.createStatement().executeQuery(sql); |
| 1537 |
} |
| 1538 |
|
| 1539 |
CREATE ALIAS QUERY FOR "org.h2.samples.Function.query"; |
| 1540 |
CALL QUERY('SELECT * FROM TEST'); |
| 1541 |
</pre> |
| 1542 |
|
| 1543 |
<h3>Using SimpleResultSet</h3> |
| 1544 |
<p> |
| 1545 |
A function can create a result set using the <code>SimpleResultSet</code> tool: |
| 1546 |
</p> |
| 1547 |
<pre> |
| 1548 |
import org.h2.tools.SimpleResultSet; |
| 1549 |
... |
| 1550 |
public static ResultSet simpleResultSet() throws SQLException { |
| 1551 |
SimpleResultSet rs = new SimpleResultSet(); |
| 1552 |
rs.addColumn("ID", Types.INTEGER, 10, 0); |
| 1553 |
rs.addColumn("NAME", Types.VARCHAR, 255, 0); |
| 1554 |
rs.addRow(0, "Hello"); |
| 1555 |
rs.addRow(1, "World"); |
| 1556 |
return rs; |
| 1557 |
} |
| 1558 |
|
| 1559 |
CREATE ALIAS SIMPLE FOR "org.h2.samples.Function.simpleResultSet"; |
| 1560 |
CALL SIMPLE(); |
| 1561 |
</pre> |
| 1562 |
|
| 1563 |
<h3>Using a Function as a Table</h3> |
| 1564 |
<p> |
| 1565 |
A function that returns a result set can be used like a table. |
| 1566 |
However, in this case the function is called at least twice: |
| 1567 |
first while parsing the statement to collect the column names |
| 1568 |
(with parameters set to <code>null</code> where not known at compile time). |
| 1569 |
And then, while executing the statement to get the data (maybe multiple times if this is a join). |
| 1570 |
If the function is called just to get the column list, the URL of the connection passed to the function is |
| 1571 |
<code>jdbc:columnlist:connection</code>. Otherwise, the URL of the connection is |
| 1572 |
<code>jdbc:default:connection</code>. |
| 1573 |
</p> |
| 1574 |
<pre> |
| 1575 |
public static ResultSet getMatrix(Connection conn, Integer size) |
| 1576 |
throws SQLException { |
| 1577 |
SimpleResultSet rs = new SimpleResultSet(); |
| 1578 |
rs.addColumn("X", Types.INTEGER, 10, 0); |
| 1579 |
rs.addColumn("Y", Types.INTEGER, 10, 0); |
| 1580 |
String url = conn.getMetaData().getURL(); |
| 1581 |
if (url.equals("jdbc:columnlist:connection")) { |
| 1582 |
return rs; |
| 1583 |
} |
| 1584 |
for (int s = size.intValue(), x = 0; x < s; x++) { |
| 1585 |
for (int y = 0; y < s; y++) { |
| 1586 |
rs.addRow(x, y); |
| 1587 |
} |
| 1588 |
} |
| 1589 |
return rs; |
| 1590 |
} |
| 1591 |
|
| 1592 |
CREATE ALIAS MATRIX FOR "org.h2.samples.Function.getMatrix"; |
| 1593 |
SELECT * FROM MATRIX(4) ORDER BY X, Y; |
| 1594 |
</pre> |
| 1595 |
|
| 1596 |
|
| 1597 |
<h2 id="pluggable_tables">Pluggable or User-Defined Tables</h2> |
| 1598 |
<p> |
| 1599 |
For situations where you need to expose other data-sources to the SQL engine as a table, |
| 1600 |
there are "pluggable tables". |
| 1601 |
For some examples, have a look at the code in <code>org.h2.test.db.TestTableEngines</code>. |
| 1602 |
</p> |
| 1603 |
<p> |
| 1604 |
In order to create your own TableEngine, you need to implement the <code>org.h2.api.TableEngine</code> interface e.g. |
| 1605 |
something like this: |
| 1606 |
</p> |
| 1607 |
<pre> |
| 1608 |
package acme; |
| 1609 |
public static class MyTableEngine implements org.h2.api.TableEngine { |
| 1610 |
|
| 1611 |
private static class MyTable extends org.h2.table.TableBase { |
| 1612 |
.. rather a lot of code here... |
| 1613 |
} |
| 1614 |
|
| 1615 |
public EndlessTable createTable(CreateTableData data) { |
| 1616 |
return new EndlessTable(data); |
| 1617 |
} |
| 1618 |
} |
| 1619 |
</pre> |
| 1620 |
<p> |
| 1621 |
and then create the table from SQL like this: |
| 1622 |
</p> |
| 1623 |
<pre> |
| 1624 |
CREATE TABLE TEST(ID INT, NAME VARCHAR) |
| 1625 |
ENGINE "acme.MyTableEngine"; |
| 1626 |
</pre> |
| 1627 |
<p> |
| 1628 |
It is also possible to pass in parameters to the table engine, like so: |
| 1629 |
</p> |
| 1630 |
<pre> |
| 1631 |
CREATE TABLE TEST(ID INT, NAME VARCHAR) ENGINE "acme.MyTableEngine" WITH "param1", "param2"; |
| 1632 |
</pre> |
| 1633 |
<p> |
| 1634 |
In which case the parameters are passed down in the tableEngineParams field of the CreateTableData object. |
| 1635 |
</p> |
| 1636 |
<p> |
| 1637 |
It is also possible to specify default table engine params on schema creation: |
| 1638 |
</p> |
| 1639 |
<pre> |
| 1640 |
CREATE SCHEMA TEST_SCHEMA WITH "param1", "param2"; |
| 1641 |
</pre> |
| 1642 |
<p> |
| 1643 |
Params from the schema are used when CREATE TABLE issued on this schema does not have its own engine params specified. |
| 1644 |
</p> |
| 1645 |
|
| 1646 |
<h2 id="triggers">Triggers</h2> |
| 1647 |
<p> |
| 1648 |
This database supports Java triggers that are called before or after a row is updated, inserted or deleted. |
| 1649 |
Triggers can be used for complex consistency checks, or to update related data in the database. |
| 1650 |
It is also possible to use triggers to simulate materialized views. |
| 1651 |
For a complete sample application, see <code>src/test/org/h2/samples/TriggerSample.java</code>. |
| 1652 |
A Java trigger must implement the interface <code>org.h2.api.Trigger</code>. The trigger class must be available |
| 1653 |
in the classpath of the database engine (when using the server mode, it must be in the classpath |
| 1654 |
of the server). |
| 1655 |
</p> |
| 1656 |
<pre> |
| 1657 |
import org.h2.api.Trigger; |
| 1658 |
... |
| 1659 |
public class TriggerSample implements Trigger { |
| 1660 |
|
| 1661 |
public void init(Connection conn, String schemaName, String triggerName, |
| 1662 |
String tableName, boolean before, int type) { |
| 1663 |
// initialize the trigger object is necessary |
| 1664 |
} |
| 1665 |
|
| 1666 |
public void fire(Connection conn, |
| 1667 |
Object[] oldRow, Object[] newRow) |
| 1668 |
throws SQLException { |
| 1669 |
// the trigger is fired |
| 1670 |
} |
| 1671 |
|
| 1672 |
public void close() { |
| 1673 |
// the database is closed |
| 1674 |
} |
| 1675 |
|
| 1676 |
public void remove() { |
| 1677 |
// the trigger was dropped |
| 1678 |
} |
| 1679 |
|
| 1680 |
} |
| 1681 |
</pre> |
| 1682 |
<p> |
| 1683 |
The connection can be used to query or update data in other tables. |
| 1684 |
The trigger then needs to be defined in the database: |
| 1685 |
</p> |
| 1686 |
<pre> |
| 1687 |
CREATE TRIGGER INV_INS AFTER INSERT ON INVOICE |
| 1688 |
FOR EACH ROW CALL "org.h2.samples.TriggerSample" |
| 1689 |
</pre> |
| 1690 |
<p> |
| 1691 |
The trigger can be used to veto a change by throwing a <code>SQLException</code>. |
| 1692 |
</p> |
| 1693 |
<p> |
| 1694 |
As an alternative to implementing the <code>Trigger</code> interface, |
| 1695 |
an application can extend the abstract class <code>org.h2.tools.TriggerAdapter</code>. |
| 1696 |
This will allows to use the <code>ResultSet</code> interface within trigger implementations. |
| 1697 |
In this case, only the <code>fire</code> method needs to be implemented: |
| 1698 |
</p> |
| 1699 |
<pre> |
| 1700 |
import org.h2.tools.TriggerAdapter; |
| 1701 |
... |
| 1702 |
public class TriggerSample extends TriggerAdapter { |
| 1703 |
|
| 1704 |
public void fire(Connection conn, ResultSet oldRow, ResultSet newRow) |
| 1705 |
throws SQLException { |
| 1706 |
// the trigger is fired |
| 1707 |
} |
| 1708 |
|
| 1709 |
} |
| 1710 |
</pre> |
| 1711 |
|
| 1712 |
<h2 id="compacting">Compacting a Database</h2> |
| 1713 |
<p> |
| 1714 |
Empty space in the database file re-used automatically. When closing the database, |
| 1715 |
the database is automatically compacted for up to 200 milliseconds by default. To compact more, |
| 1716 |
use the SQL statement SHUTDOWN COMPACT. However re-creating the database may further |
| 1717 |
reduce the database size because this will re-build the indexes. |
| 1718 |
Here is a sample function to do this: |
| 1719 |
</p> |
| 1720 |
<pre> |
| 1721 |
public static void compact(String dir, String dbName, |
| 1722 |
String user, String password) throws Exception { |
| 1723 |
String url = "jdbc:h2:" + dir + "/" + dbName; |
| 1724 |
String file = "data/test.sql"; |
| 1725 |
Script.execute(url, user, password, file); |
| 1726 |
DeleteDbFiles.execute(dir, dbName, true); |
| 1727 |
RunScript.execute(url, user, password, file, null, false); |
| 1728 |
} |
| 1729 |
</pre> |
| 1730 |
<p> |
| 1731 |
See also the sample application <code>org.h2.samples.Compact</code>. |
| 1732 |
The commands <code>SCRIPT / RUNSCRIPT</code> can be used as well to create a backup |
| 1733 |
of a database and re-build the database from the script. |
| 1734 |
</p> |
| 1735 |
|
| 1736 |
<h2 id="cache_settings">Cache Settings</h2> |
| 1737 |
<p> |
| 1738 |
The database keeps most frequently used data in the main memory. |
| 1739 |
The amount of memory used for caching can be changed using the setting |
| 1740 |
<code>CACHE_SIZE</code>. This setting can be set in the database connection URL |
| 1741 |
(<code>jdbc:h2:~/test;CACHE_SIZE=131072</code>), or it can be changed at runtime using |
| 1742 |
<code>SET CACHE_SIZE size</code>. |
| 1743 |
The size of the cache, as represented by <code>CACHE_SIZE</code> is measured in KB, with each KB being 1024 bytes. |
| 1744 |
This setting has no effect for in-memory databases. |
| 1745 |
For persistent databases, the setting is stored in the database and re-used when the database is opened |
| 1746 |
the next time. However, when opening an existing database, the cache size is set to at most |
| 1747 |
half the amount of memory available for the virtual machine (Runtime.getRuntime().maxMemory()), |
| 1748 |
even if the cache size setting stored in the database is larger; however the setting stored in the database |
| 1749 |
is kept. Setting the cache size in the database URL or explicitly using <code>SET CACHE_SIZE</code> |
| 1750 |
overrides this value (even if larger than the physical memory). |
| 1751 |
To get the current used maximum cache size, use the query |
| 1752 |
<code>SELECT * FROM INFORMATION_SCHEMA.SETTINGS WHERE SETTING_NAME = 'info.CACHE_MAX_SIZE'</code> |
| 1753 |
</p><p> |
| 1754 |
An experimental scan-resistant cache algorithm "Two Queue" (2Q) is available. |
| 1755 |
To enable it, append <code>;CACHE_TYPE=TQ</code> to the database URL. |
| 1756 |
The cache might not actually improve performance. |
| 1757 |
If you plan to use it, please run your own test cases first. |
| 1758 |
</p><p> |
| 1759 |
Also included is an experimental second level soft reference cache. |
| 1760 |
Rows in this cache are only garbage collected on low memory. |
| 1761 |
By default the second level cache is disabled. |
| 1762 |
To enable it, use the prefix <code>SOFT_</code>. |
| 1763 |
Example: <code>jdbc:h2:~/test;CACHE_TYPE=SOFT_LRU</code>. |
| 1764 |
The cache might not actually improve performance. |
| 1765 |
If you plan to use it, please run your own test cases first. |
| 1766 |
</p><p> |
| 1767 |
To get information about page reads and writes, and the current caching algorithm in use, |
| 1768 |
call <code>SELECT * FROM INFORMATION_SCHEMA.SETTINGS</code>. The number of pages read / written |
| 1769 |
is listed. |
| 1770 |
</p> |
| 1771 |
<h2 id="external_authentication">External authentication (Experimental)</h2> |
| 1772 |
<p> |
| 1773 |
External authentication allows to optionally validate user credentials externally (JAAS,LDAP,custom classes). |
| 1774 |
Is also possible to temporary assign roles to externally authenticated users. <b>This feature is experimental and subject to change</b> |
| 1775 |
</p> |
| 1776 |
<p>Master user cannot be externally authenticated</p> |
| 1777 |
<p> |
| 1778 |
To enable external authentication on a database execute statement <code>SET AUTHENTICATOR TRUE</code>. This setting in persisted on the database. |
| 1779 |
</p> |
| 1780 |
<p> |
| 1781 |
To connect on a database by using external credentials client must append <code>AUTHREALM=H2</code> to the database URL. <code>H2</code> |
| 1782 |
is the identifier of the authentication realm (see later). |
| 1783 |
</p> |
| 1784 |
<p>External authentication requires to send password to the server. For this reason is works only on local connection or remote over ssl</p> |
| 1785 |
<p> |
| 1786 |
By default external authentication is performed through JAAS login interface (configuration name is <code>h2</code>). |
| 1787 |
To configure JAAS add argument <code>-Djava.security.auth.login.config=jaas.conf</code> |
| 1788 |
Here an example of <a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/jgss/tutorials/LoginConfigFile.html"> |
| 1789 |
JAAS login configuration file</a> content: |
| 1790 |
</p> |
| 1791 |
<pre> |
| 1792 |
h2 { |
| 1793 |
com.sun.security.auth.module.LdapLoginModule REQUIRED \ |
| 1794 |
userProvider="ldap://127.0.0.1:10389" authIdentity="uid={USERNAME},ou=people,dc=example,dc=com" \ |
| 1795 |
debug=true useSSL=false ; |
| 1796 |
}; |
| 1797 |
</pre> |
| 1798 |
<p> |
| 1799 |
Is it possible to specify custom authentication settings by using |
| 1800 |
JVM argument <code>-Dh2auth.configurationFile={urlOfH2Auth.xml}</code>. Here an example of h2auth.xml file content: |
| 1801 |
</p> |
| 1802 |
<pre> |
| 1803 |
<h2Auth allowUserRegistration="false" createMissingRoles="true"> |
| 1804 |
|
| 1805 |
<!-- realm: DUMMY authenticate users named DUMMY[0-9] with a static password --> |
| 1806 |
<realm name="DUMMY" |
| 1807 |
validatorClass="org.h2.security.auth.impl.FixedPasswordCredentialsValidator"> |
| 1808 |
<property name="userNamePattern" value="DUMMY[0-9]" /> |
| 1809 |
<property name="password" value="mock" /> |
| 1810 |
</realm> |
| 1811 |
|
| 1812 |
<!-- realm LDAPEXAMPLE:perform credentials validation on LDAP --> |
| 1813 |
<realm name="LDAPEXAMPLE" |
| 1814 |
validatorClass="org.h2.security.auth.impl.LdapCredentialsValidator"> |
| 1815 |
<property name="bindDnPattern" value="uid=%u,ou=people,dc=example,dc=com" /> |
| 1816 |
<property name="host" value="127.0.0.1" /> |
| 1817 |
<property name="port" value="10389" /> |
| 1818 |
<property name="secure" value="false" /> |
| 1819 |
</realm> |
| 1820 |
|
| 1821 |
<!-- realm JAAS: perform credentials validation by using JAAS api --> |
| 1822 |
<realm name="JAAS" |
| 1823 |
validatorClass="org.h2.security.auth.impl.JaasCredentialsValidator"> |
| 1824 |
<property name="appName" value="H2" /> |
| 1825 |
</realm> |
| 1826 |
|
| 1827 |
<!--Assign to each user role @{REALM} --> |
| 1828 |
<userToRolesMapper class="org.h2.security.auth.impl.AssignRealmNameRole"/> |
| 1829 |
|
| 1830 |
<!--Assign to each user role REMOTEUSER --> |
| 1831 |
<userToRolesMapper class="org.h2.security.auth.impl.StaticRolesMapper"> |
| 1832 |
<property name="roles" value="REMOTEUSER"/> |
| 1833 |
</userToRolesMapper> |
| 1834 |
</h2Auth> |
| 1835 |
</pre> |
| 1836 |
<p> |
| 1837 |
Custom credentials validators must implement the interface |
| 1838 |
<code>org.h2.api.CredentialsValidator</code> |
| 1839 |
</p> |
| 1840 |
<p> |
| 1841 |
Custom criteria for role assignments must implement the interface |
| 1842 |
<code>org.h2.api.UserToRoleMapper</code> |
| 1843 |
</p> |
| 1844 |
|
| 1845 |
<!-- [close] { --></div></td></tr></table><!-- } --><!-- analytics --></body></html> |
| 1846 |
|