SPR-7602 - Correctly shutdown Derby >= 10.6

The shutdown mechanism for in-memory databases has changed since 10.6. We now have to trigger 'drop' instead of 'shutdown'. Besides that we can skip purging the database manually in newer versions.
This commit is contained in:
Oliver Gierke 2010-09-30 17:56:41 +00:00
parent e211c09065
commit b467f2c858
1 changed files with 12 additions and 9 deletions

View File

@ -20,6 +20,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Properties; import java.util.Properties;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -42,6 +43,8 @@ final class DerbyEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigure
// Error code that indicates successful shutdown // Error code that indicates successful shutdown
private static final String SHUTDOWN_CODE = "08006"; 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;
@ -72,15 +75,16 @@ final class DerbyEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigure
public void shutdown(DataSource dataSource, String databaseName) { public void shutdown(DataSource dataSource, String databaseName) {
try { try {
new EmbeddedDriver().connect( new EmbeddedDriver().connect(String.format(URL_TEMPLATE, databaseName, SHUTDOWN_COMMAND), new Properties());
String.format(URL_TEMPLATE, databaseName, "shutdown=true"), new Properties()); } catch (SQLException ex) {
}
catch (SQLException ex) { if (!SHUTDOWN_CODE.equals(ex.getSQLState())) {
if (SHUTDOWN_CODE.equals(ex.getSQLState())) {
purgeDatabase(databaseName);
}
else {
logger.warn("Could not shutdown in-memory Derby database", ex); logger.warn("Could not shutdown in-memory Derby database", ex);
return;
}
if (!IS_AT_LEAST_DOT_SIX) {
purgeDatabase(databaseName);
} }
} }
} }
@ -99,5 +103,4 @@ final class DerbyEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigure
logger.warn("Could not purge in-memory Derby database", ex); logger.warn("Could not purge in-memory Derby database", ex);
} }
} }
} }