Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.10.3. Define a Derby Datasource
Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
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
mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=org.fusesource.example -DartifactId=derby-ds
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the bundle configuration to the POM. In the
pom.xml
file, add the followingbuild
element as a child of theproject
element:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 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:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Add the Derby dependency to the POM (this is the only Maven dependency that is needed in this project). In the
pom.xml
file, add the following elements as children of theproject
element:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
src/main/resources/OSGI-INF/blueprint/dataSource.xml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Using your favorite text editor, add the following contents to thedataSource.xml
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the definition of thederbyXADataSource
bean, thedatabaseName
property identifies the database instance that is created (in this case,txXaTutorial
). Theservice
element then exports the XA datasource as an OSGi service with the interface,javax.sql.XADataSource
. Thedatasource.name
service property makes it possible to identify this datasource unambiguously, when it is referenced from other bundles. - 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
mvn install
Copy to Clipboard Copied! Toggle word wrap Toggle overflow