Upgraded embedded Derby support to 10.6+ and build to 10.10

Issue: SPR-10469
This commit is contained in:
Juergen Hoeller
2013-12-23 21:58:42 +01:00
parent 9a39f39b6f
commit b4d6e27fb3
4 changed files with 34 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,19 +16,16 @@
package org.springframework.jdbc.datasource.embedded;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.derby.impl.io.VFMemoryStorageFactory;
import org.apache.derby.jdbc.EmbeddedDriver;
/**
* {@link EmbeddedDatabaseConfigurer} for the Apache Derby database.
* {@link EmbeddedDatabaseConfigurer} for the Apache Derby database 10.6+.
* Call {@link #getInstance()} to get the singleton instance of this class.
*
* @author Oliver Gierke
* @author Juergen Hoeller
@@ -36,19 +33,9 @@ import org.apache.derby.jdbc.EmbeddedDriver;
*/
final class DerbyEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigurer {
private static final Log logger = LogFactory.getLog(DerbyEmbeddedDatabaseConfigurer.class);
private static final String URL_TEMPLATE = "jdbc:derby:memory:%s;%s";
// Error code that indicates successful shutdown
private static final String SHUTDOWN_CODE = "08006";
private static final boolean IS_AT_LEAST_DOT_SIX = new EmbeddedDriver().getMinorVersion() >= 6;
private static final String SHUTDOWN_COMMAND =
String.format("%s=true", IS_AT_LEAST_DOT_SIX ? "drop" : "shutdown");
private static DerbyEmbeddedDatabaseConfigurer INSTANCE;
private static DerbyEmbeddedDatabaseConfigurer instance;
/**
@@ -57,15 +44,16 @@ final class DerbyEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigure
* @throws ClassNotFoundException if Derby is not on the classpath
*/
public static synchronized DerbyEmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException {
if (INSTANCE == null) {
if (instance == null) {
// disable log file
System.setProperty("derby.stream.error.method",
OutputStreamFactory.class.getName() + ".getNoopOutputStream");
INSTANCE = new DerbyEmbeddedDatabaseConfigurer();
instance = new DerbyEmbeddedDatabaseConfigurer();
}
return INSTANCE;
return instance;
}
private DerbyEmbeddedDatabaseConfigurer() {
}
@@ -81,22 +69,12 @@ final class DerbyEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigure
public void shutdown(DataSource dataSource, String databaseName) {
try {
new EmbeddedDriver().connect(
String.format(URL_TEMPLATE, databaseName, SHUTDOWN_COMMAND), new Properties());
String.format(URL_TEMPLATE, databaseName, "drop=true"), new Properties());
}
catch (SQLException ex) {
if (!SHUTDOWN_CODE.equals(ex.getSQLState())) {
logger.warn("Could not shutdown in-memory Derby database", ex);
return;
}
if (!IS_AT_LEAST_DOT_SIX) {
// Explicitly purge the in-memory database, to prevent it
// from hanging around after being shut down.
try {
VFMemoryStorageFactory.purgeDatabase(new File(databaseName).getCanonicalPath());
}
catch (IOException ex2) {
logger.warn("Could not purge in-memory Derby database", ex2);
}
// Error code that indicates successful shutdown
if (!"08006".equals(ex.getSQLState())) {
LogFactory.getLog(getClass()).warn("Could not shutdown in-memory Derby database", ex);
}
}
}