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 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.
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).
Before using Derby in the OSGi container, you must integrate Derby with the container, as described in Integrate Derby with Fuse ESB Enterprise.
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
![[Note]](imagesdb/note.gif)
Note The 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 the
org.fusesource.example/derby-dsartifact under thederby-dsdirectory.Change the project packaging type to
bundle. Under thederby-dsdirectory, open thepom.xmlfile with a text editor and change the contents of thepackagingelement fromjartobundle, 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.xmlfile, add the followingbuildelement as a child of theprojectelement:<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.xmlfile, add the followingpluginelement as a child of thepluginselement, 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 (this is the only Maven dependency that is needed in this project). In the
pom.xmlfile, add the following elements as children of theprojectelement:<project ...> ... <properties> <derby-version>10.8.2.2</derby-version> </properties> <dependencies> <!-- Database dependencies --> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derby</artifactId> <version>${derby-version}</version> </dependency> </dependencies> ... </project>Customize the
derby-versionproperty 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-dsproject directory, create thedataSource.xmlblueprint file in the following location:src/main/resources/OSGI-INF/blueprint/dataSource.xml
Using your favorite text editor, add the following contents to the
dataSource.xmlfile:<?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"/> </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 the
derbyXADataSourcebean, thedatabaseNameproperty identifies the database instance that is created (in this case,txXaTutorial). Theserviceelement then exports the XA datasource as an OSGi service with the interface,javax.sql.XADataSource. Thedatasource.nameservice property makes it possible to identify this datasource unambiguously, when it is referenced from other bundles.To build the
derby-dsbundle and install it in the local Maven repository, enter the following Maven command from thederby-dsdirectory:mvn install








