User Guide :: Using DataSources
Derby JDBC classes include support for javax.sql.DataSource and related APIs (see DataSource classes for more information). When writing JUnit tests involving Derby classes, if the tests require classes that would access the database through a DataSource, you are free to write code to initialize the DataSources, connection pools, etc. independent of any JUnit Helper code. However, JUnit Helper includes some convenience mechanisms of accessing such resources without additional code. This page discusses using DataSrouces from the EmbeddedDerbyDataSourceResource.
The classes provided in this library support all three different kinds of DataSource interfaces in JDBC and supported by Derby.
EmbeddedDerbyDataSourceResource
The module provides a resource EmbeddedDerbyDataSourceResource which provides additional mechanisms for creating and accessing data sources. The class provides an in-memory Derby resource capable of providing different kinds of DataSources supported by Derby. Access to the EmbeddedDerbyDataSourceFactory for accessing the datasources is provided through the #getDataSourceFactory() method.
This resource extends EmbeddedDerbyResource and all configuration and functionality in that class are available here.
EmbeddedDerbyDataSourceFactory
EmbeddedDerbyDataSourceFactory interface defines a factory that is implemented by the EmbeddedDerbyDataSourceResource which can create the different kinds of data sources. The factory instance can be accessed from EmbeddedDerbyDataSourceResource#getDataSourceFactory().
The factory will return data source instances only while the EmbeddedDerbyDataSourceResource is active, other times an IllegalStateException is returned.
The following table shows the DataSource instances returned by the different factory methods:
Factory method | JDBC Interface | Derby implementation returned |
---|---|---|
#getDataSource(boolean) | javax.sql.DataSource | org.apache.derby.jdbc.EmbeddedDataSource |
#getConnectionPoolDataSource(boolean) | javax.sql.ConnectionPoolDataSource | org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource |
#getXADataSource(boolean) | javax.sql.XADataSource | org.apache.derby.jdbc.EmbeddedXADataSource |
Caching DataSources
The factory provides a basic single instance lazy cache for each kind of DataSource returned. If the cachedInstance parameter of the factory methods is set to true the internal cached instance is returned (the instance is created on the first call for a cached DataSource instance).
JavaSE Compact Profiles
Since Java SE version 8, Oracle JRE has provided compact profiles. compact2 profile has JDBC support, but leaves out certain classes required for full DataSource support. Derby provides support for DataSources for use with the compact2 profile (using org.apache.derby.jdbc.BasicEmbedded* classes); however at this time the Derby JUnit Helper library does not provide support for those data sources in the provided factory.