Starting from version 3.1.0, Eclipse Kura includes the H2 database engine. This document provides a description of the main features of the H2 database integrated in Eclipse Kura and gives some basic information about its usage.

Supported Features

Eclipse Kura supports the following H2 database features:

  • Persistence modes: The H2 implementation currently supports in-memory and file-based database instances. See Persistence Modes for more details.

  • Multiple database instances: It is possible to create and configure multiple database instances from the Kura Web UI, these instances can be selectively consumed by applications. A default database instance is created automatically.

  • TCP Server: The current implementation allows external processes to access the database instances managed by Kura using TCP. This enables the integration of external applications that can share data with Kura components using the database.

  • Web based console: It is possible to start the H2 Web console directly from the Kura Web UI. The console can be used to inspect the database contents and perform arbitrary queries for debug purposes.

  • Basic credential management: The current implementation allows to change the password of the admin DB user from the Kura Web UI. This allows to restrict access to existing database instances.

Limitations

The following list briefly describes some limitations of the H2 database integrated in Eclipse Kura:

  • Private in-memory instances: Only named in-memory instances are supported (e.g. jdbc:h2:mem:<dbname>, where <dbname> is not the empty string), private instances represented by the jdbc:h2:mem: URL are currently not supported.

  • Remote connections: The current implementation only supports embedded database instances. Connecting to remote instances using the jdbc:h2:tcp:* and jdbc:h2:ssl:* connector URLs is not supported.

The DataService in Eclipse Kura uses the H2 database by default for implementing the message store. However, there is no support for migrating an old HSQLDB database to the new H2.

The new H2DbWireRecordFilter and H2DbWireRecordStore Wire components have been added. These components provide the same functionalities offered by the HSQL based DbWireRecordFilter and DbWireRecordStore components, but using the H2 database. The legacy components will continue to be available in order to keep backwards compatibility, but will be deprecated since Kura 3.1.0 and should not be used for new installations.

Usage

Creating a new H2 database instance

To create a new H2 database instance, use the following procedure:

  1. Open the Kura Web UI press and on the + button in the side menu, under the Services section. A pop-up dialog should appear.
  2. Select org.eclipse.kura.core.db.H2DbService from the Factories drop down list, enter an arbitrary name for the new instance and click Apply.

New instance

  1. An entry for the newly created instance should appear in the side menu under Services, click on it to review its configuration:

Instance configuration

  1. Change the settings if needed and the click Apply.

For more information about the DB URL format see http://www.h2database.com/html/features.html and Persistence Modes.

It is not possible to create different DB instances that manage the same DB url. When creating a new instance please make sure that the URL specified in the db.connector.url field is not managed by another instance.

Selecting a database instance for existing Kura components

A database instance is identified by its Kura service pid. The pid for the default instance is org.eclipse.kura.db.H2DbService, the pid for instances created using the Web UI is the string entered in the Name field at step 2 of the previous section.

The built-in Kura components that use database functionalities allow to specify which instance to use in their configuration. These components are the DataService component of the cloud stack and the H2DbWireRecordFilter and H2DbWireRecordStore wire components.

The configuration of each component contains a property that allows to specify the service pid of the desired instance:

  • DataService Data Service The pid of the database instance can be specified using the store.db.service.pid field.

  • DbWireRecordFilter Db Filter The pid of the database instance can be specified using the db.service.pid field.

  • DbWireRecordStore Db Store The pid of the database instance can be specified using the db.service.pid field.

Enabling the TCP Server

The TCP server can be used by creating an H2DbServer instance:

  1. Open the Kura Web UI press and on the + button in the side menu, under the Services section. A pop-up dialog should appear.

  2. Select org.eclipse.kura.core.db.H2DbServer from the Factories drop down list, enter an arbitrary name for the new instance and click Apply.

New instance

  1. Click on the name of the new server instance in the left side of the Web Ui. The configuration of the server component will appear:

Server

  1. Set the db.server.type field to TCP

  2. Review the server options under db.server.commandline, check http://www.h2database.com/javadoc/org/h2/tools/Server.html for more information about the available options.

The default settings, -tcpPort 9123 -tcpAllowOthers -ifExists, configure the server to bind on port 9123, to allow connections from any host and to allow access only to existing databases.

  1. Set the db.server.enabled to true.

  2. The server is now listening on the specified port.

Make sure to review the firewall configuration in order to ensure that the server is reachable from the external process.

Enabling the Web console

In order to enable the H2 Web console, proceed as follows:

  1. Create a new H2DbServer instance like in the previous section.

  2. Set the db.server.type field to WEB

  3. Enter appropriate parameters for the Web server in the db.server.commandline field. An example of valid settings can be -webPort 9123 -webAllowOthers -ifExists.

  4. Set the db.server.enabled to true.

  5. The server is now listening on the specified port. Make sure to review the firewall configuration in order to ensure that the server is reachable from the external process.

  6. Use a browser to access the console. Open the http://<device-ip>:<web-console-port> URL, where <device-ip> is the IP address of the Kura device and <web-console-port> is the port specified at step 3.

  1. Enter the DB URL as specified in the Kura configuration in the JDBC URL field and the credentials. Click on Connect, you should be able to access the console.

Changing the password

The prerequisite for being able to change the password is that

  1. Open the configuration of the desired database instance in the Kura Web UI.

  2. Enter the new password in the db.password field.

  3. Click Apply.

Note:

If the H2DbServer instance fails to open a database, it will delete and recreate all database files. This behaviour is aimed at preventing potential issues caused by incorrect credentials in the configuration snapshots. It is highly recommended to perform a backup of an existing database before trying to open it using a H2DbService instance and before changing the password.

Persistence Modes

The H2 database support several levels of persistence modes.

In memory

An in-memory database instance can be created using the following URL structure: jdbc:h2:mem:<dbname>, where <dbname> is a non-empty string that represents the database name. This configuration is suggested for database instances that are frequently updated. Examples:

  • jdbc:h2:mem:kuradb
  • jdbc:h2:mem:mydb

The default database instance is in-memory by default and uses the jdbc:h2:mem:kuradb URL.

Most persistent

A persistent database instance can be created using the jdbc:h2:file:<dbpath>, where <dbpath> is a non-empty string that represents the database path.

If no URL parameters are supplied the database will enable the transaction log by default. The transaction log is used to restore the database to a consistent state after a crash or power failure. This provides good protection against data losses but causes a lot of writes to the storage device, reducing both performance and the lifetime of flash-based storage devices.

This configuration is suggested for database instances that are rarely updated. Examples:

  • jdbc:h2:file:/opt/db/mydb

Make sure to use absolute paths in the db URL since H2 does not support db paths relative to the working directory

Mostly persistent

The transaction log can be disabled by appending the LOG=0 parameter to the DB URL. In this way it is possible to reduce the stress on the underlying storage device and increase performance, at the expense of a higher probability of losing data in case of power failure.

In order to reduce the probability of data losses the H2DbService performs periodic checkpoints on the database. A checkpoint forces all pending modifications to be committed to the storage device. The interval in seconds between two consecutive checkpoints can be configured using the db.checkpoint.interval.seconds property.

This configuration is suggested for database instances with intermediate update rates. Examples:

  • jdbc:h2:file:/opt/db/mydb;LOG=0