Previous Topic

Next Topic

Book Contents

Book Index

Overview of SQLite Database

Guidelines to accessing the SQLite database within an OSGi-enabled platform.

The open source SQLite database is integrated in the Database Module. It is an embedded database library that implements a subset of the SQL-92 standard. For further information refer to the SQLite official site.

SQLite Java Wrapper/JDBC Driver

The SQLite database is accessible through the integrated SQLite Java Wrapper/JDBC Driver via the DataSourceFactory service.

To get the DataSourceFactory service, follow these steps:

  1. Get its Service Reference and then get the service itself:

    ServiceReference ref = bundleContext.getServiceReference(DataSourceFactory.class.getName());

    DataSourceFactory dataSourceFactory = (DataSourceFactory) bundleContext.getService(ref);

  2. Get the DataSource from the given dataSourceFactory by calling the createDataSource method:

    Properties dataProps = new Properties();

    dataProps.put(DataSourceFactory.JDBC_URL, "jdbc:sqlite:/" + dbLocation);

    DataSource ds = dataSourceFactory.createDataSource(dataProps);

    In the code snippet above dataProps is a Properties table, that must include the database URL as a minimum.

  3. Get the specific connection:

    Connection conn = ds.getConnection();

The dataSourceFactory service is registered with the following properties:

The build version of the driver in the last property can be changed.

Backup and Restore

The backup and restore functionalities of the Database module are provided via the DataSourceBackup service. The examples below show how to use the DataSourceBackup service methods.

As can be seen from the examples, both DataSourceFactory and DataSourceBackup services use the same properties for their registration.

The package that provides the DataSourceBackup interface is com.prosyst.mbs.services.db.sql – for details refer to the API page.

Database URL Specifics

The user executing the OSGi framework must have the proper file permissions over the database file.

If a database write operation exists but the user does not have the needed permissions, an exception will be thrown. The following examples illustrate different approaches towards this condition.

References