10.3. Define a Derby Datasource
Overview
This section explains how to define a Derby datasource (and database instance), package the datasource as an OSGi bundle, and export the datasource as an OSGi service.
Derby data source implementations
Derby provides a variety of different data source implementations, as described in Derby data sources. For XA transactions, there are two alternatives:
EmbeddedXADataSource
(where the database instance runs in the same JVM) and ClientXADataSource
(where the application connects to a remote database instance). The current example uses EmbeddedXADataSource
.
Auto-enlisting an XA data source
In practice, you need to wrap the basic Derby data source with an object that performs auto-enlisting of the XA data source. Apache Aries provides such a wrapper layer. In order to trigger the wrapper mechanism, however, you must export the Derby data source as an OSGi service as described in Apache Aries Auto-Enlisting XA Wrapper (and as is done in the current example).
Prerequisites
Before using Derby in the OSGi container, you must integrate Derby with the container, as described in Section 10.2, “Integrate Derby with Red Hat JBoss Fuse”.
Steps to define a Derby datasource
Perform the following steps to define a Derby datasource packaged in an OSGi bundle:
- Use the quickstart archetype to create a basic Maven project. Maven provides archetypes, which serve as templates for creating new projects. The Maven quickstart archetype is a basic archetype, providing the bare outline of a new Maven project.To create a new project for the Derby datasource bundle, invoke the Maven archetype plug-in as follows. Open a new command prompt, change directory to a convenient location (that is, to the directory where you will store your Maven projects), and enter the following command:
mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=org.fusesource.example -DartifactId=derby-ds
NoteThe preceding command parameters are shown on separate lines for ease of reading. You must enter the entire command on a single line, however.After downloading the requisite dependencies to run the quickstart archetype, the command creates a new Maven project for theorg.fusesource.example/derby-ds
artifact under thederby-ds
directory. - Change the project packaging type to
bundle
. Under thederby-ds
directory, open thepom.xml
file with a text editor and change the contents of thepackaging
element fromjar
tobundle
, as shown in the following highlighted line:<project ...> ... <groupId>org.fusesource.example</groupId> <artifactId>derby-ds</artifactId> <version>1.0-SNAPSHOT</version> <packaging>bundle</packaging> ... </project>
- Add the bundle configuration to the POM. In the
pom.xml
file, add the followingbuild
element as a child of theproject
element:<project ...> ... <build> <defaultGoal>install</defaultGoal> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName> </instructions> </configuration> </plugin> </plugins> </build> </project>
- Customize the Maven compiler plug-in to enforce JDK 1.6 coding syntax. In the
pom.xml
file, add the followingplugin
element as a child of theplugins
element, to configure the Maven compiler plug-in:<project ...> ... <build> <defaultGoal>install</defaultGoal> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> ... </plugins> </build> </project>
- Add the Derby dependency to the POM and add the
derby-version
property to specify the version of Derby you are using. In thepom.xml
file, add thederby-version
element and thedependency
element as shown:<project ...> ... <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <derby-version>10.10.1.1</derby-version> </properties> <dependencies> <!-- Database dependencies --> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derby</artifactId> <version>${derby-version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> ... </project>
ImportantRemember to customize thederby-version
property to the version of Derby you are using. - Instantiate the Derby database instance and export the datasource as an OSGi service. In fact, this example exports two datasources: an XA datasource and a non-transactional datasource. The Derby datasources are exported using a blueprint XML file, which must be stored in the standard location,
OSGI-INF/blueprint/
. Under thederby-ds
project directory, create thedataSource.xml
blueprint file in the following location:src/main/resources/OSGI-INF/blueprint/dataSource.xml
Using your favorite text editor, add the following contents to thedataSource.xml
file:<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0" default-activation="lazy"> <bean id="derbyXADataSource" class="org.apache.derby.jdbc.EmbeddedXADataSource"> <property name="databaseName" value="txXaTutorial"/> </bean> <service ref="derbyXADataSource" interface="javax.sql.XADataSource"> <service-properties> <entry key="datasource.name" value="derbyXADB"/> <!-- A unique ID for this XA resource. Required to enable XA recovery. --> <entry key="aries.xa.name" value="derbyDS"/> </service-properties> </service> <bean id="derbyDataSource" class="org.apache.derby.jdbc.EmbeddedDataSource"> <property name="databaseName" value="txXaTutorial"/> </bean> <service ref="derbyDataSource" interface="javax.sql.DataSource"> <service-properties> <entry key="datasource.name" value="derbyDB"/> </service-properties> </service> </blueprint>
In the definition of thederbyXADataSource
bean, thedatabaseName
property identifies the database instance that is created (in this case,txXaTutorial
).The firstservice
element exports the XA datasource as an OSGi service with the interface,javax.sql.XADataSource
. The following service properties are defined:datasource.name
- Identifies this datasource unambiguously when it is referenced from other OSGi bundles.
aries.xa.name
- Defines a unique XA resource name, which is used by the Aries transaction manager to identify this JDBC resource. This property must be defined in order to support XA recovery.
The secondservice
element defines a non-transactional datasource as an OSGi service with the interfacejavax.sql.DataSource
. - To build the
derby-ds
bundle and install it in the local Maven repository, enter the following Maven command from thederby-ds
directory:mvn install