| 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> |
| 9 |
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> |
| 10 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 11 |
<title> |
| 12 |
Advanced |
| 13 |
</title> |
| 14 |
<link rel="stylesheet" type="text/css" href="stylesheet.css" /> |
| 15 |
<!-- [search] { --> |
| 16 |
<script type="text/javascript" src="navigation.js"></script> |
| 17 |
</head><body onload="frameMe();"> |
| 18 |
<table class="content"><tr class="content"><td class="content"><div class="contentDiv"> |
| 19 |
<!-- } --> |
| 20 |
|
| 21 |
<h1>Advanced</h1> |
| 22 |
<a href="#result_sets"> |
| 23 |
Result Sets</a><br /> |
| 24 |
<a href="#large_objects"> |
| 25 |
Large Objects</a><br /> |
| 26 |
<a href="#linked_tables"> |
| 27 |
Linked Tables</a><br /> |
| 28 |
<a href="#spatial_features"> |
| 29 |
Spatial Features</a><br /> |
| 30 |
<a href="#recursive_queries"> |
| 31 |
Recursive Queries</a><br /> |
| 32 |
<a href="#updatable_views"> |
| 33 |
Updatable Views</a><br /> |
| 34 |
<a href="#transaction_isolation"> |
| 35 |
Transaction Isolation</a><br /> |
| 36 |
<a href="#mvcc"> |
| 37 |
Multi-Version Concurrency Control (MVCC)</a><br /> |
| 38 |
<a href="#clustering"> |
| 39 |
Clustering / High Availability</a><br /> |
| 40 |
<a href="#two_phase_commit"> |
| 41 |
Two Phase Commit</a><br /> |
| 42 |
<a href="#compatibility"> |
| 43 |
Compatibility</a><br /> |
| 44 |
<a href="#keywords"> |
| 45 |
Keywords / Reserved Words</a><br /> |
| 46 |
<a href="#standards_compliance"> |
| 47 |
Standards Compliance</a><br /> |
| 48 |
<a href="#windows_service"> |
| 49 |
Run as Windows Service</a><br /> |
| 50 |
<a href="#odbc_driver"> |
| 51 |
ODBC Driver</a><br /> |
| 52 |
<a href="#acid"> |
| 53 |
ACID</a><br /> |
| 54 |
<a href="#durability_problems"> |
| 55 |
Durability Problems</a><br /> |
| 56 |
<a href="#using_recover_tool"> |
| 57 |
Using the Recover Tool</a><br /> |
| 58 |
<a href="#file_locking_protocols"> |
| 59 |
File Locking Protocols</a><br /> |
| 60 |
<a href="#passwords"> |
| 61 |
Using Passwords</a><br /> |
| 62 |
<a href="#password_hash"> |
| 63 |
Password Hash</a><br /> |
| 64 |
<a href="#sql_injection"> |
| 65 |
Protection against SQL Injection</a><br /> |
| 66 |
<a href="#remote_access"> |
| 67 |
Protection against Remote Access</a><br /> |
| 68 |
<a href="#restricting_classes"> |
| 69 |
Restricting Class Loading and Usage</a><br /> |
| 70 |
<a href="#security_protocols"> |
| 71 |
Security Protocols</a><br /> |
| 72 |
<a href="#tls_connections"> |
| 73 |
TLS Connections</a><br /> |
| 74 |
<a href="#uuid"> |
| 75 |
Universally Unique Identifiers (UUID)</a><br /> |
| 76 |
<a href="#system_properties"> |
| 77 |
Settings Read from System Properties</a><br /> |
| 78 |
<a href="#server_bind_address"> |
| 79 |
Setting the Server Bind Address</a><br /> |
| 80 |
<a href="#file_system"> |
| 81 |
Pluggable File System</a><br /> |
| 82 |
<a href="#file_system_split"> |
| 83 |
Split File System</a><br /> |
| 84 |
<a href="#java_objects_serialization"> |
| 85 |
Java Objects Serialization</a><br /> |
| 86 |
<a href="#limits_limitations"> |
| 87 |
Limits and Limitations</a><br /> |
| 88 |
<a href="#glossary_links"> |
| 89 |
Glossary and Links</a><br /> |
| 90 |
|
| 91 |
<h2 id="result_sets">Result Sets</h2> |
| 92 |
|
| 93 |
<h3>Statements that Return a Result Set</h3> |
| 94 |
<p> |
| 95 |
The following statements return a result set: <code>SELECT</code>, <code>TABLE</code>, <code>VALUES</code>, |
| 96 |
<code>EXPLAIN</code>, <code>CALL</code>, <code>SCRIPT</code>, <code>SHOW</code>, <code>HELP</code>. |
| 97 |
<code>EXECUTE</code> may return either a result set or an update count. |
| 98 |
Result of a <code>WITH</code> statement depends on inner command. |
| 99 |
All other statements return an update count. |
| 100 |
</p> |
| 101 |
|
| 102 |
<h3>Limiting the Number of Rows</h3> |
| 103 |
<p> |
| 104 |
Before the result is returned to the application, all rows are read by the database. |
| 105 |
Server side cursors are not supported currently. |
| 106 |
If only the first few rows are interesting for the application, then the |
| 107 |
result set size should be limited to improve the performance. |
| 108 |
This can be done using <code>FETCH</code> in a query |
| 109 |
(example: <code>SELECT * FROM TEST FETCH FIRST 100 ROWS ONLY</code>), |
| 110 |
or by using <code>Statement.setMaxRows(max)</code>. |
| 111 |
</p> |
| 112 |
|
| 113 |
<h3>Large Result Sets and External Sorting</h3> |
| 114 |
<p> |
| 115 |
For large result set, the result is buffered to disk. The threshold can be defined using the statement |
| 116 |
<code>SET MAX_MEMORY_ROWS</code>. |
| 117 |
If <code>ORDER BY</code> is used, the sorting is done using an |
| 118 |
external sort algorithm. |
| 119 |
In this case, each block of rows is sorted using quick sort, then written to disk; |
| 120 |
when reading the data, the blocks are merged together. |
| 121 |
</p> |
| 122 |
|
| 123 |
<h2 id="large_objects">Large Objects</h2> |
| 124 |
|
| 125 |
<h3>Storing and Reading Large Objects</h3> |
| 126 |
<p> |
| 127 |
If it is possible that the objects don't fit into memory, then the data type |
| 128 |
CLOB (for textual data) or BLOB (for binary data) should be used. |
| 129 |
For these data types, the objects are not fully read into memory, by using streams. |
| 130 |
To store a BLOB, use <code>PreparedStatement.setBinaryStream</code>. To store a CLOB, use |
| 131 |
<code>PreparedStatement.setCharacterStream</code>. To read a BLOB, use <code>ResultSet.getBinaryStream</code>, |
| 132 |
and to read a CLOB, use <code>ResultSet.getCharacterStream</code>. |
| 133 |
When using the client/server mode, large BLOB and CLOB data is stored in a temporary file |
| 134 |
on the client side. |
| 135 |
</p> |
| 136 |
|
| 137 |
<h3>When to use CLOB/BLOB</h3> |
| 138 |
<p> |
| 139 |
By default, this database stores large LOB (CLOB and BLOB) objects separate from the main table data. |
| 140 |
Small LOB objects are stored in-place, the threshold can be set using |
| 141 |
<a href="commands.html#set_max_length_inplace_lob" class="notranslate" >MAX_LENGTH_INPLACE_LOB</a>, |
| 142 |
but there is still an overhead to use CLOB/BLOB. Because of this, BLOB and CLOB |
| 143 |
should never be used for columns with a maximum size below about 200 bytes. |
| 144 |
The best threshold depends on the use case; reading in-place objects is faster |
| 145 |
than reading from separate files, but slows down the performance of operations |
| 146 |
that don't involve this column. |
| 147 |
</p> |
| 148 |
|
| 149 |
<h2 id="linked_tables">Linked Tables</h2> |
| 150 |
<p> |
| 151 |
This database supports linked tables, which means tables that don't exist in the current database but |
| 152 |
are just links to another database. To create such a link, use the |
| 153 |
<code>CREATE LINKED TABLE</code> statement: |
| 154 |
</p> |
| 155 |
<pre> |
| 156 |
CREATE LINKED TABLE LINK('org.postgresql.Driver', 'jdbc:postgresql:test', 'sa', 'sa', 'TEST'); |
| 157 |
</pre> |
| 158 |
<p> |
| 159 |
You can then access the table in the usual way. |
| 160 |
Whenever the linked table is accessed, the database issues specific queries over JDBC. |
| 161 |
Using the example above, if you issue the query <code>SELECT * FROM LINK WHERE ID=1</code>, |
| 162 |
then the following query is run against the PostgreSQL database: <code>SELECT * FROM TEST WHERE ID=?</code>. |
| 163 |
The same happens for insert and update statements. |
| 164 |
Only simple statements are executed against the target database, that means no joins |
| 165 |
(queries that contain joins are converted to simple queries). |
| 166 |
Prepared statements are used where possible. |
| 167 |
</p> |
| 168 |
<p> |
| 169 |
To view the statements that are executed against the target table, set the trace level to 3. |
| 170 |
</p> |
| 171 |
<p> |
| 172 |
If multiple linked tables point to the same database (using the same database URL), the connection |
| 173 |
is shared. To disable this, set the system property <code>h2.shareLinkedConnections=false</code>. |
| 174 |
</p> |
| 175 |
<p> |
| 176 |
The statement <a href="commands.html#create_linked_table" class="notranslate" >CREATE LINKED TABLE</a> |
| 177 |
supports an optional schema name parameter. |
| 178 |
</p> |
| 179 |
<p> |
| 180 |
The following are not supported because they may result in a deadlock: |
| 181 |
creating a linked table to the same database, |
| 182 |
and creating a linked table to another database using the server mode if the other database is open in the same server |
| 183 |
(use the embedded mode instead). |
| 184 |
</p> |
| 185 |
<p> |
| 186 |
Data types that are not supported in H2 are also not supported for linked tables, |
| 187 |
for example unsigned data types if the value is outside the range of the signed type. |
| 188 |
In such cases, the columns needs to be cast to a supported type. |
| 189 |
</p> |
| 190 |
|
| 191 |
<h2 id="updatable_views">Updatable Views</h2> |
| 192 |
<p> |
| 193 |
By default, views are not updatable. |
| 194 |
To make a view updatable, use an "instead of" trigger as follows: |
| 195 |
</p> |
| 196 |
<pre> |
| 197 |
CREATE TRIGGER TRIGGER_NAME |
| 198 |
INSTEAD OF INSERT, UPDATE, DELETE |
| 199 |
ON VIEW_NAME |
| 200 |
FOR EACH ROW CALL "com.acme.TriggerClassName"; |
| 201 |
</pre> |
| 202 |
<p> |
| 203 |
Update the base table(s) within the trigger as required. |
| 204 |
For details, see the sample application <code>org.h2.samples.UpdatableView</code>. |
| 205 |
</p> |
| 206 |
|
| 207 |
<h2 id="transaction_isolation">Transaction Isolation</h2> |
| 208 |
<p> |
| 209 |
Please note that most data definition language (DDL) statements, |
| 210 |
such as "create table", commit the current transaction. |
| 211 |
See the <a href="commands.html">Commands</a> for details. |
| 212 |
</p> |
| 213 |
<p> |
| 214 |
Transaction isolation is provided for all data manipulation language (DML) statements. |
| 215 |
</p> |
| 216 |
<p> |
| 217 |
H2 supports read uncommitted, read committed, repeatable read, snapshot, |
| 218 |
and serializable (partially, see below) isolation levels: |
| 219 |
</p> |
| 220 |
<ul> |
| 221 |
<li><b>Read uncommitted</b><br /> |
| 222 |
Dirty reads, non-repeatable reads, and phantom reads are possible. |
| 223 |
To enable, execute the SQL statement |
| 224 |
<code>SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED</code> |
| 225 |
</li> |
| 226 |
<li><b>Read committed</b><br /> |
| 227 |
This is the default level. |
| 228 |
Dirty reads aren't possible; non-repeatable reads and phantom reads are possible. |
| 229 |
To enable, execute the SQL statement |
| 230 |
<code>SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED</code> |
| 231 |
</li> |
| 232 |
<li><b>Repeatable read</b><br /> |
| 233 |
Dirty reads and non-repeatable reads aren't possible, phantom reads are possible. |
| 234 |
To enable, execute the SQL statement |
| 235 |
<code>SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ</code> |
| 236 |
</li> |
| 237 |
<li><b>Snapshot</b><br /> |
| 238 |
Dirty reads, non-repeatable reads, and phantom reads aren't possible. |
| 239 |
This isolation level is very expensive in databases with many tables. |
| 240 |
To enable, execute the SQL statement |
| 241 |
<code>SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SNAPSHOT</code> |
| 242 |
</li> |
| 243 |
<li><b>Serializable</b><br /> |
| 244 |
Dirty reads, non-repeatable reads, and phantom reads aren't possible. |
| 245 |
Note that this isolation level in H2 currently doesn't ensure equivalence of concurrent and serializable execution |
| 246 |
of transactions that perform write operations. |
| 247 |
This isolation level is very expensive in databases with many tables. |
| 248 |
To enable, execute the SQL statement |
| 249 |
<code>SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE</code> |
| 250 |
</li> |
| 251 |
</ul> |
| 252 |
<ul> |
| 253 |
<li><b>Dirty reads</b><br /> |
| 254 |
Means a connection can read uncommitted changes made by another connection.<br /> |
| 255 |
Possible with: read uncommitted. |
| 256 |
</li><li><b>Non-repeatable reads</b><br /> |
| 257 |
A connection reads a row, another connection changes a row and commits, |
| 258 |
and the first connection re-reads the same row and gets the new result.<br /> |
| 259 |
Possible with: read uncommitted, read committed. |
| 260 |
</li><li><b>Phantom reads</b><br /> |
| 261 |
A connection reads a set of rows using a condition, another connection |
| 262 |
inserts a row that falls in this condition and commits, then the first connection |
| 263 |
re-reads using the same condition and gets the new row.<br /> |
| 264 |
Possible with: read uncommitted, read committed, repeatable read. |
| 265 |
</li> |
| 266 |
</ul> |
| 267 |
|
| 268 |
<h3 id="mvcc">Multi-Version Concurrency Control (MVCC)</h3> |
| 269 |
<p> |
| 270 |
Insert and update operations only issue a shared lock on the table. |
| 271 |
An exclusive lock is still used when adding or removing columns or when dropping the table. |
| 272 |
Connections only 'see' committed data, and own changes. That means, if connection A updates |
| 273 |
a row but doesn't commit this change yet, connection B will see the old value. |
| 274 |
Only when the change is committed, the new value is visible by other connections |
| 275 |
(read committed). If multiple connections concurrently try to lock or update the same row, the |
| 276 |
database waits until it can apply the change, but at most until the lock timeout expires. |
| 277 |
</p> |
| 278 |
|
| 279 |
<h3>Lock Timeout</h3> |
| 280 |
<p> |
| 281 |
If a connection cannot get a lock on an object, the connection waits for some amount |
| 282 |
of time (the lock timeout). During this time, hopefully the connection holding the |
| 283 |
lock commits and it is then possible to get the lock. If this is not possible because |
| 284 |
the other connection does not release the lock for some time, the unsuccessful |
| 285 |
connection will get a lock timeout exception. The lock timeout can be set individually |
| 286 |
for each connection. |
| 287 |
</p> |
| 288 |
|
| 289 |
<h2 id="clustering">Clustering / High Availability</h2> |
| 290 |
<p> |
| 291 |
This database supports a simple clustering / high availability mechanism. The architecture is: |
| 292 |
two database servers run on two different computers, and on both computers is a copy of the |
| 293 |
same database. If both servers run, each database operation is executed on both computers. |
| 294 |
If one server fails (power, hardware or network failure), the other server can still continue to work. |
| 295 |
From this point on, the operations will be executed only on one server until the other server |
| 296 |
is back up. |
| 297 |
</p><p> |
| 298 |
Clustering can only be used in the server mode (the embedded mode does not support clustering). |
| 299 |
The cluster can be re-created using the <code>CreateCluster</code> tool without stopping |
| 300 |
the remaining server. Applications that are still connected are automatically disconnected, |
| 301 |
however when appending <code>;AUTO_RECONNECT=TRUE</code>, they will recover from that. |
| 302 |
</p><p> |
| 303 |
To initialize the cluster, use the following steps: |
| 304 |
</p> |
| 305 |
<ul> |
| 306 |
<li>Create a database |
| 307 |
</li><li>Use the <code>CreateCluster</code> tool to copy the database to |
| 308 |
another location and initialize the clustering. |
| 309 |
Afterwards, you have two databases containing the same data. |
| 310 |
</li><li>Start two servers (one for each copy of the database) |
| 311 |
</li><li>You are now ready to connect to the databases with the client application(s) |
| 312 |
</li></ul> |
| 313 |
|
| 314 |
<h3>Using the CreateCluster Tool</h3> |
| 315 |
<p> |
| 316 |
To understand how clustering works, please try out the following example. |
| 317 |
In this example, the two databases reside on the same computer, but usually, the |
| 318 |
databases will be on different servers. |
| 319 |
</p> |
| 320 |
<ul> |
| 321 |
<li>Create two directories: <code>server1, server2</code>. |
| 322 |
Each directory will simulate a directory on a computer. |
| 323 |
</li><li>Start a TCP server pointing to the first directory. |
| 324 |
You can do this using the command line: |
| 325 |
<pre> |
| 326 |
java org.h2.tools.Server |
| 327 |
-tcp -tcpPort 9101 |
| 328 |
-baseDir server1 |
| 329 |
</pre> |
| 330 |
</li><li>Start a second TCP server pointing to the second directory. |
| 331 |
This will simulate a server running on a second (redundant) computer. |
| 332 |
You can do this using the command line: |
| 333 |
<pre> |
| 334 |
java org.h2.tools.Server |
| 335 |
-tcp -tcpPort 9102 |
| 336 |
-baseDir server2 |
| 337 |
</pre> |
| 338 |
</li><li>Use the <code>CreateCluster</code> tool to initialize clustering. |
| 339 |
This will automatically create a new, empty database if it does not exist. |
| 340 |
Run the tool on the command line: |
| 341 |
<pre> |
| 342 |
java org.h2.tools.CreateCluster |
| 343 |
-urlSource jdbc:h2:tcp://localhost:9101/~/test |
| 344 |
-urlTarget jdbc:h2:tcp://localhost:9102/~/test |
| 345 |
-user sa |
| 346 |
-serverList localhost:9101,localhost:9102 |
| 347 |
</pre> |
| 348 |
</li><li>You can now connect to the databases using |
| 349 |
an application or the H2 Console using the JDBC URL |
| 350 |
<code>jdbc:h2:tcp://localhost:9101,localhost:9102/~/test</code> |
| 351 |
</li><li>If you stop a server (by killing the process), |
| 352 |
you will notice that the other machine continues to work, |
| 353 |
and therefore the database is still accessible. |
| 354 |
</li><li>To restore the cluster, you first need to delete the |
| 355 |
database that failed, then restart the server that was stopped, |
| 356 |
and re-run the <code>CreateCluster</code> tool. |
| 357 |
</li></ul> |
| 358 |
|
| 359 |
<h3>Detect Which Cluster Instances are Running</h3> |
| 360 |
<p> |
| 361 |
To find out which cluster nodes are currently running, execute the following SQL statement: |
| 362 |
</p> |
| 363 |
<pre> |
| 364 |
SELECT SETTING_VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE SETTING_NAME = 'CLUSTER' |
| 365 |
</pre> |
| 366 |
<p> |
| 367 |
If the result is <code>''</code> (two single quotes), then the cluster mode is disabled. Otherwise, the list of |
| 368 |
servers is returned, enclosed in single quote. Example: <code>'server1:9191,server2:9191'</code>. |
| 369 |
</p> |
| 370 |
|
| 371 |
<p> |
| 372 |
It is also possible to get the list of servers by using Connection.getClientInfo(). |
| 373 |
</p> |
| 374 |
|
| 375 |
<p> |
| 376 |
The property list returned from <code>getClientInfo()</code> contains a <code>numServers</code> property that returns the |
| 377 |
number of servers that are in the connection list. To get the actual servers, <code>getClientInfo()</code> also has |
| 378 |
properties <code>server0</code>..<code>serverX</code>, where serverX is the number of servers minus 1. |
| 379 |
</p> |
| 380 |
|
| 381 |
<p> |
| 382 |
Example: To get the 2nd server in the connection list one uses <code>getClientInfo('server1')</code>. |
| 383 |
<b>Note:</b> The <code>serverX</code> property only returns IP addresses and ports and not hostnames. |
| 384 |
</p> |
| 385 |
|
| 386 |
<h3>Clustering Algorithm and Limitations</h3> |
| 387 |
<p> |
| 388 |
Read-only queries are only executed against the first cluster node, but all other statements are |
| 389 |
executed against all nodes. There is currently no load balancing made to avoid problems with |
| 390 |
transactions. The following functions may yield different results on different cluster nodes and must be |
| 391 |
executed with care: <code>UUID(), RANDOM_UUID(), SECURE_RAND(), SESSION_ID(), |
| 392 |
MEMORY_FREE(), MEMORY_USED(), CSVREAD(), CSVWRITE(), RAND()</code> [when not using a seed]. |
| 393 |
Those functions should not be used directly in modifying statements |
| 394 |
(for example <code>INSERT, UPDATE, MERGE</code>). However, they can be used |
| 395 |
in read-only statements and the result can then be used for modifying statements. |
| 396 |
Identity columns aren't supported. |
| 397 |
Instead, sequence values need to be manually requested and then used to insert data (using two statements). |
| 398 |
</p> |
| 399 |
<p> |
| 400 |
When using the cluster modes, result sets are read fully in memory by the client, so that |
| 401 |
there is no problem if the server dies that executed the query. Result sets must fit in memory |
| 402 |
on the client side. |
| 403 |
</p> |
| 404 |
<p> |
| 405 |
The SQL statement <code>SET AUTOCOMMIT FALSE</code> is not supported in the cluster mode. |
| 406 |
To disable autocommit, the method <code>Connection.setAutoCommit(false)</code> needs to be called. |
| 407 |
</p> |
| 408 |
<p> |
| 409 |
It is possible that a transaction from one connection overtakes a transaction from a different connection. |
| 410 |
Depending on the operations, this might result in different results, for example when |
| 411 |
conditionally incrementing a value in a row. |
| 412 |
</p> |
| 413 |
|
| 414 |
<h2 id="two_phase_commit">Two Phase Commit</h2> |
| 415 |
<p> |
| 416 |
The two phase commit protocol is supported. 2-phase-commit works as follows: |
| 417 |
</p> |
| 418 |
<ul> |
| 419 |
<li>Autocommit needs to be switched off |
| 420 |
</li><li>A transaction is started, for example by inserting a row |
| 421 |
</li><li>The transaction is marked 'prepared' by executing the SQL statement |
| 422 |
<code>PREPARE COMMIT transactionName</code> |
| 423 |
</li><li>The transaction can now be committed or rolled back |
| 424 |
</li><li>If a problem occurs before the transaction was successfully committed or rolled back |
| 425 |
(for example because a network problem occurred), the transaction is in the state 'in-doubt' |
| 426 |
</li><li>When re-connecting to the database, the in-doubt transactions can be listed |
| 427 |
with <code>SELECT * FROM INFORMATION_SCHEMA.IN_DOUBT</code> |
| 428 |
</li><li>Each transaction in this list must now be committed or rolled back by executing |
| 429 |
<code>COMMIT TRANSACTION transactionName</code> or |
| 430 |
<code>ROLLBACK TRANSACTION transactionName</code> |
| 431 |
</li><li>The database needs to be closed and re-opened to apply the changes |
| 432 |
</li></ul> |
| 433 |
|
| 434 |
<h2 id="compatibility">Compatibility</h2> |
| 435 |
<p> |
| 436 |
This database is (up to a certain point) compatible to other databases such as HSQLDB, MySQL and PostgreSQL. |
| 437 |
There are certain areas where H2 is incompatible. |
| 438 |
</p> |
| 439 |
|
| 440 |
<h3>Transaction Commit when Autocommit is On</h3> |
| 441 |
<p> |
| 442 |
At this time, this database engine commits a transaction (if autocommit is switched on) just before returning the result. |
| 443 |
For a query, this means the transaction is committed even before the application scans through the result set, and before the result set is closed. |
| 444 |
Other database engines may commit the transaction in this case when the result set is closed. |
| 445 |
</p> |
| 446 |
|
| 447 |
<h2 id="keywords">Keywords / Reserved Words</h2> |
| 448 |
<p> |
| 449 |
There is a list of keywords that can't be used as identifiers (table names, column names and so on), |
| 450 |
unless they are quoted (surrounded with double quotes). |
| 451 |
The following tokens are keywords in H2: |
| 452 |
</p> |
| 453 |
<table class="main"> |
| 454 |
<thead> |
| 455 |
<tr> |
| 456 |
<th rowspan="2">Keyword</th> |
| 457 |
<th rowspan="2">H2</th> |
| 458 |
<th colspan="6" style="text-align: center">SQL Standard</th> |
| 459 |
</tr> |
| 460 |
<tr> |
| 461 |
<th>2016</th> |
| 462 |
<th>2011</th> |
| 463 |
<th>2008</th> |
| 464 |
<th>2003</th> |
| 465 |
<th>1999</th> |
| 466 |
<th>92</th> |
| 467 |
</tr> |
| 468 |
</thead> |
| 469 |
<tbody> |
| 470 |
<tr><td>ALL</td> |
| 471 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 472 |
<tr><td>AND</td> |
| 473 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 474 |
<tr><td>ANY</td> |
| 475 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 476 |
<tr><td>ARRAY</td> |
| 477 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td></tr> |
| 478 |
<tr><td>AS</td> |
| 479 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 480 |
<tr><td>ASYMMETRIC</td> |
| 481 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>NR</td><td></td></tr> |
| 482 |
<tr><td>AUTHORIZATION</td> |
| 483 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 484 |
<tr><td>BETWEEN</td> |
| 485 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>NR</td><td>+</td></tr> |
| 486 |
<tr><td>BOTH</td> |
| 487 |
<td>CS</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 488 |
<tr><td>CASE</td> |
| 489 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 490 |
<tr><td>CAST</td> |
| 491 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 492 |
<tr><td>CHECK</td> |
| 493 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 494 |
<tr><td>CONSTRAINT</td> |
| 495 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 496 |
<tr><td>CROSS</td> |
| 497 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 498 |
<tr><td>CURRENT_CATALOG</td> |
| 499 |
<td>+</td><td>+</td><td>+</td><td>+</td><td></td><td></td><td></td></tr> |
| 500 |
<tr><td>CURRENT_DATE</td> |
| 501 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 502 |
<tr><td>CURRENT_PATH</td> |
| 503 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td></tr> |
| 504 |
<tr><td>CURRENT_ROLE</td> |
| 505 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td></tr> |
| 506 |
<tr><td>CURRENT_SCHEMA</td> |
| 507 |
<td>+</td><td>+</td><td>+</td><td>+</td><td></td><td></td><td></td></tr> |
| 508 |
<tr><td>CURRENT_TIME</td> |
| 509 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 510 |
<tr><td>CURRENT_TIMESTAMP</td> |
| 511 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 512 |
<tr><td>CURRENT_USER</td> |
| 513 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 514 |
<tr><td>DAY</td> |
| 515 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 516 |
<tr><td>DEFAULT</td> |
| 517 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 518 |
<tr><td>DISTINCT</td> |
| 519 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 520 |
<tr><td>ELSE</td> |
| 521 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 522 |
<tr><td>END</td> |
| 523 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 524 |
<tr><td>EXCEPT</td> |
| 525 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 526 |
<tr><td>EXISTS</td> |
| 527 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>NR</td><td>+</td></tr> |
| 528 |
<tr><td>FALSE</td> |
| 529 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 530 |
<tr><td>FETCH</td> |
| 531 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 532 |
<tr><td>FOR</td> |
| 533 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 534 |
<tr><td>FOREIGN</td> |
| 535 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 536 |
<tr><td>FROM</td> |
| 537 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 538 |
<tr><td>FULL</td> |
| 539 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 540 |
<tr><td>GROUP</td> |
| 541 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 542 |
<tr><td>GROUPS</td> |
| 543 |
<td>CS</td><td>+</td><td>+</td><td></td><td></td><td></td><td></td></tr> |
| 544 |
<tr><td>HAVING</td> |
| 545 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 546 |
<tr><td>HOUR</td> |
| 547 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 548 |
<tr><td>IF</td> |
| 549 |
<td>+</td><td></td><td></td><td></td><td></td><td></td><td></td></tr> |
| 550 |
<tr><td>ILIKE</td> |
| 551 |
<td>CS</td><td></td><td></td><td></td><td></td><td></td><td></td></tr> |
| 552 |
<tr><td>IN</td> |
| 553 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 554 |
<tr><td>INNER</td> |
| 555 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 556 |
<tr><td>INTERSECT</td> |
| 557 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 558 |
<tr><td>INTERVAL</td> |
| 559 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 560 |
<tr><td>IS</td> |
| 561 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 562 |
<tr><td>JOIN</td> |
| 563 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 564 |
<tr><td>KEY</td> |
| 565 |
<td>+</td><td>NR</td><td>NR</td><td>NR</td><td>NR</td><td>+</td><td>+</td></tr> |
| 566 |
<tr><td>LEADING</td> |
| 567 |
<td>CS</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 568 |
<tr><td>LEFT</td> |
| 569 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 570 |
<tr><td>LIKE</td> |
| 571 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 572 |
<tr><td>LIMIT</td> |
| 573 |
<td>MS</td><td></td><td></td><td></td><td></td><td>+</td><td></td></tr> |
| 574 |
<tr><td>LOCALTIME</td> |
| 575 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td></tr> |
| 576 |
<tr><td>LOCALTIMESTAMP</td> |
| 577 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td></tr> |
| 578 |
<tr><td>MINUS</td> |
| 579 |
<td>MS</td><td></td><td></td><td></td><td></td><td></td><td></td></tr> |
| 580 |
<tr><td>MINUTE</td> |
| 581 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 582 |
<tr><td>MONTH</td> |
| 583 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 584 |
<tr><td>NATURAL</td> |
| 585 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 586 |
<tr><td>NOT</td> |
| 587 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 588 |
<tr><td>NULL</td> |
| 589 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 590 |
<tr><td>OFFSET</td> |
| 591 |
<td>+</td><td>+</td><td>+</td><td>+</td><td></td><td></td><td></td></tr> |
| 592 |
<tr><td>ON</td> |
| 593 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 594 |
<tr><td>OR</td> |
| 595 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 596 |
<tr><td>ORDER</td> |
| 597 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 598 |
<tr><td>OVER</td> |
| 599 |
<td>CS</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td><td></td></tr> |
| 600 |
<tr><td>PARTITION</td> |
| 601 |
<td>CS</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td><td></td></tr> |
| 602 |
<tr><td>PRIMARY</td> |
| 603 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 604 |
<tr><td>QUALIFY</td> |
| 605 |
<td>+</td><td></td><td></td><td></td><td></td><td></td><td></td></tr> |
| 606 |
<tr><td>RANGE</td> |
| 607 |
<td>CS</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td><td></td></tr> |
| 608 |
<tr><td>REGEXP</td> |
| 609 |
<td>CS</td><td></td><td></td><td></td><td></td><td></td><td></td></tr> |
| 610 |
<tr><td>RIGHT</td> |
| 611 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 612 |
<tr><td>ROW</td> |
| 613 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td></tr> |
| 614 |
<tr><td>ROWNUM</td> |
| 615 |
<td>+</td><td></td><td></td><td></td><td></td><td></td><td></td></tr> |
| 616 |
<tr><td>ROWS</td> |
| 617 |
<td>CS</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 618 |
<tr><td>SECOND</td> |
| 619 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 620 |
<tr><td>SELECT</td> |
| 621 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 622 |
<tr><td>SESSION_USER</td> |
| 623 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td></tr> |
| 624 |
<tr><td>SET</td> |
| 625 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 626 |
<tr><td>SOME</td> |
| 627 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 628 |
<tr><td>SYMMETRIC</td> |
| 629 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>NR</td><td></td></tr> |
| 630 |
<tr><td>SYSTEM_USER</td> |
| 631 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 632 |
<tr><td>TABLE</td> |
| 633 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 634 |
<tr><td>TO</td> |
| 635 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 636 |
<tr><td>TOP</td> |
| 637 |
<td>MS<br/>CS</td><td></td><td></td><td></td><td></td><td></td><td></td></tr> |
| 638 |
<tr><td>TRAILING</td> |
| 639 |
<td>CS</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 640 |
<tr><td>TRUE</td> |
| 641 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 642 |
<tr><td>UESCAPE</td> |
| 643 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td><td></td></tr> |
| 644 |
<tr><td>UNION</td> |
| 645 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 646 |
<tr><td>UNIQUE</td> |
| 647 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 648 |
<tr><td>UNKNOWN</td> |
| 649 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 650 |
<tr><td>USER</td> |
| 651 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 652 |
<tr><td>USING</td> |
| 653 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 654 |
<tr><td>VALUE</td> |
| 655 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 656 |
<tr><td>VALUES</td> |
| 657 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 658 |
<tr><td>WHEN</td> |
| 659 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 660 |
<tr><td>WHERE</td> |
| 661 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 662 |
<tr><td>WINDOW</td> |
| 663 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td></td><td></td></tr> |
| 664 |
<tr><td>WITH</td> |
| 665 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 666 |
<tr><td>YEAR</td> |
| 667 |
<td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td><td>+</td></tr> |
| 668 |
<tr><td>_ROWID_</td> |
| 669 |
<td>+</td><td></td><td></td><td></td><td></td><td></td><td></td></tr> |
| 670 |
</tbody> |
| 671 |
</table> |
| 672 |
<p> |
| 673 |
Mode-sensitive keywords (MS) are keywords only in some compatibility modes. |
| 674 |
</p> |
| 675 |
<ul><li>LIMIT is a keywords only in Regular, Legacy, DB2, HSQLDB, MariaDB, MySQL, and PostgreSQL compatibility modes. |
| 676 |
It is an identifier in Strict, Derby, MSSQLServer, and Oracle compatibility modes. |
| 677 |
</li><li>MINUS is a keyword only in Regular, Legacy, DB2, HSQLDB, and Oracle compatibility modes. |
| 678 |
It is an identifier in Strict, Derby, MSSQLServer, MariaDB, MySQL, and PostgreSQL compatibility modes. |
| 679 |
</li><li>TOP is a context-sensitive keyword (can be either keyword or identifier) |
| 680 |
only in Regular, Legacy, HSQLDB, and MSSQLServer compatibility modes. |
| 681 |
It is an identifier unconditionally in Strict, Derby, DB2, MariaDB, MySQL, Oracle, and PostgreSQL compatibility modes. |
| 682 |
</li></ul> |
| 683 |
<p> |
| 684 |
Context-sensitive keywords (CS) can be used as identifiers in some places, |
| 685 |
but cannot be used as identifiers in others. |
| 686 |
Normal keywords (+) are always treated as keywords. |
| 687 |
</p> |
| 688 |
<p> |
| 689 |
Most keywords in H2 are also reserved (+) or non-reserved (NR) words in the SQL Standard. |
| 690 |
Newer versions of H2 may have more keywords than older ones. |
| 691 |
Reserved words from the SQL Standard are potential candidates for keywords in future versions. |
| 692 |
</p> |
| 693 |
|
| 694 |
<p>There is a compatibility setting |
| 695 |
<a href="commands.html#set_non_keywords"><code>SET NON_KEYWORDS</code></a> |
| 696 |
that can be used as a temporary workaround for applications that use keywords as unquoted identifiers.</p> |
| 697 |
|
| 698 |
<h2 id="standards_compliance">Standards Compliance</h2> |
| 699 |
<p> |
| 700 |
This database tries to be as much standard compliant as possible. For the SQL language, ANSI/ISO is the main |
| 701 |
standard. There are several versions that refer to the release date: SQL-92, SQL:1999, and SQL:2003. |
| 702 |
Unfortunately, the standard documentation is not freely available. Another problem is that important features |
| 703 |
are not standardized. Whenever this is the case, this database tries to be compatible to other databases. |
| 704 |
</p> |
| 705 |
|
| 706 |
<h3>Supported Character Sets, Character Encoding, and Unicode</h3> |
| 707 |
<p> |
| 708 |
H2 internally uses Unicode, and supports all character encoding systems and character sets supported by the virtual machine you use. |
| 709 |
</p> |
| 710 |
|
| 711 |
<h2 id="windows_service">Run as Windows Service</h2> |
| 712 |
<p> |
| 713 |
Using a native wrapper / adapter, Java applications can be run as a Windows Service. |
| 714 |
There are various tools available to do that. The Java Service Wrapper from |
| 715 |
<a href="https://wrapper.tanukisoftware.org">Tanuki Software, Inc.</a> |
| 716 |
is included in the installation. Batch files are provided to install, start, stop and uninstall the |
| 717 |
H2 Database Engine Service. This service contains the TCP Server and the H2 Console web application. |
| 718 |
The batch files are located in the directory <code>h2/service</code>. |
| 719 |
</p> |
| 720 |
<p> |
| 721 |
The service wrapper bundled with H2 is a 32-bit version. |
| 722 |
To use a 64-bit version of Windows (x64), you need to use a 64-bit version of the wrapper, |
| 723 |
for example the one from |
| 724 |
<a href="https://www.krenger.ch/blog/java-service-wrapper-3-5-14-for-windows-x64/"> |
| 725 |
Simon Krenger</a>. |
| 726 |
</p> |
| 727 |
<p> |
| 728 |
When running the database as a service, absolute path should be used. |
| 729 |
Using <code>~</code> in the database URL is problematic in this case, |
| 730 |
because it means to use the home directory of the current user. |
| 731 |
The service might run without or with the wrong user, so that |
| 732 |
the database files might end up in an unexpected place. |
| 733 |
</p> |
| 734 |
|
| 735 |
<h3>Install the Service</h3> |
| 736 |
<p> |
| 737 |
The service needs to be registered as a Windows Service first. |
| 738 |
To do that, double click on <code>1_install_service.bat</code>. |
| 739 |
If successful, a command prompt window will pop up and disappear immediately. If not, a message will appear. |
| 740 |
</p> |
| 741 |
|
| 742 |
<h3>Start the Service</h3> |
| 743 |
<p> |
| 744 |
You can start the H2 Database Engine Service using the service manager of Windows, |
| 745 |
or by double clicking on <code>2_start_service.bat</code>. |
| 746 |
Please note that the batch file does not print an error message if the service is not installed. |
| 747 |
</p> |
| 748 |
|
| 749 |
<h3>Connect to the H2 Console</h3> |
| 750 |
<p> |
| 751 |
After installing and starting the service, you can connect to the H2 Console application using a browser. |
| 752 |
Double clicking on <code>3_start_browser.bat</code> to do that. The |
| 753 |
default port (8082) is hard coded in the batch file. |
| 754 |
</p> |
| 755 |
|
| 756 |
<h3>Stop the Service</h3> |
| 757 |
<p> |
| 758 |
To stop the service, double click on <code>4_stop_service.bat</code>. |
| 759 |
Please note that the batch file does not print an error message if the service is not installed or started. |
| 760 |
</p> |
| 761 |
|
| 762 |
<h3>Uninstall the Service</h3> |
| 763 |
<p> |
| 764 |
To uninstall the service, double click on <code>5_uninstall_service.bat</code>. |
| 765 |
If successful, a command prompt window will pop up and disappear immediately. If not, a message will appear. |
| 766 |
</p> |
| 767 |
|
| 768 |
<h3>Additional JDBC drivers</h3> |
| 769 |
<p> |
| 770 |
To use other databases (for example MySQL), the location of the JDBC drivers of those databases need to be |
| 771 |
added to the environment variables <code>H2DRIVERS</code> or <code>CLASSPATH</code> before |
| 772 |
installing the service. Multiple drivers can be set; each entry needs to be separated with a <code>;</code> |
| 773 |
(Windows) or <code>:</code> (other operating systems). Spaces in the path names are supported. |
| 774 |
The settings must not be quoted. |
| 775 |
</p> |
| 776 |
|
| 777 |
<h2 id="odbc_driver">ODBC Driver</h2> |
| 778 |
<p> |
| 779 |
This database does not come with its own ODBC driver at this time, |
| 780 |
but it supports the PostgreSQL network protocol. |
| 781 |
Therefore, the PostgreSQL ODBC driver can be used. |
| 782 |
Support for the PostgreSQL network protocol is quite new and should be viewed |
| 783 |
as experimental. It should not be used for production applications. |
| 784 |
</p> |
| 785 |
<p> |
| 786 |
To use the PostgreSQL ODBC driver on 64 bit versions of Windows, |
| 787 |
first run <code>c:/windows/syswow64/odbcad32.exe</code>. |
| 788 |
At this point you set up your DSN just like you would on any other system. |
| 789 |
See also: |
| 790 |
<a href="https://www.postgresql.org/message-id/[email protected]">Re: ODBC Driver on Windows 64 bit</a> |
| 791 |
</p> |
| 792 |
|
| 793 |
<h3>ODBC Installation</h3> |
| 794 |
<p> |
| 795 |
First, the ODBC driver must be installed. |
| 796 |
Any recent PostgreSQL ODBC driver should work, however version 8.2 (<code>psqlodbc-08_02*</code>) or newer is recommended. |
| 797 |
The Windows version of the PostgreSQL ODBC driver is available at |
| 798 |
<a href="https://www.postgresql.org/ftp/odbc/versions/msi/">https://www.postgresql.org/ftp/odbc/versions/msi/</a>. |
| 799 |
</p> |
| 800 |
|
| 801 |
<h3>Starting the Server</h3> |
| 802 |
<p> |
| 803 |
After installing the ODBC driver, start the H2 Server using the command line: |
| 804 |
</p> |
| 805 |
<pre> |
| 806 |
java -cp h2*.jar org.h2.tools.Server |
| 807 |
</pre> |
| 808 |
<p> |
| 809 |
The PG Server (PG for PostgreSQL protocol) is started as well. |
| 810 |
By default, databases are stored in the current working directory where the server is started. |
| 811 |
Use <code>-baseDir</code> to save databases in another directory, for example the user home directory: |
| 812 |
</p> |
| 813 |
<pre> |
| 814 |
java -cp h2*.jar org.h2.tools.Server -baseDir ~ |
| 815 |
</pre> |
| 816 |
<p> |
| 817 |
The PG server can be started and stopped from within a Java application as follows: |
| 818 |
</p> |
| 819 |
<pre> |
| 820 |
Server server = Server.createPgServer("-baseDir", "~"); |
| 821 |
server.start(); |
| 822 |
... |
| 823 |
server.stop(); |
| 824 |
</pre> |
| 825 |
<p> |
| 826 |
By default, only connections from localhost are allowed. To allow remote connections, use |
| 827 |
<code>-pgAllowOthers</code> when starting the server. |
| 828 |
</p> |
| 829 |
<p> |
| 830 |
To map an ODBC database name to a different JDBC database name, |
| 831 |
use the option <code>-key</code> when starting the server. |
| 832 |
Please note only one mapping is allowed. The following will map the ODBC database named |
| 833 |
<code>TEST</code> to the database URL <code>jdbc:h2:~/data/test;cipher=aes</code>: |
| 834 |
</p> |
| 835 |
<pre> |
| 836 |
java org.h2.tools.Server -pg -key TEST "~/data/test;cipher=aes" |
| 837 |
</pre> |
| 838 |
|
| 839 |
<h3>ODBC Configuration</h3> |
| 840 |
<p> |
| 841 |
After installing the driver, a new Data Source must be added. In Windows, |
| 842 |
run <code>odbcad32.exe</code> to open the Data Source Administrator. Then click on 'Add...' |
| 843 |
and select the PostgreSQL Unicode driver. Then click 'Finish'. |
| 844 |
You will be able to change the connection properties. |
| 845 |
The property column represents the property key in the <code>odbc.ini</code> file |
| 846 |
(which may be different from the GUI). |
| 847 |
</p> |
| 848 |
<table class="main"> |
| 849 |
<tr><th>Property</th><th>Example</th><th>Remarks</th></tr> |
| 850 |
<tr><td>Data Source</td><td>H2 Test</td><td>The name of the ODBC Data Source</td></tr> |
| 851 |
<tr><td>Database</td><td>~/test;ifexists=true</td> |
| 852 |
<td> |
| 853 |
The database name. This can include connections settings. |
| 854 |
By default, the database is stored in the current working directory |
| 855 |
where the Server is started except when the -baseDir setting is used. |
| 856 |
The name must be at least 3 characters. |
| 857 |
</td></tr> |
| 858 |
<tr><td>Servername</td><td>localhost</td><td>The server name or IP address.<br />By default, only remote connections are allowed</td></tr> |
| 859 |
<tr><td>Username</td><td>sa</td><td>The database user name.</td></tr> |
| 860 |
<tr><td>SSL</td><td>false (disabled)</td><td>At this time, SSL is not supported.</td></tr> |
| 861 |
<tr><td>Port</td><td>5435</td><td>The port where the PG Server is listening.</td></tr> |
| 862 |
<tr><td>Password</td><td>sa</td><td>The database password.</td></tr> |
| 863 |
</table> |
| 864 |
<p> |
| 865 |
To improve performance, please enable 'server side prepare' under Options / Datasource / Page 2 / Server side prepare. |
| 866 |
</p> |
| 867 |
<p> |
| 868 |
Afterwards, you may use this data source. |
| 869 |
</p> |
| 870 |
|
| 871 |
<h3>PG Protocol Support Limitations</h3> |
| 872 |
<p> |
| 873 |
At this time, only a subset of the PostgreSQL network protocol is implemented. |
| 874 |
Also, there may be compatibility problems on the SQL level, with the catalog, or with text encoding. |
| 875 |
Problems are fixed as they are found. |
| 876 |
Currently, statements can not be canceled when using the PG protocol. |
| 877 |
Also, H2 does not provide index meta over ODBC. |
| 878 |
</p> |
| 879 |
<p> |
| 880 |
PostgreSQL ODBC Driver Setup requires a database password; that means it |
| 881 |
is not possible to connect to H2 databases without password. This is a limitation |
| 882 |
of the ODBC driver. |
| 883 |
</p> |
| 884 |
|
| 885 |
<h3>Security Considerations</h3> |
| 886 |
<p> |
| 887 |
Currently, the PG Server does not support challenge response or encrypt passwords. |
| 888 |
This may be a problem if an attacker can listen to the data transferred between the ODBC driver |
| 889 |
and the server, because the password is readable to the attacker. |
| 890 |
Also, it is currently not possible to use encrypted SSL connections. |
| 891 |
Therefore the ODBC driver should not be used where security is important. |
| 892 |
</p> |
| 893 |
<p> |
| 894 |
The first connection that opens a database using the PostgreSQL server needs to be an administrator user. |
| 895 |
Subsequent connections don't need to be opened by an administrator. |
| 896 |
</p> |
| 897 |
|
| 898 |
<h3>Using Microsoft Access</h3> |
| 899 |
<p> |
| 900 |
When using Microsoft Access to edit data in a linked H2 table, you may need to enable the following option: |
| 901 |
Tools - Options - Edit/Find - ODBC fields. |
| 902 |
</p> |
| 903 |
|
| 904 |
<h2 id="acid">ACID</h2> |
| 905 |
<p> |
| 906 |
In the database world, ACID stands for: |
| 907 |
</p> |
| 908 |
<ul> |
| 909 |
<li>Atomicity: transactions must be atomic, meaning either all tasks are performed or none. |
| 910 |
</li><li>Consistency: all operations must comply with the defined constraints. |
| 911 |
</li><li>Isolation: transactions must be isolated from each other. |
| 912 |
</li><li>Durability: committed transaction will not be lost. |
| 913 |
</li></ul> |
| 914 |
|
| 915 |
<h3>Atomicity</h3> |
| 916 |
<p> |
| 917 |
Transactions in this database are always atomic. |
| 918 |
</p> |
| 919 |
|
| 920 |
<h3>Consistency</h3> |
| 921 |
<p> |
| 922 |
By default, this database is always in a consistent state. |
| 923 |
Referential integrity rules are enforced except when |
| 924 |
explicitly disabled. |
| 925 |
</p> |
| 926 |
|
| 927 |
<h3>Isolation</h3> |
| 928 |
<p> |
| 929 |
For H2, as with most other database systems, the default isolation level is 'read committed'. |
| 930 |
This provides better performance, but also means that transactions are not completely isolated. |
| 931 |
H2 supports the transaction isolation levels 'read uncommitted', 'read committed', 'repeatable read', |
| 932 |
and 'serializable'. |
| 933 |
</p> |
| 934 |
|
| 935 |
<h3>Durability</h3> |
| 936 |
<p> |
| 937 |
This database does not guarantee that all committed transactions survive a power failure. |
| 938 |
Tests show that all databases sometimes lose transactions on power failure (for details, see below). |
| 939 |
Where losing transactions is not acceptable, a laptop or UPS (uninterruptible power supply) should be used. |
| 940 |
If durability is required for all possible cases of hardware failure, clustering should be used, |
| 941 |
such as the H2 clustering mode. |
| 942 |
</p> |
| 943 |
|
| 944 |
<h2 id="durability_problems">Durability Problems</h2> |
| 945 |
<p> |
| 946 |
Complete durability means all committed transaction survive a power failure. |
| 947 |
Some databases claim they can guarantee durability, but such claims are wrong. |
| 948 |
A durability test was run against H2, HSQLDB, PostgreSQL, and Derby. |
| 949 |
All of those databases sometimes lose committed transactions. |
| 950 |
The test is included in the H2 download, see <code>org.h2.test.poweroff.Test</code>. |
| 951 |
</p> |
| 952 |
|
| 953 |
<h3>Ways to (Not) Achieve Durability</h3> |
| 954 |
<p> |
| 955 |
Making sure that committed transactions are not lost is more complicated than it seems first. |
| 956 |
To guarantee complete durability, a database must ensure that the log record is on the hard drive |
| 957 |
before the commit call returns. To do that, databases use different methods. One |
| 958 |
is to use the 'synchronous write' file access mode. In Java, <code>RandomAccessFile</code> |
| 959 |
supports the modes <code>rws</code> and <code>rwd</code>: |
| 960 |
</p> |
| 961 |
<ul> |
| 962 |
<li><code>rwd</code>: every update to the file's content is written synchronously to the underlying storage device. |
| 963 |
</li><li><code>rws</code>: in addition to <code>rwd</code>, every update to the metadata is written synchronously.</li> |
| 964 |
</ul> |
| 965 |
<p> |
| 966 |
A test (<code>org.h2.test.poweroff.TestWrite</code>) with one of those modes achieves |
| 967 |
around 50 thousand write operations per second. |
| 968 |
Even when the operating system write buffer is disabled, the write rate is around 50 thousand operations per second. |
| 969 |
This feature does not force changes to disk because it does not flush all buffers. |
| 970 |
The test updates the same byte in the file again and again. If the hard drive was able to write at this rate, |
| 971 |
then the disk would need to make at least 50 thousand revolutions per second, or 3 million RPM |
| 972 |
(revolutions per minute). There are no such hard drives. The hard drive used for the test is about 7200 RPM, |
| 973 |
or about 120 revolutions per second. There is an overhead, so the maximum write rate must be lower than that. |
| 974 |
</p> |
| 975 |
<p> |
| 976 |
Calling <code>fsync</code> flushes the buffers. There are two ways to do that in Java: |
| 977 |
</p> |
| 978 |
<ul> |
| 979 |
<li><code>FileDescriptor.sync()</code>. The documentation says that this forces all system |
| 980 |
buffers to synchronize with the underlying device. |
| 981 |
This method is supposed to return after all in-memory modified copies of buffers associated with this file descriptor |
| 982 |
have been written to the physical medium. |
| 983 |
</li><li><code>FileChannel.force()</code>. This method is supposed |
| 984 |
to force any updates to this channel's file to be written to the storage device that contains it. |
| 985 |
</li></ul> |
| 986 |
<p> |
| 987 |
By default, MySQL calls <code>fsync</code> for each commit. When using one of those methods, only around 60 write operations |
| 988 |
per second can be achieved, which is consistent with the RPM rate of the hard drive used. |
| 989 |
Unfortunately, even when calling <code>FileDescriptor.sync()</code> or |
| 990 |
<code>FileChannel.force()</code>, |
| 991 |
data is not always persisted to the hard drive, because most hard drives do not obey |
| 992 |
<code>fsync()</code>: see |
| 993 |
<a href="https://hardware.slashdot.org/story/05/05/13/0529252/your-hard-drive-lies-to-you">Your Hard Drive Lies to You</a>. |
| 994 |
In Mac OS X, <code>fsync</code> does not flush hard drive buffers. See |
| 995 |
<a href="https://lists.apple.com/archives/darwin-dev/2005/Feb/msg00072.html">Bad fsync?</a>. |
| 996 |
So the situation is confusing, and tests prove there is a problem. |
| 997 |
</p> |
| 998 |
<p> |
| 999 |
Trying to flush hard drive buffers is hard, and if you do the performance is very bad. |
| 1000 |
First you need to make sure that the hard drive actually flushes all buffers. |
| 1001 |
Tests show that this can not be done in a reliable way. |
| 1002 |
Then the maximum number of transactions is around 60 per second. |
| 1003 |
Because of those reasons, the default behavior of H2 is to delay writing committed transactions. |
| 1004 |
</p> |
| 1005 |
<p> |
| 1006 |
In H2, after a power failure, a bit more than one second of committed transactions may be lost. |
| 1007 |
To change the behavior, use <code>SET WRITE_DELAY</code> and |
| 1008 |
<code>CHECKPOINT SYNC</code>. |
| 1009 |
Most other databases support commit delay as well. |
| 1010 |
In the performance comparison, commit delay was used for all databases that support it. |
| 1011 |
</p> |
| 1012 |
|
| 1013 |
<h3>Running the Durability Test</h3> |
| 1014 |
<p> |
| 1015 |
To test the durability / non-durability of this and other databases, you can use the test application |
| 1016 |
in the package <code>org.h2.test.poweroff</code>. |
| 1017 |
Two computers with network connection are required to run this test. |
| 1018 |
One computer just listens, while the test application is run (and power is cut) on the other computer. |
| 1019 |
The computer with the listener application opens a TCP/IP port and listens for an incoming connection. |
| 1020 |
The second computer first connects to the listener, and then created the databases and starts inserting |
| 1021 |
records. The connection is set to 'autocommit', which means after each inserted record a commit is performed |
| 1022 |
automatically. Afterwards, the test computer notifies the listener that this record was inserted successfully. |
| 1023 |
The listener computer displays the last inserted record number every 10 seconds. Now, switch off the power |
| 1024 |
manually, then restart the computer, and run the application again. You will find out that in most cases, |
| 1025 |
none of the databases contains all the records that the listener computer knows about. For details, please |
| 1026 |
consult the source code of the listener and test application. |
| 1027 |
</p> |
| 1028 |
|
| 1029 |
<h2 id="using_recover_tool">Using the Recover Tool</h2> |
| 1030 |
<p> |
| 1031 |
The <code>Recover</code> tool can be used to extract the contents of a database file, even if the database is corrupted. |
| 1032 |
It also extracts the content of the transaction log and large objects (CLOB or BLOB). |
| 1033 |
To run the tool, type on the command line: |
| 1034 |
</p> |
| 1035 |
<pre> |
| 1036 |
java -cp h2*.jar org.h2.tools.Recover |
| 1037 |
</pre> |
| 1038 |
<p> |
| 1039 |
For each database in the current directory, a text file will be created. |
| 1040 |
This file contains raw insert statements (for the data) and data definition (DDL) statements to recreate |
| 1041 |
the schema of the database. This file can be executed using the <code>RunScript</code> tool or a |
| 1042 |
<a href="https://h2database.com/html/commands.html#runscript"><code>RUNSCRIPT</code></a> SQL statement. |
| 1043 |
The script includes at least one |
| 1044 |
<code>CREATE USER</code> statement. If you run the script against a database that was created with the same |
| 1045 |
user, or if there are conflicting users, running the script will fail. Consider running the script |
| 1046 |
against a database that was created with a user name that is not in the script. |
| 1047 |
</p> |
| 1048 |
<p> |
| 1049 |
The <code>Recover</code> tool creates a SQL script from database file. It also processes the transaction log. |
| 1050 |
</p> |
| 1051 |
<p> |
| 1052 |
To verify the database can recover at any time, append <code>;RECOVER_TEST=64</code> |
| 1053 |
to the database URL in your test environment. This will simulate an application crash after each 64 writes to the database file. |
| 1054 |
A log file named <code>databaseName.h2.db.log</code> is created that lists the operations. |
| 1055 |
The recovery is tested using an in-memory file system, that means it may require a larger heap setting. |
| 1056 |
</p> |
| 1057 |
|
| 1058 |
<h2 id="file_locking_protocols">File Locking Protocols</h2> |
| 1059 |
<p> |
| 1060 |
Multiple concurrent connections to the same database are supported, however a database file |
| 1061 |
can only be open for reading and writing (in embedded mode) by one process at the same time. |
| 1062 |
Otherwise, the processes would overwrite each others data and corrupt the database file. |
| 1063 |
To protect against this problem, whenever a database is opened, a lock file is created |
| 1064 |
to signal other processes that the database is in use. If the database is closed, or if the process that opened |
| 1065 |
the database stops normally, this lock file is deleted. |
| 1066 |
</p><p> |
| 1067 |
In special cases (if the process did not terminate normally, for example because |
| 1068 |
there was a power failure), the lock file is not deleted by the process that created it. |
| 1069 |
That means the existence of the lock file is not a safe protocol for file locking. |
| 1070 |
However, this software uses a challenge-response protocol to protect the database |
| 1071 |
files. There are two methods (algorithms) implemented to provide both security |
| 1072 |
(that is, the same database files cannot be opened by two processes at the same time) |
| 1073 |
and simplicity (that is, the lock file does not need to be deleted manually by the user). |
| 1074 |
The two methods are 'file method' and 'socket methods'. |
| 1075 |
</p> |
| 1076 |
<p> |
| 1077 |
The file locking protocols (except the file locking method 'FS') |
| 1078 |
have the following limitation: if a shared file system is used, |
| 1079 |
and the machine with the lock owner is sent to sleep (standby or hibernate), |
| 1080 |
another machine may take over. If the machine that originally held the lock |
| 1081 |
wakes up, the database may become corrupt. If this situation can occur, |
| 1082 |
the application must ensure the database is closed when the application |
| 1083 |
is put to sleep. |
| 1084 |
</p> |
| 1085 |
|
| 1086 |
<h3>File Locking Method 'File'</h3> |
| 1087 |
<p> |
| 1088 |
The default method for database file locking for version 1.3 and older is the 'File Method'. |
| 1089 |
The algorithm is: |
| 1090 |
</p> |
| 1091 |
<ul> |
| 1092 |
<li>If the lock file does not exist, it is created (using the atomic operation |
| 1093 |
<code>File.createNewFile</code>). |
| 1094 |
Then, the process waits a little bit (20 ms) and checks the file again. If the file was changed |
| 1095 |
during this time, the operation is aborted. This protects against a race condition |
| 1096 |
when one process deletes the lock file just after another one create it, and a third process creates |
| 1097 |
the file again. It does not occur if there are only two writers. |
| 1098 |
</li><li> |
| 1099 |
If the file can be created, a random number is inserted together with the locking method |
| 1100 |
('file'). Afterwards, a watchdog thread is started that |
| 1101 |
checks regularly (every second once by default) if the file was deleted or modified by |
| 1102 |
another (challenger) thread / process. Whenever that occurs, the file is overwritten with the |
| 1103 |
old data. The watchdog thread runs with high priority so that a change to the lock file does |
| 1104 |
not get through undetected even if the system is very busy. However, the watchdog thread |
| 1105 |
does use very little resources (CPU time), because it waits most of the time. Also, the watchdog only reads from the hard disk |
| 1106 |
and does not write to it. |
| 1107 |
</li><li> |
| 1108 |
If the lock file exists and was recently modified, the process waits for some time (up to two seconds). |
| 1109 |
If it was still changed, an exception is thrown (database is locked). This is done to eliminate race conditions with many concurrent |
| 1110 |
writers. Afterwards, the file is overwritten with a new version (challenge). |
| 1111 |
After that, the thread waits for 2 seconds. |
| 1112 |
If there is a watchdog thread protecting the file, he will overwrite the change |
| 1113 |
and this process will fail to lock the database. |
| 1114 |
However, if there is no watchdog thread, the lock file will still be as written by |
| 1115 |
this thread. In this case, the file is deleted and atomically created again. |
| 1116 |
The watchdog thread is started in this case and the file is locked. |
| 1117 |
</li></ul> |
| 1118 |
<p> |
| 1119 |
This algorithm is tested with over 100 concurrent threads. In some cases, when there are |
| 1120 |
many concurrent threads trying to lock the database, they block each other (meaning |
| 1121 |
the file cannot be locked by any of them) for some time. However, the file never gets |
| 1122 |
locked by two threads at the same time. However using that many concurrent threads |
| 1123 |
/ processes is not the common use case. Generally, an application should throw an error |
| 1124 |
to the user if it cannot open a database, and not try again in a (fast) loop. |
| 1125 |
</p> |
| 1126 |
|
| 1127 |
<h3>File Locking Method 'Socket'</h3> |
| 1128 |
<p> |
| 1129 |
There is a second locking mechanism implemented, but disabled by default. |
| 1130 |
To use it, append <code>;FILE_LOCK=SOCKET</code> to the database URL. |
| 1131 |
The algorithm is: |
| 1132 |
</p> |
| 1133 |
<ul> |
| 1134 |
<li>If the lock file does not exist, it is created. |
| 1135 |
Then a server socket is opened on a defined port, and kept open. |
| 1136 |
The port and IP address of the process that opened the database is written |
| 1137 |
into the lock file. |
| 1138 |
</li><li>If the lock file exists, and the lock method is 'file', then the software switches |
| 1139 |
to the 'file' method. |
| 1140 |
</li><li>If the lock file exists, and the lock method is 'socket', then the process |
| 1141 |
checks if the port is in use. If the original process is still running, the port is in use |
| 1142 |
and this process throws an exception (database is in use). If the original process |
| 1143 |
died (for example due to a power failure, or abnormal termination of the virtual machine), |
| 1144 |
then the port was released. The new process deletes the lock file and starts again. |
| 1145 |
</li></ul> |
| 1146 |
<p> |
| 1147 |
This method does not require a watchdog thread actively polling (reading) the same |
| 1148 |
file every second. The problem with this method is, if the file is stored on a network |
| 1149 |
share, two processes (running on different computers) could still open the same |
| 1150 |
database files, if they do not have a direct TCP/IP connection. |
| 1151 |
</p> |
| 1152 |
|
| 1153 |
<h3>File Locking Method 'FS'</h3> |
| 1154 |
<p> |
| 1155 |
This is the default mode for version 1.4 and newer. |
| 1156 |
This database file locking mechanism uses native file system lock on the database file. |
| 1157 |
No *.lock.db file is created in this case, and no background thread is started. |
| 1158 |
This mechanism may not work on all systems as expected. |
| 1159 |
Some systems allow to lock the same file multiple times within the same virtual machine, |
| 1160 |
and on some system native file locking is not supported or files are not unlocked after a power failure. |
| 1161 |
</p> |
| 1162 |
<p> |
| 1163 |
To enable this feature, append <code>;FILE_LOCK=FS</code> to the database URL. |
| 1164 |
</p> |
| 1165 |
<p> |
| 1166 |
This feature is relatively new. When using it for production, please ensure |
| 1167 |
your system does in fact lock files as expected. |
| 1168 |
</p> |
| 1169 |
|
| 1170 |
<h2 id="passwords">Using Passwords</h2> |
| 1171 |
|
| 1172 |
<h3>Using Secure Passwords</h3> |
| 1173 |
<p> |
| 1174 |
Remember that weak passwords can be broken regardless of the encryption and security protocols. |
| 1175 |
Don't use passwords that can be found in a dictionary. Appending numbers does not make passwords |
| 1176 |
secure. A way to create good passwords that can be remembered is: take the first |
| 1177 |
letters of a sentence, use upper and lower case characters, and creatively include special characters |
| 1178 |
(but it's more important to use a long password than to use special characters). |
| 1179 |
Example: |
| 1180 |
</p><p> |
| 1181 |
<code>i'sE2rtPiUKtT</code> from the sentence <code>it's easy to remember this password if you know the trick</code>. |
| 1182 |
</p> |
| 1183 |
|
| 1184 |
<h3>Passwords: Using Char Arrays instead of Strings</h3> |
| 1185 |
<p> |
| 1186 |
Java strings are immutable objects and cannot be safely 'destroyed' by the application. |
| 1187 |
After creating a string, it will remain in the main memory of the computer at least |
| 1188 |
until it is garbage collected. The garbage collection cannot be controlled by the application, |
| 1189 |
and even if it is garbage collected the data may still remain in memory. |
| 1190 |
It might also be possible that the part of memory containing the password |
| 1191 |
is swapped to disk (if not enough main memory is available), which is |
| 1192 |
a problem if the attacker has access to the swap file of the operating system. |
| 1193 |
</p><p> |
| 1194 |
It is a good idea to use char arrays instead of strings for passwords. |
| 1195 |
Char arrays can be cleared (filled with zeros) after use, and therefore the |
| 1196 |
password will not be stored in the swap file. |
| 1197 |
</p><p> |
| 1198 |
This database supports using char arrays instead of string to pass user and file passwords. |
| 1199 |
The following code can be used to do that: |
| 1200 |
</p> |
| 1201 |
<pre> |
| 1202 |
import java.sql.*; |
| 1203 |
import java.util.*; |
| 1204 |
public class Test { |
| 1205 |
public static void main(String[] args) throws Exception { |
| 1206 |
String url = "jdbc:h2:~/test"; |
| 1207 |
Properties prop = new Properties(); |
| 1208 |
prop.setProperty("user", "sa"); |
| 1209 |
System.out.print("Password?"); |
| 1210 |
char[] password = System.console().readPassword(); |
| 1211 |
prop.put("password", password); |
| 1212 |
Connection conn = null; |
| 1213 |
try { |
| 1214 |
conn = DriverManager.getConnection(url, prop); |
| 1215 |
} finally { |
| 1216 |
Arrays.fill(password, (char) 0); |
| 1217 |
} |
| 1218 |
conn.close(); |
| 1219 |
} |
| 1220 |
} |
| 1221 |
</pre> |
| 1222 |
<p> |
| 1223 |
When using Swing, use <code>javax.swing.JPasswordField</code>. |
| 1224 |
</p> |
| 1225 |
|
| 1226 |
<h3>Passing the User Name and/or Password in the URL</h3> |
| 1227 |
<p> |
| 1228 |
Instead of passing the user name as a separate parameter as in |
| 1229 |
<code> |
| 1230 |
Connection conn = DriverManager. |
| 1231 |
getConnection("jdbc:h2:~/test", "sa", "123"); |
| 1232 |
</code> |
| 1233 |
the user name (and/or password) can be supplied in the URL itself: |
| 1234 |
<code> |
| 1235 |
Connection conn = DriverManager. |
| 1236 |
getConnection("jdbc:h2:~/test;USER=sa;PASSWORD=123"); |
| 1237 |
</code> |
| 1238 |
The settings in the URL override the settings passed as a separate parameter. |
| 1239 |
</p> |
| 1240 |
|
| 1241 |
<h2 id="password_hash">Password Hash</h2> |
| 1242 |
<p> |
| 1243 |
Sometimes the database password needs to be stored in a configuration file |
| 1244 |
(for example in the <code>web.xml</code> file). |
| 1245 |
In addition to connecting with the plain text password, |
| 1246 |
this database supports connecting with the password hash. |
| 1247 |
This means that only the hash of the password (and not the plain text password) |
| 1248 |
needs to be stored in the configuration file. |
| 1249 |
This will only protect others from reading or re-constructing the plain text password |
| 1250 |
(even if they have access to the configuration file); |
| 1251 |
it does not protect others from accessing the database using the password hash. |
| 1252 |
</p> |
| 1253 |
<p> |
| 1254 |
To connect using the password hash instead of plain text password, append |
| 1255 |
<code>;PASSWORD_HASH=TRUE</code> to the database URL, and replace |
| 1256 |
the password with the password hash. To calculate the password hash from a plain text password, |
| 1257 |
run the following command within the H2 Console tool: |
| 1258 |
<code>@password_hash <upperCaseUserName> <password></code>. |
| 1259 |
As an example, if the user name is <code>sa</code> and the password is |
| 1260 |
<code>test</code>, run the command |
| 1261 |
<code>@password_hash SA test</code>. |
| 1262 |
Then use the resulting password hash as you would use the plain text password. |
| 1263 |
When using an encrypted database, then the user password and file password |
| 1264 |
need to be hashed separately. To calculate the hash of the file password, run: |
| 1265 |
<code>@password_hash file <filePassword></code>. |
| 1266 |
</p> |
| 1267 |
|
| 1268 |
<h2 id="sql_injection">Protection against SQL Injection</h2> |
| 1269 |
<h3>What is SQL Injection</h3> |
| 1270 |
<p> |
| 1271 |
This database engine provides a solution for the security vulnerability known as 'SQL Injection'. |
| 1272 |
Here is a short description of what SQL injection means. |
| 1273 |
Some applications build SQL statements with embedded user input such as: |
| 1274 |
</p> |
| 1275 |
<pre> |
| 1276 |
String sql = "SELECT * FROM USERS WHERE PASSWORD='"+pwd+"'"; |
| 1277 |
ResultSet rs = conn.createStatement().executeQuery(sql); |
| 1278 |
</pre> |
| 1279 |
<p> |
| 1280 |
If this mechanism is used anywhere in the application, and user input is not correctly filtered or encoded, |
| 1281 |
it is possible for a user to inject SQL functionality or statements by using specially built input |
| 1282 |
such as (in this example) this password: <code>' OR ''='</code>. |
| 1283 |
In this case the statement becomes: |
| 1284 |
</p> |
| 1285 |
<pre> |
| 1286 |
SELECT * FROM USERS WHERE PASSWORD='' OR ''=''; |
| 1287 |
</pre> |
| 1288 |
<p> |
| 1289 |
Which is always true no matter what the password stored in the database is. |
| 1290 |
For more information about SQL Injection, see <a href="#glossary_links">Glossary and Links</a>. |
| 1291 |
</p> |
| 1292 |
|
| 1293 |
<h3>Disabling Literals</h3> |
| 1294 |
<p> |
| 1295 |
SQL Injection is not possible if user input is not directly embedded in SQL statements. |
| 1296 |
A simple solution for the problem above is to use a prepared statement: |
| 1297 |
</p> |
| 1298 |
<pre> |
| 1299 |
String sql = "SELECT * FROM USERS WHERE PASSWORD=?"; |
| 1300 |
PreparedStatement prep = conn.prepareStatement(sql); |
| 1301 |
prep.setString(1, pwd); |
| 1302 |
ResultSet rs = prep.executeQuery(); |
| 1303 |
</pre> |
| 1304 |
<p> |
| 1305 |
This database provides a way to enforce usage of parameters when passing user input |
| 1306 |
to the database. This is done by disabling embedded literals in SQL statements. |
| 1307 |
To do this, execute the statement: |
| 1308 |
</p> |
| 1309 |
<pre> |
| 1310 |
SET ALLOW_LITERALS NONE; |
| 1311 |
</pre> |
| 1312 |
<p> |
| 1313 |
Afterwards, SQL statements with text and number literals are not allowed any more. |
| 1314 |
That means, SQL statement of the form <code>WHERE NAME='abc'</code> |
| 1315 |
or <code>WHERE CustomerId=10</code> will fail. |
| 1316 |
It is still possible to use prepared statements and parameters as described above. Also, it is still possible to generate |
| 1317 |
SQL statements dynamically, and use the Statement API, as long as the SQL statements |
| 1318 |
do not include literals. |
| 1319 |
There is also a second mode where number literals are allowed: |
| 1320 |
<code>SET ALLOW_LITERALS NUMBERS</code>. |
| 1321 |
To allow all literals, execute <code>SET ALLOW_LITERALS ALL</code> |
| 1322 |
(this is the default setting). Literals can only be enabled or disabled by an administrator. |
| 1323 |
</p> |
| 1324 |
|
| 1325 |
<h3>Using Constants</h3> |
| 1326 |
<p> |
| 1327 |
Disabling literals also means disabling hard-coded 'constant' literals. This database supports |
| 1328 |
defining constants using the <code>CREATE CONSTANT</code> command. |
| 1329 |
Constants can be defined only |
| 1330 |
when literals are enabled, but used even when literals are disabled. To avoid name clashes |
| 1331 |
with column names, constants can be defined in other schemas: |
| 1332 |
</p> |
| 1333 |
<pre> |
| 1334 |
CREATE SCHEMA CONST AUTHORIZATION SA; |
| 1335 |
CREATE CONSTANT CONST.ACTIVE VALUE 'Active'; |
| 1336 |
CREATE CONSTANT CONST.INACTIVE VALUE 'Inactive'; |
| 1337 |
SELECT * FROM USERS WHERE TYPE=CONST.ACTIVE; |
| 1338 |
</pre> |
| 1339 |
<p> |
| 1340 |
Even when literals are enabled, it is better to use constants instead |
| 1341 |
of hard-coded number or text literals in queries or views. With constants, typos are found at compile |
| 1342 |
time, the source code is easier to understand and change. |
| 1343 |
</p> |
| 1344 |
|
| 1345 |
<h3>Using the ZERO() Function</h3> |
| 1346 |
<p> |
| 1347 |
It is not required to create a constant for the number 0 as there is already a built-in function <code>ZERO()</code>: |
| 1348 |
</p> |
| 1349 |
<pre> |
| 1350 |
SELECT * FROM USERS WHERE LENGTH(PASSWORD)=ZERO(); |
| 1351 |
</pre> |
| 1352 |
|
| 1353 |
<h2 id="remote_access">Protection against Remote Access</h2> |
| 1354 |
<p> |
| 1355 |
By default this database does not allow connections from other machines when starting the H2 Console, |
| 1356 |
the TCP server, or the PG server. Remote access can be enabled using the command line |
| 1357 |
options <code>-webAllowOthers, -tcpAllowOthers, -pgAllowOthers</code>. |
| 1358 |
</p> |
| 1359 |
<p> |
| 1360 |
If you enable remote access using |
| 1361 |
<code>-tcpAllowOthers</code> or <code>-pgAllowOthers</code>, |
| 1362 |
please also consider using the options <code>-baseDir</code>, |
| 1363 |
so that remote users can not create new databases |
| 1364 |
or access existing databases with weak passwords. |
| 1365 |
When using the option <code>-baseDir</code>, only databases within that directory may be accessed. |
| 1366 |
Ensure the existing accessible databases are protected using strong passwords. |
| 1367 |
</p> |
| 1368 |
<p> |
| 1369 |
If you enable remote access using <code>-webAllowOthers</code>, |
| 1370 |
please ensure the web server can only be accessed from trusted networks. |
| 1371 |
If this option is specified, <code>-webExternalNames</code> should be also specified with |
| 1372 |
comma-separated list of external names or addresses of this server. |
| 1373 |
The options <code>-baseDir</code> don't protect |
| 1374 |
access to the saved connection settings, |
| 1375 |
or access to other databases accessible from the system. |
| 1376 |
</p> |
| 1377 |
|
| 1378 |
<h2 id="restricting_classes">Restricting Class Loading and Usage</h2> |
| 1379 |
<p> |
| 1380 |
By default there is no restriction on loading classes and executing Java code for admins. |
| 1381 |
That means an admin may call system functions such as |
| 1382 |
<code>System.setProperty</code> by executing: |
| 1383 |
</p> |
| 1384 |
<pre> |
| 1385 |
CREATE ALIAS SET_PROPERTY FOR "java.lang.System.setProperty"; |
| 1386 |
CALL SET_PROPERTY('abc', '1'); |
| 1387 |
CREATE ALIAS GET_PROPERTY FOR "java.lang.System.getProperty"; |
| 1388 |
CALL GET_PROPERTY('abc'); |
| 1389 |
</pre> |
| 1390 |
<p> |
| 1391 |
To restrict users (including admins) from loading classes and executing code, |
| 1392 |
the list of allowed classes can be set in the system property |
| 1393 |
<code>h2.allowedClasses</code> |
| 1394 |
in the form of a comma separated list of classes or patterns (items ending with <code>*</code>). |
| 1395 |
By default all classes are allowed. Example: |
| 1396 |
</p> |
| 1397 |
<pre> |
| 1398 |
java -Dh2.allowedClasses=java.lang.Math,com.acme.* |
| 1399 |
</pre> |
| 1400 |
<p> |
| 1401 |
This mechanism is used for all user classes, including database event listeners, |
| 1402 |
trigger classes, user-defined functions, user-defined aggregate functions, and JDBC |
| 1403 |
driver classes (with the exception of the H2 driver) when using the H2 Console. |
| 1404 |
</p> |
| 1405 |
|
| 1406 |
<h2 id="security_protocols">Security Protocols</h2> |
| 1407 |
<p> |
| 1408 |
The following paragraphs document the security protocols used in this database. |
| 1409 |
These descriptions are very technical and only intended for security experts that already know |
| 1410 |
the underlying security primitives. |
| 1411 |
</p> |
| 1412 |
|
| 1413 |
<h3>User Password Encryption</h3> |
| 1414 |
<p> |
| 1415 |
When a user tries to connect to a database, the combination of |
| 1416 |
user name, @, and password are hashed using SHA-256, and this hash value |
| 1417 |
is transmitted to the database. |
| 1418 |
This step does not protect against an attacker that re-uses the value if he is able to listen to the |
| 1419 |
(unencrypted) transmission between the client and the server. |
| 1420 |
But, the passwords are never transmitted as plain text, |
| 1421 |
even when using an unencrypted connection between client and server. |
| 1422 |
That means if a user reuses the same password for different things, |
| 1423 |
this password is still protected up to some point. See also |
| 1424 |
'RFC 2617 - HTTP Authentication: Basic and Digest Access Authentication' |
| 1425 |
for more information. |
| 1426 |
</p><p> |
| 1427 |
When a new database or user is created, a new random salt value is generated. |
| 1428 |
The size of the salt is 64 bits. Using the random salt reduces the risk of an |
| 1429 |
attacker pre-calculating hash values for many different (commonly used) passwords. |
| 1430 |
</p><p> |
| 1431 |
The combination of user-password hash value (see above) and salt is hashed |
| 1432 |
using SHA-256. The resulting value is stored in the database. |
| 1433 |
When a user tries to connect to the database, the database combines |
| 1434 |
user-password hash value with the stored salt value and calculates the |
| 1435 |
hash value. Other products use multiple iterations (hash the hash value again and again), |
| 1436 |
but this is not done in this product to reduce the risk of denial of service attacks |
| 1437 |
(where the attacker tries to connect with bogus passwords, and the server |
| 1438 |
spends a lot of time calculating the hash value for each password). |
| 1439 |
The reasoning is: if the attacker has access to the hashed passwords, he also has |
| 1440 |
access to the data in plain text, and therefore does not need the password any more. |
| 1441 |
If the data is protected by storing it on another computer and only accessible remotely, |
| 1442 |
then the iteration count is not required at all. |
| 1443 |
</p> |
| 1444 |
|
| 1445 |
<h3>File Encryption</h3> |
| 1446 |
<p> |
| 1447 |
The database files can be encrypted using the AES-128 algorithm. |
| 1448 |
</p><p> |
| 1449 |
When a user tries to connect to an encrypted database, the combination of |
| 1450 |
<code>file@</code> and the file password is hashed using SHA-256. This hash value is |
| 1451 |
transmitted to the server. |
| 1452 |
</p><p> |
| 1453 |
When a new database file is created, a new cryptographically secure |
| 1454 |
random salt value is generated. The size of the salt is 64 bits. |
| 1455 |
The combination of the file password hash and the salt value is hashed 1024 times |
| 1456 |
using SHA-256. The reason for the iteration is to make it harder for an attacker to |
| 1457 |
calculate hash values for common passwords. |
| 1458 |
</p><p> |
| 1459 |
The resulting hash value is used as the key for the block cipher algorithm. |
| 1460 |
Then, an initialization vector (IV) key |
| 1461 |
is calculated by hashing the key again using SHA-256. |
| 1462 |
This is to make sure the IV is unknown to the attacker. |
| 1463 |
The reason for using a secret IV is to protect against watermark attacks. |
| 1464 |
</p><p> |
| 1465 |
Before saving a block of data (each block is 8 bytes long), the following operations are executed: |
| 1466 |
first, the IV is calculated by encrypting the block number with the IV key (using the same |
| 1467 |
block cipher algorithm). This IV is combined with the plain text using XOR. The resulting data is |
| 1468 |
encrypted using the AES-128 algorithm. |
| 1469 |
</p><p> |
| 1470 |
When decrypting, the operation is done in reverse. First, the block is decrypted using the key, |
| 1471 |
and then the IV is calculated combined with the decrypted text using XOR. |
| 1472 |
</p><p> |
| 1473 |
Therefore, the block cipher mode of operation is CBC (cipher-block chaining), but each chain |
| 1474 |
is only one block long. The advantage over the ECB (electronic codebook) mode is that patterns |
| 1475 |
in the data are not revealed, and the advantage over multi block CBC is that flipped cipher text bits |
| 1476 |
are not propagated to flipped plaintext bits in the next block. |
| 1477 |
</p><p> |
| 1478 |
Database encryption is meant for securing the database while it is not in use (stolen laptop and so on). |
| 1479 |
It is not meant for cases where the attacker has access to files while the database is in use. |
| 1480 |
When he has write access, he can for example replace pieces of files with pieces of older versions |
| 1481 |
and manipulate data like this. |
| 1482 |
</p><p> |
| 1483 |
File encryption slows down the performance of the database engine. Compared to unencrypted mode, |
| 1484 |
database operations take about 2.5 times longer using AES (embedded mode). |
| 1485 |
</p> |
| 1486 |
|
| 1487 |
<h3>Wrong Password / User Name Delay</h3> |
| 1488 |
<p> |
| 1489 |
To protect against remote brute force password attacks, the delay after each unsuccessful |
| 1490 |
login gets double as long. Use the system properties <code>h2.delayWrongPasswordMin</code> |
| 1491 |
and <code>h2.delayWrongPasswordMax</code> to change the minimum (the default is 250 milliseconds) |
| 1492 |
or maximum delay (the default is 4000 milliseconds, or 4 seconds). The delay only |
| 1493 |
applies for those using the wrong password. Normally there is no delay for a user that knows the correct |
| 1494 |
password, with one exception: after using the wrong password, there is a delay of up to (randomly distributed) |
| 1495 |
the same delay as for a wrong password. This is to protect against parallel brute force attacks, |
| 1496 |
so that an attacker needs to wait for the whole delay. Delays are synchronized. This is also required |
| 1497 |
to protect against parallel attacks. |
| 1498 |
</p> |
| 1499 |
<p> |
| 1500 |
There is only one exception message for both wrong user and for wrong password, |
| 1501 |
to make it harder to get the list of user names. It is not possible from the stack trace to see |
| 1502 |
if the user name was wrong or the password. |
| 1503 |
</p> |
| 1504 |
|
| 1505 |
<h3>HTTPS Connections</h3> |
| 1506 |
<p> |
| 1507 |
The web server supports HTTP and HTTPS connections using <code>SSLServerSocket</code>. |
| 1508 |
There is a default self-certified certificate to support an easy starting point, but |
| 1509 |
custom certificates are supported as well. |
| 1510 |
</p> |
| 1511 |
|
| 1512 |
<h2 id="tls_connections">TLS Connections</h2> |
| 1513 |
<p> |
| 1514 |
Remote TLS connections are supported using the Java Secure Socket Extension |
| 1515 |
(<code>SSLServerSocket, SSLSocket</code>). By default, anonymous TLS is enabled. |
| 1516 |
</p> |
| 1517 |
<p> |
| 1518 |
To use your own keystore, set the system properties <code>javax.net.ssl.keyStore</code> and |
| 1519 |
<code>javax.net.ssl.keyStorePassword</code> before starting the H2 server and client. |
| 1520 |
See also <a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/JSSERefGuide.html#CustomizingStores"> |
| 1521 |
Customizing the Default Key and Trust Stores, Store Types, and Store Passwords</a> |
| 1522 |
for more information. |
| 1523 |
</p> |
| 1524 |
<p> |
| 1525 |
To disable anonymous TLS, set the system property <code>h2.enableAnonymousTLS</code> to false. |
| 1526 |
</p> |
| 1527 |
|
| 1528 |
<h2 id="uuid">Universally Unique Identifiers (UUID)</h2> |
| 1529 |
<p> |
| 1530 |
This database supports UUIDs. Also supported is a function to create new UUIDs using |
| 1531 |
a cryptographically strong pseudo random number generator. |
| 1532 |
With random UUIDs, the chance of two having the same value can be calculated |
| 1533 |
using the probability theory. See also 'Birthday Paradox'. |
| 1534 |
Standardized randomly generated UUIDs have 122 random bits. |
| 1535 |
4 bits are used for the version (Randomly generated UUID), and 2 bits for the variant (Leach-Salz). |
| 1536 |
This database supports generating such UUIDs using the built-in function |
| 1537 |
<code>RANDOM_UUID()</code> or <code>UUID()</code>. |
| 1538 |
Here is a small program to estimate the probability of having two identical UUIDs |
| 1539 |
after generating a number of values: |
| 1540 |
</p> |
| 1541 |
<pre> |
| 1542 |
public class Test { |
| 1543 |
public static void main(String[] args) throws Exception { |
| 1544 |
double x = Math.pow(2, 122); |
| 1545 |
for (int i = 35; i < 62; i++) { |
| 1546 |
double n = Math.pow(2, i); |
| 1547 |
double p = 1 - Math.exp(-(n * n) / 2 / x); |
| 1548 |
System.out.println("2^" + i + "=" + (1L << i) + |
| 1549 |
" probability: 0" + |
| 1550 |
String.valueOf(1 + p).substring(1)); |
| 1551 |
} |
| 1552 |
} |
| 1553 |
} |
| 1554 |
</pre> |
| 1555 |
<p> |
| 1556 |
Some values are: |
| 1557 |
</p> |
| 1558 |
<table class="main"> |
| 1559 |
<tr><th>Number of UUIs</th><th>Probability of Duplicates</th></tr> |
| 1560 |
<tr><td>2^36=68'719'476'736</td><td>0.000'000'000'000'000'4</td></tr> |
| 1561 |
<tr><td>2^41=2'199'023'255'552</td><td>0.000'000'000'000'4</td></tr> |
| 1562 |
<tr><td>2^46=70'368'744'177'664</td><td>0.000'000'000'4</td></tr> |
| 1563 |
</table> |
| 1564 |
<p> |
| 1565 |
To help non-mathematicians understand what those numbers mean, here a comparison: |
| 1566 |
one's annual risk of being hit by a meteorite is estimated to be one chance in 17 billion, |
| 1567 |
that means the probability is about 0.000'000'000'06. |
| 1568 |
</p> |
| 1569 |
|
| 1570 |
<h2 id="spatial_features">Spatial Features</h2> |
| 1571 |
<p> |
| 1572 |
H2 supports the geometry data type and spatial indexes. |
| 1573 |
Here is an example SQL script to create a table with a spatial column and index: |
| 1574 |
</p> |
| 1575 |
<pre> |
| 1576 |
CREATE TABLE GEO_TABLE( |
| 1577 |
GID BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, |
| 1578 |
THE_GEOM GEOMETRY); |
| 1579 |
INSERT INTO GEO_TABLE(THE_GEOM) VALUES |
| 1580 |
('POINT(500 505)'), |
| 1581 |
('LINESTRING(550 551, 525 512, 565 566)'), |
| 1582 |
('POLYGON ((550 521, 580 540, 570 564, 512 566, 550 521))'); |
| 1583 |
CREATE SPATIAL INDEX GEO_TABLE_SPATIAL_INDEX |
| 1584 |
ON GEO_TABLE(THE_GEOM); |
| 1585 |
</pre> |
| 1586 |
<p> |
| 1587 |
To query the table using geometry envelope intersection, |
| 1588 |
use the operation <code>&&</code>, as in PostGIS: |
| 1589 |
</p> |
| 1590 |
<pre> |
| 1591 |
SELECT * FROM GEO_TABLE |
| 1592 |
WHERE THE_GEOM && |
| 1593 |
'POLYGON ((490 490, 536 490, 536 515, 490 515, 490 490))'; |
| 1594 |
</pre> |
| 1595 |
<p> |
| 1596 |
You can verify that the spatial index is used using the "explain plan" feature: |
| 1597 |
</p> |
| 1598 |
<pre> |
| 1599 |
EXPLAIN SELECT * FROM GEO_TABLE |
| 1600 |
WHERE THE_GEOM && |
| 1601 |
'POLYGON ((490 490, 536 490, 536 515, 490 515, 490 490))'; |
| 1602 |
-- Result |
| 1603 |
SELECT |
| 1604 |
"PUBLIC"."GEO_TABLE"."GID", |
| 1605 |
"PUBLIC"."GEO_TABLE"."THE_GEOM" |
| 1606 |
FROM "PUBLIC"."GEO_TABLE" |
| 1607 |
/* PUBLIC.GEO_TABLE_SPATIAL_INDEX: THE_GEOM && |
| 1608 |
GEOMETRY 'POLYGON ((490 490, 536 490, 536 515, 490 515, 490 490))' */ |
| 1609 |
WHERE "THE_GEOM" && |
| 1610 |
GEOMETRY 'POLYGON ((490 490, 536 490, 536 515, 490 515, 490 490))' |
| 1611 |
</pre> |
| 1612 |
<p> |
| 1613 |
For persistent databases, the spatial index is stored on disk; |
| 1614 |
for in-memory databases, the index is kept in memory. |
| 1615 |
</p> |
| 1616 |
|
| 1617 |
<h2 id="recursive_queries">Recursive Queries</h2> |
| 1618 |
<p> |
| 1619 |
H2 has experimental support for recursive queries using so called "common table expressions" (CTE). |
| 1620 |
Examples: |
| 1621 |
</p> |
| 1622 |
<pre> |
| 1623 |
WITH RECURSIVE T(N) AS ( |
| 1624 |
SELECT 1 |
| 1625 |
UNION ALL |
| 1626 |
SELECT N+1 FROM T WHERE N<10 |
| 1627 |
) |
| 1628 |
SELECT * FROM T; |
| 1629 |
-- returns the values 1 .. 10 |
| 1630 |
|
| 1631 |
WITH RECURSIVE T(N) AS ( |
| 1632 |
SELECT 1 |
| 1633 |
UNION ALL |
| 1634 |
SELECT N*2 FROM T WHERE N<10 |
| 1635 |
) |
| 1636 |
SELECT * FROM T; |
| 1637 |
-- returns the values 1, 2, 4, 8, 16 |
| 1638 |
|
| 1639 |
CREATE TABLE FOLDER(ID INT PRIMARY KEY, NAME VARCHAR(255), PARENT INT); |
| 1640 |
|
| 1641 |
INSERT INTO FOLDER VALUES(1, null, null), (2, 'src', 1), |
| 1642 |
(3, 'main', 2), (4, 'org', 3), (5, 'test', 2); |
| 1643 |
|
| 1644 |
WITH LINK(ID, NAME, LEVEL) AS ( |
| 1645 |
SELECT ID, NAME, 0 FROM FOLDER WHERE PARENT IS NULL |
| 1646 |
UNION ALL |
| 1647 |
SELECT FOLDER.ID, COALESCE(LINK.NAME || '/', '') || FOLDER.NAME, LEVEL + 1 |
| 1648 |
FROM LINK INNER JOIN FOLDER ON LINK.ID = FOLDER.PARENT |
| 1649 |
) |
| 1650 |
SELECT NAME FROM LINK WHERE NAME IS NOT NULL ORDER BY ID; |
| 1651 |
-- src |
| 1652 |
-- src/main |
| 1653 |
-- src/main/org |
| 1654 |
-- src/test |
| 1655 |
</pre> |
| 1656 |
<p> |
| 1657 |
Limitations: Recursive queries need to be of the type <code>UNION ALL</code>, |
| 1658 |
and the recursion needs to be on the second part of the query. |
| 1659 |
No tables or views with the name of the table expression may exist. |
| 1660 |
Different table expression names need to be used when using multiple distinct table |
| 1661 |
expressions within the same transaction and for the same session. |
| 1662 |
All columns of the table expression are of type <code>VARCHAR</code>, |
| 1663 |
and may need to be cast to the required data type. |
| 1664 |
Views with recursive queries are not supported. |
| 1665 |
Subqueries and <code>INSERT INTO ... FROM</code> with recursive queries are not supported. |
| 1666 |
Parameters are only supported within the last <code>SELECT</code> statement |
| 1667 |
(a workaround is to use session variables like <code>@start</code> |
| 1668 |
within the table expression). |
| 1669 |
The syntax is: |
| 1670 |
</p> |
| 1671 |
<pre> |
| 1672 |
WITH RECURSIVE recursiveQueryName(columnName, ...) AS ( |
| 1673 |
nonRecursiveSelect |
| 1674 |
UNION ALL |
| 1675 |
recursiveSelect |
| 1676 |
) |
| 1677 |
select |
| 1678 |
</pre> |
| 1679 |
|
| 1680 |
<h2 id="system_properties">Settings Read from System Properties</h2> |
| 1681 |
<p> |
| 1682 |
Some settings of the database can be set on the command line using |
| 1683 |
<code>-DpropertyName=value</code>. It is usually not required to change those settings manually. |
| 1684 |
The settings are case sensitive. |
| 1685 |
Example: |
| 1686 |
</p> |
| 1687 |
<pre> |
| 1688 |
java -Dh2.serverCachedObjects=256 org.h2.tools.Server |
| 1689 |
</pre> |
| 1690 |
<p> |
| 1691 |
The current value of the settings can be read in the table |
| 1692 |
<code>INFORMATION_SCHEMA.SETTINGS</code>. |
| 1693 |
</p> |
| 1694 |
<p> |
| 1695 |
For a complete list of settings, see |
| 1696 |
<a href="https://h2database.com/javadoc/org/h2/engine/SysProperties.html">SysProperties</a>. |
| 1697 |
</p> |
| 1698 |
|
| 1699 |
<h2 id="server_bind_address">Setting the Server Bind Address</h2> |
| 1700 |
<p> |
| 1701 |
Usually server sockets accept connections on any/all local addresses. |
| 1702 |
This may be a problem on multi-homed hosts. |
| 1703 |
To bind only to one address, use the system property <code>h2.bindAddress</code>. |
| 1704 |
This setting is used for both regular server sockets and for TLS server sockets. |
| 1705 |
IPv4 and IPv6 address formats are supported. |
| 1706 |
</p> |
| 1707 |
|
| 1708 |
<h2 id="file_system">Pluggable File System</h2> |
| 1709 |
<p> |
| 1710 |
This database supports a pluggable file system API. |
| 1711 |
The file system implementation is selected using a file name prefix. |
| 1712 |
Internally, the interfaces are very similar to the Java 7 NIO2 API. |
| 1713 |
The following file systems are included: |
| 1714 |
</p> |
| 1715 |
<ul><li><code>file:</code> the default file system that uses <code>FileChannel</code>. |
| 1716 |
</li><li><code>zip:</code> read-only zip-file based file system. Format: <code>zip:~/zipFileName!/fileName</code>. |
| 1717 |
</li><li><code>split:</code> file system that splits files in 1 GB files (stackable with other file systems). |
| 1718 |
</li><li><code>nioMapped:</code> file system that uses memory mapped files (faster in some operating systems). |
| 1719 |
Please note that there currently is a file size limitation of 2 GB when using this file system. |
| 1720 |
To work around this limitation, combine it with the split file system: <code>split:nioMapped:~/test</code>. |
| 1721 |
</li><li><code>async:</code> experimental file system that uses <code>AsynchronousFileChannel</code> instead of <code>FileChannel</code> (faster in some operating systems). |
| 1722 |
</li><li><code>memFS:</code> in-memory file system (slower than mem; experimental; mainly used for testing the database engine itself). |
| 1723 |
</li><li><code>memLZF:</code> compressing in-memory file system (slower than memFS but uses less memory; experimental; mainly used for testing the database engine itself). |
| 1724 |
</li><li><code>nioMemFS:</code> stores data outside of the VM's heap - useful for large memory DBs without incurring GC costs. |
| 1725 |
</li> |
| 1726 |
<li> |
| 1727 |
<code>nioMemLZF:</code> stores compressed data outside of the VM's heap - |
| 1728 |
useful for large memory DBs without incurring GC costs. |
| 1729 |
Use "nioMemLZF:12:" to tweak the % of blocks that are stored uncompressed. |
| 1730 |
If you size this to your working set correctly, |
| 1731 |
compressed storage is roughly the same performance as uncompressed. |
| 1732 |
The default value is 1%. |
| 1733 |
</li></ul> |
| 1734 |
<p> |
| 1735 |
As an example, to use the <code>async:</code> file system |
| 1736 |
use the following database URL: <code>jdbc:h2:async:~/test</code>. |
| 1737 |
</p> |
| 1738 |
<p> |
| 1739 |
To register a new file system, extend the classes <code>org.h2.store.fs.FilePath, FileBase</code>, |
| 1740 |
and call the method <code>FilePath.register</code> before using it. |
| 1741 |
</p> |
| 1742 |
<p> |
| 1743 |
For input streams (but not for random access files), URLs may be used in addition to the registered file systems. |
| 1744 |
Example: <code>jar:file:///c:/temp/example.zip!/org/example/nested.csv</code>. |
| 1745 |
To read a stream from the classpath, use the prefix <code>classpath:</code>, as in |
| 1746 |
<code>classpath:/org/h2/samples/newsfeed.sql</code>. |
| 1747 |
</p> |
| 1748 |
|
| 1749 |
<h2 id="file_system_split">Split File System</h2> |
| 1750 |
<p> |
| 1751 |
The file system prefix <code>split:</code> is used to split logical files into multiple physical files, |
| 1752 |
for example so that a database can get larger than the maximum file system size of the operating system. |
| 1753 |
If the logical file is larger than the maximum file size, then the file is split as follows: |
| 1754 |
</p> |
| 1755 |
<ul><li><code><fileName></code> (first block, is always created) |
| 1756 |
</li><li><code><fileName>.1.part</code> (second block) |
| 1757 |
</li></ul> |
| 1758 |
<p> |
| 1759 |
More physical files (<code>*.2.part, *.3.part</code>) are automatically created / deleted if needed. |
| 1760 |
The maximum physical file size of a block is 2^30 bytes, which is also called 1 GiB or 1 GB. |
| 1761 |
However this can be changed if required, by specifying the block size in the file name. |
| 1762 |
The file name format is: <code>split:<x>:<fileName></code> where the file size per block is 2^x. |
| 1763 |
For 1 MiB block sizes, use x = 20 (because 2^20 is 1 MiB). |
| 1764 |
The following file name means the logical file is split into 1 MiB blocks: <code>split:20:~/test.h2.db</code>. |
| 1765 |
An example database URL for this case is <code>jdbc:h2:split:20:~/test</code>. |
| 1766 |
</p> |
| 1767 |
|
| 1768 |
<h2 id="java_objects_serialization">Java Objects Serialization</h2> |
| 1769 |
<p> |
| 1770 |
Java objects serialization is enabled by default for columns of type <code>OTHER</code>, using standard Java serialization/deserialization semantics. |
| 1771 |
</p> |
| 1772 |
<p> |
| 1773 |
To disable this feature set the system property <code>h2.serializeJavaObject=false</code> (default: true). |
| 1774 |
</p> |
| 1775 |
<p> |
| 1776 |
Serialization and deserialization of java objects is customizable both at system level and at database level providing a |
| 1777 |
<a href="https://h2database.com/javadoc/org/h2/api/JavaObjectSerializer.html">JavaObjectSerializer</a> implementation: |
| 1778 |
</p> |
| 1779 |
<ul> |
| 1780 |
<li> |
| 1781 |
At system level set the system property <code>h2.javaObjectSerializer</code> with the |
| 1782 |
Fully Qualified Name of the <code>JavaObjectSerializer</code> interface implementation. |
| 1783 |
It will be used over the entire JVM session to (de)serialize java objects being stored in column of type OTHER. |
| 1784 |
Example <code>h2.javaObjectSerializer=com.acme.SerializerClassName</code>. |
| 1785 |
</li> |
| 1786 |
<li> |
| 1787 |
At database level execute the SQL statement <code>SET JAVA_OBJECT_SERIALIZER 'com.acme.SerializerClassName'</code> |
| 1788 |
or append <code>;JAVA_OBJECT_SERIALIZER='com.acme.SerializerClassName'</code> to the database URL: <code>jdbc:h2:~/test;JAVA_OBJECT_SERIALIZER='com.acme.SerializerClassName'</code>. |
| 1789 |
<p> |
| 1790 |
Please note that this SQL statement can only be executed before any tables are defined. |
| 1791 |
</p> |
| 1792 |
</li> |
| 1793 |
</ul> |
| 1794 |
|
| 1795 |
<h2 id="limits_limitations">Limits and Limitations</h2> |
| 1796 |
<p> |
| 1797 |
This database has the following known limitations: |
| 1798 |
</p> |
| 1799 |
<ul> |
| 1800 |
<li>Database file size limit: |
| 1801 |
4 TB (using the default page size of 2 KB) or higher (when using a larger page size). |
| 1802 |
This limit is including CLOB and BLOB data. |
| 1803 |
</li><li>The maximum file size for FAT or FAT32 file systems is 4 GB. |
| 1804 |
That means when using FAT or FAT32, the limit is 4 GB for the data. This is the limitation of the file system. |
| 1805 |
The database does provide a workaround for this problem, it is to use the file name prefix <code>split:</code>. |
| 1806 |
In that case files are split into files of 1 GB by default. |
| 1807 |
An example database URL is: <code>jdbc:h2:split:~/test</code>. |
| 1808 |
</li><li>The maximum number of rows per table is 2^64. |
| 1809 |
</li><li>The maximum number of open transactions is 65535. |
| 1810 |
</li><li>The maximum number of columns in a table or expressions in a SELECT statement is 16384. |
| 1811 |
The actual possible number can be smaller if their definitions are too long. |
| 1812 |
</li><li>The maximum length of an identifier (table name, column name, and so on) is 256 characters. |
| 1813 |
</li><li>The maximum length of CHARACTER, CHARACTER VARYING and VARCHAR_IGNORECASE values and columns |
| 1814 |
is 1_000_000_000 characters. |
| 1815 |
</li><li>The maximum length of BINARY, BINARY VARYING, JAVA_OBJECT, GEOMETRY, and JSON values and columns |
| 1816 |
is 1_000_000_000 bytes. |
| 1817 |
</li><li>The maximum precision of NUMERIC and DECFLOAT values and columns is 100000. |
| 1818 |
</li><li>The maximum length of an ENUM value is 1048576 characters, the maximum number of ENUM values is 65536. |
| 1819 |
</li><li>The maximum cardinality of an ARRAY value or column is 65536. |
| 1820 |
</li><li>The maximum degree of a ROW value or column is 16384. |
| 1821 |
</li><li>The maximum index of parameter is 100000. |
| 1822 |
</li><li>Main memory requirements: The larger the database, the more main memory is required. |
| 1823 |
</li><li>Limit on the complexity of SQL statements. |
| 1824 |
Very complex expressions may result in a stack overflow exception. |
| 1825 |
</li><li>There is no limit for the following entities, except the memory and storage capacity: |
| 1826 |
maximum number of tables, indexes, triggers, and other database objects; |
| 1827 |
maximum statement length, tables per statement; |
| 1828 |
maximum rows per query; |
| 1829 |
maximum indexes per table, lob columns per table, and so on; |
| 1830 |
maximum row length, index row length, select row length. |
| 1831 |
</li><li>Querying from the metadata tables is slow if there are many tables (thousands). |
| 1832 |
</li><li>For other limitations on data types, see the data type documentation of this database. |
| 1833 |
</li></ul> |
| 1834 |
|
| 1835 |
<h2 id="glossary_links">Glossary and Links</h2> |
| 1836 |
<table class="main"> |
| 1837 |
<tr> |
| 1838 |
<th>Term</th> |
| 1839 |
<th>Description</th> |
| 1840 |
</tr> |
| 1841 |
<tr> |
| 1842 |
<td>AES-128</td> |
| 1843 |
<td>A block encryption algorithm. See also: <a |
| 1844 |
href="https://en.wikipedia.org/wiki/Advanced_Encryption_Standard">Wikipedia: |
| 1845 |
Advanced Encryption Standard</a></td> |
| 1846 |
</tr> |
| 1847 |
<tr> |
| 1848 |
<td>Birthday Paradox</td> |
| 1849 |
<td>Describes the higher than expected probability that two |
| 1850 |
persons in a room have the same birthday. Also valid for randomly |
| 1851 |
generated UUIDs. See also: <a |
| 1852 |
href="https://en.wikipedia.org/wiki/Birthday_problem">Wikipedia: |
| 1853 |
Birthday problem</a></td> |
| 1854 |
</tr> |
| 1855 |
<tr> |
| 1856 |
<td>Digest</td> |
| 1857 |
<td>Protocol to protect a password (but not to protect data). |
| 1858 |
See also: <a href="https://www.ietf.org/rfc/rfc2617.txt">RFC |
| 1859 |
2617: HTTP Digest Access Authentication</a></td> |
| 1860 |
</tr> |
| 1861 |
<tr> |
| 1862 |
<td>HTTPS</td> |
| 1863 |
<td>A protocol to provide security to HTTP connections. See |
| 1864 |
also: <a href="https://www.ietf.org/rfc/rfc2818.txt">RFC 2818: |
| 1865 |
HTTP Over TLS</a></td> |
| 1866 |
</tr> |
| 1867 |
<tr> |
| 1868 |
<td>Modes of Operation</td> |
| 1869 |
<td><a |
| 1870 |
href="https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation">Wikipedia: |
| 1871 |
Block cipher mode of operation</a></td> |
| 1872 |
</tr> |
| 1873 |
<tr> |
| 1874 |
<td>Salt</td> |
| 1875 |
<td>Random number to increase the security of passwords. See |
| 1876 |
also: <a href="https://en.wikipedia.org/wiki/Key_derivation_function">Wikipedia: |
| 1877 |
Key derivation function</a></td> |
| 1878 |
</tr> |
| 1879 |
<tr> |
| 1880 |
<td>SHA-256</td> |
| 1881 |
<td>A cryptographic one-way hash function. See also: <a |
| 1882 |
href="https://en.wikipedia.org/wiki/Secure_Hash_Algorithms">Wikipedia: |
| 1883 |
Secure Hash Algorithms</a></td> |
| 1884 |
</tr> |
| 1885 |
<tr> |
| 1886 |
<td>SQL Injection</td> |
| 1887 |
<td>A security vulnerability where an application embeds SQL |
| 1888 |
statements or expressions in user input. See also: <a |
| 1889 |
href="https://en.wikipedia.org/wiki/SQL_injection">Wikipedia: |
| 1890 |
SQL injection</a></td> |
| 1891 |
</tr> |
| 1892 |
<tr> |
| 1893 |
<td>Watermark Attack</td> |
| 1894 |
<td>Security problem of certain encryption programs where the |
| 1895 |
existence of certain data can be proven without decrypting. For more |
| 1896 |
information, search in the internet for 'watermark attack |
| 1897 |
cryptoloop'</td> |
| 1898 |
</tr> |
| 1899 |
<tr> |
| 1900 |
<td>SSL/TLS</td> |
| 1901 |
<td>Secure Sockets Layer / Transport Layer Security. See also: |
| 1902 |
<a href="https://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/JSSERefGuide.html">Java Secure Socket |
| 1903 |
Extension (JSSE)</a></td> |
| 1904 |
</tr> |
| 1905 |
</table> |
| 1906 |
|
| 1907 |
<!-- [close] { --></div></td></tr></table><!-- } --><!-- analytics --></body></html> |