public class EmbeddedDerbyResource extends ExternalResource implements Closeable
configuration
and a Derby System Home
.
The class can be used either as a JUnit Rule
/ ClassRule
OR
directly by the user. Initialization and de-initialization of an instance of this class (and the embedded Derby
instance) is handed by the start()
and close()
methods respectively. When used as a
Rule
or ClassRule
, the before()
and after()
mdthods from those interfaces
handle the initialization and de-initialization for the user (internally using the #start()
and
#close()
methods.
Derby does not allow running multiple instances in the same JVM, so external protection should be provided to protect against that.
Example of usage:
public class SimpleDerbyTest { private TemporaryFolder tempFolder = new TemporaryFolder(); private EmbeddedDerbyResource embeddedDerbyResource = new EmbeddedDerbyResource(DerbyResourceConfig.buildDefault().useDevNullErrorLogging(), tempFolder); @Rule public RuleChain derbyRuleChain = RuleChain.outerRule(tempFolder).around(embeddedDerbyResource); @Test public void test () throws SQLException { final String jdbcUrl = embeddedDerbyResource.getJdbcUrl(); Connection connection = null; Statement stmt = null; ResultSet rs = null; try { connection = DriverManager.getConnection(jdbcUrl); // Check a value stmt = connection.createStatement(); rs = stmt.executeQuery("SELECT 1 FROM SYSIBM.SYSDUMMY1"); assertTrue(rs.next()); } finally { // Close resources } } }
For further information and examples, see User Manual on the Project Website.
DerbyResourceConfig
Constructor and Description |
---|
EmbeddedDerbyResource(DerbyResourceConfig dbResourceConfig,
File derbySystemHomeDir)
Creates a new Derby resource.
|
EmbeddedDerbyResource(DerbyResourceConfig dbResourceConfig,
TemporaryFolder derbySystemHomeParentTmpFolder)
Creates a new Derby resource.
|
Modifier and Type | Method and Description |
---|---|
protected void |
after() |
protected void |
appendDbLocNameToUrl(StringBuilder jdbcUrlBldr)
Adds the database name to the Derby JDBC Url being built, taking into consideration sub-protocol specific
variations.
|
void |
backupLiveDatabase(File backupDir,
boolean waitForTransactions,
boolean enableArchiveLogging,
boolean deleteArchivedLogs)
Perform an online backup of the running instance.
|
protected void |
before() |
void |
close()
Shuts down and closes the Derby Instance.
|
Connection |
createConnection()
Create and return a new connection for this resource.
|
protected void |
ensureActive()
Checks if the Embedded Derby Resource is active, if not throws an
IllegalStateException . |
protected DerbyResourceConfig |
getConfig()
Allows extending classes to use the config.
|
String |
getDatabasePath()
Returns the database path of the JDBC URL.
|
File |
getDerbySystemHome()
Returns the file reference to the Derby system home.
|
String |
getJdbcUrl()
Returns a URL that can be used to create a connection to this database instance.
|
boolean |
isActive()
Returns true if the resource is started and not closed.
|
void |
start()
Starts the Embedded derby instance.
|
apply
public EmbeddedDerbyResource(DerbyResourceConfig dbResourceConfig, File derbySystemHomeDir)
dbResourceConfig
- Configurations to setup this resourcederbySystemHomeDir
- A folder to use as the derby system homepublic EmbeddedDerbyResource(DerbyResourceConfig dbResourceConfig, TemporaryFolder derbySystemHomeParentTmpFolder)
dbResourceConfig
- Configurations to setup this resourcederbySystemHomeParentTmpFolder
- A temporary folder to use as the derby system homeprotected DerbyResourceConfig getConfig()
protected void appendDbLocNameToUrl(StringBuilder jdbcUrlBldr)
jdbcUrlBldr
- The String builder to which the database name is being addedprotected void before() throws Throwable
before
in class ExternalResource
Throwable
public void start() throws IOException, SQLException
Note: If using this instance as a JUnit Rule, do not call this method; initialization is already handled from the ExternalResource.before().
On successful completion (with the exception of failures in
DerbyResourceConfig.getPostInitScripts(), the isActive()
state of this resource is set to
true
. Further calls to this method while the resource is active has no effect.
IOException
- IO exception creating or setting derby homeSQLException
- SQL exception starting derby or running the init scriptsprotected void after()
after
in class ExternalResource
public void close() throws IOException
derby.system.home
system property. However, should another derby instance need to be created in the same JVM after this is shut
down, ensure the Derby system is properly shut down (see DerbyUtils.shutdownDerbySystemQuitely(boolean)
.
This method sets the isActive()
state of the resouce to false, and calls to this method when the
resource is not active are ignored.
close
in interface Closeable
close
in interface AutoCloseable
IOException
public File getDerbySystemHome()
IllegalStateException
- if the method is invoked when the database is not isActive()
.public String getJdbcUrl()
IllegalStateException
- if the method is invoked when the database is not isActive()
.public String getDatabasePath()
DerbyResourceConfig.getDatabasePath()
public Connection createConnection() throws SQLException
SQLException
- If there is an error creating the connectionIllegalStateException
- if the method is invoked when the database is not isActive()
.public boolean isActive()
protected void ensureActive()
IllegalStateException
.public void backupLiveDatabase(File backupDir, boolean waitForTransactions, boolean enableArchiveLogging, boolean deleteArchivedLogs) throws SQLException
SYSCS_UTIL.SYSCS_BACKUP_DATABASE
if enableArchiveLogging
is set to false
or SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE
otherwise. If the
waitForTransactions
parameter is set to false
the _NOWAIT
versions of the
procedures are used.
For more information on backing up Derby database, see Using the backup procedures to perform an online backup in the Derby Administrators guide.
backupDir
- The directory to which the database should be backed up.waitForTransactions
- Wait for running transactions to complete.enableArchiveLogging
- If archive logging should be enabled for the database.deleteArchivedLogs
- Ask Derby to delete the old archive logs after the backup is successful.SQLException
- Exception from Derby when the backup fails.IllegalStateException
- if the method is invoked when the database is not isActive()
.Copyright © 2015-2016 Development Entropy (deventropy.org) Contributors