JDBC Persistence

This service writes and reads item states to and from a number of relational database systems that support Java Database Connectivity (JDBC). This service allows you to persist state updates using one of several different underlying database services. It is designed for a maximum of scalability, to store very large amounts of data and still over the years not lose its speed.

The generic design makes it relatively easy for developers to integrate other databases that have JDBC drivers. The following databases are currently supported and tested:

DatabaseTested Driver / Version
Apache Derbyderby-10.12.1.1.jar
H2h2-1.4.191.jar
HSQLDBhsqldb-2.3.3.jar
MariaDBmariadb-java-client-1.4.6.jar
MySQLmysql-connector-java-5.1.39.jar
PostgreSQLpostgresql-9.4.1209.jre7.jar
SQLitesqlite-jdbc-3.16.1.jar

Table of Contents

Configuration

This service can be configured in the file services/jdbc.cfg.

PropertyDefaultRequiredDescription
urlYesJDBC URL to establish a connection to your database. Examples:

jdbc:derby:./testDerby;create=true
jdbc:h2:./testH2
jdbc:hsqldb:./testHsqlDb
jdbc:mariadb://192.168.0.1:3306/testMariadb
jdbc:mysql://192.168.0.1:3306/testMysql?serverTimezone=UTC
jdbc:postgresql://192.168.0.1:5432/testPostgresql
jdbc:sqlite:./testSqlite.db.

If no database is available it will be created; for example the url jdbc:h2:./testH2 creates a new H2 database in openHAB folder. Example to create your own MySQL database directly:

CREATE DATABASE 'yourDB' CHARACTER SET utf8 COLLATE utf8_general_ci;
userif neededdatabase user name
passwordif neededdatabase user password
errReconnectThreshold0Nowhen the service is deactivated (0 means ignore)
sqltype.CALLVARCHAR(200)NoAll sqlType options allow you to change the SQL data type used to store values for different openHAB item states. See the following links for further information: mybatisH2PostgresSQL
sqltype.COLORVARCHAR(70)Nosee above
sqltype.CONTACTVARCHAR(6)Nosee above
sqltype.DATETIMEDATETIMENosee above
sqltype.DIMMERTINYINTNosee above
sqltype.LOCATIONVARCHAR(30)Nosee above
sqltype.NUMBERDOUBLENosee above
sqltype.ROLLERSHUTTERTINYINTNosee above
sqltype.STRINGVARCHAR(65500)Nosee above
sqltype.SWITCHVARCHAR(6)Nosee above
sqltype.TABLEPRIMARYKEYTIMESTAMPNosee above
sqltype.TABLEPRIMARYVALUENOW()Nosee above
numberDecimalcount3Nofor Itemtype "Number" default decimal digit count
tableNamePrefixitemNotable name prefix. For Migration from MySQL Persistence, set to Item.
tableUseRealItemNamesfalseNotable name prefix generation. When set to true, real item names are used for table names and tableNamePrefix is ignored. When set to false, the tableNamePrefix is used to generate table names with sequential numbers.
tableIdDigitCount4Nowhen tableUseRealItemNames is false and thus table names are generated sequentially, this controls how many zero-padded digits are used in the table name. With the default of 4, the first table name will end with 0001. For migration from the MySQL persistence service, set this to 0.
rebuildTableNamesfalseNorename existing tables using tableUseRealItemNames and tableIdDigitCount. USE WITH CARE! Deactivate after Renaming is done!
jdbc.maximumPoolSizeconfigured per database in package org.openhab.persistence.jdbc.db.*NoSome embeded databases can handle only one connection. See this link for more information
jdbc.minimumIdlesee aboveNosee above
enableLogTimefalseNotimekeeping

All item- and event-related configuration is done in the file persistence/jdbc.persist.

To configure this service as the default persistence service for openHAB 2, add or change the line

org.eclipse.smarthome.persistence:default=jdbc

in the file services/runtime.cfg.

Minimal Configuration

services/jdbc.cfg

url=jdbc:postgresql://192.168.0.1:5432/testPostgresql

Migration from MySQL to JDBC Persistence Services

The JDBC Persistence service can act as a replacement for the MySQL Persistence service. Here is an example of a configuration for a MySQL database named testMysql with user test and password test:

services/jdbc.cfg

url=jdbc:mysql://192.168.0.1:3306/testMysql
user=test
password=test
tableNamePrefix=Item
tableUseRealItemNames=false
tableIdDigitCount=0

Remember to install and uninstall the services you want, and rename persistence/mysql.persist to persistence/jdbc.persist.

Technical Notes

Database Table Schema

The table name schema can be reconfigured after creation, if needed.

The service will create a mapping table to link each item to a table, and a separate table is generated for each item. The item data tables include time and data values. The SQL data type used depends on the openHAB item type, and allows the item state to be recovered back into openHAB in the same way it was stored.

With this per-item layout, the scalability and easy maintenance of the database is ensured, even if large amounts of data must be managed. To rename existing tables, use the parameters tableUseRealItemNames and tableIdDigitCount in the configuration.

Number Precision

Default openHAB number items are persisted with SQL datatype double. Internally openHAB uses BigDecimal. If better numerical precision is needed, for example set sqltype.NUMBER = DECIMAL(max digits, max decimals), then on the Java side, the service works with BigDecimal without type conversion. If more come decimals as max decimals provides, this persisted value is rounded mathematically correctly. The SQL types DECIMAL or NUMERIC are precise, but to work with DOUBLE is faster.

Rounding results

The results of database queries of number items are rounded to three decimal places by default. With numberDecimalcount decimals can be changed. Especially if sql types DECIMAL or NUMERIC are used for sqltype.NUMBER, rounding can be disabled by setting numberDecimalcount=-1.

For Developers

  • Clearly separated source files for the database-specific part of openHAB logic.
  • Code duplication by similar services is prevented.
  • Integrating a new SQL and JDBC enabled database is fairly simple.

Performance Tests

Not necessarily representative of the performance you may experience.

DATABASEFIRST RUNAVERAGEFASTESTSIZE AFTERCOMMENT
Derby7.8296.8925.3815.36 MBlocal embedded
H21.7972.0801.5800.96 MBlocal embedded
hsqldb3.4742.1041.3101.23 MBlocal embedded
mysql11.87311.52410.971-ext. Server VM
postgresql8.1477.0726.895-ext. Server VM
sqlite2.4061.2491.1370.28 MBlocal embedded
  • Each test ran about 20 Times every 30 seconds.
  • openHAB 1.x has ready started for about a Minute.
  • the data in seconds for the evaluation are from the console output.

Used a script like this:

var count = 0;
rule "DB STRESS TEST"
when 
	Time cron "30 * * * * ?"
then
	if( count = 24) count = 0
	count = count+1
	if( count > 3 && count < 23){
		for( var i=500; i>1; i=i-1){
			postUpdate( NUMBERITEM, i)
			SWITCHITEM.previousState().state
			postUpdate( DIMMERITEM, OFF)
			NUMBERITEM.changedSince( now().minusMinutes(1))
			postUpdate( DIMMERITEM, ON)
		}
	}
end