* Can be configured:
* Call {@link #setDatabaseName(String)} to change the name of the database.
@@ -54,8 +54,8 @@ public class EmbeddedDatabaseFactory {
private DataSource dataSource;
/**
- * Creates a default {@link EmbeddedDatabaseFactory}. Calling {@link #getDatabase()} will create a embedded HSQL
- * database of name 'testdb'.
+ * Creates a default {@link EmbeddedDatabaseFactory}.
+ * Calling {@link #getDatabase()} will create a embedded HSQL database of name 'testdb'.
*/
public EmbeddedDatabaseFactory() {
setDatabaseName("testdb");
@@ -82,8 +82,8 @@ public class EmbeddedDatabaseFactory {
}
/**
- * Sets the strategy that will be used to configure the embedded database instance. Call this when you wish to use
- * an embedded database type not already supported.
+ * Sets the strategy that will be used to configure the embedded database instance.
+ * Call this when you wish to use an embedded database type not already supported.
* @param configurer the embedded database configurer
*/
public void setDatabaseConfigurer(EmbeddedDatabaseConfigurer configurer) {
@@ -100,8 +100,8 @@ public class EmbeddedDatabaseFactory {
}
/**
- * Sets the factory to use to create the DataSource instance that connects to the embedded database. Defaults to
- * {@link SimpleDriverDataSourceFactory}.
+ * Sets the factory to use to create the DataSource instance that connects to the embedded database
+ * Defaults to {@link SimpleDriverDataSourceFactory}.
* @param dataSourceFactory the data source factory
*/
public void setDataSourceFactory(DataSourceFactory dataSourceFactory) {
@@ -124,9 +124,8 @@ public class EmbeddedDatabaseFactory {
// subclassing hooks
/**
- * Hook to initialize the embedded database.
- * Subclasses may call to force initialization.
- * After calling this method, {@link #getDataSource()} returns the DataSource providing connectivity to the db.
+ * Hook to initialize the embedded database. Subclasses may call to force initialization. After calling this method,
+ * {@link #getDataSource()} returns the DataSource providing connectivity to the db.
*/
protected void initDatabase() {
// create the embedded database source first
@@ -152,14 +151,12 @@ public class EmbeddedDatabaseFactory {
}
/**
- * Hook to shutdown the embedded database.
- * Subclasses may call to force shutdown.
- * After calling, {@link #getDataSource()} returns null.
- * Does nothing if no embedded database has been initialized.
+ * Hook to shutdown the embedded database. Subclasses may call to force shutdown.
+ * After calling, {@link #getDataSource()} returns null. Does nothing if no embedded database has been initialized.
*/
protected void shutdownDatabase() {
if (dataSource != null) {
- databaseConfigurer.shutdown(dataSource);
+ databaseConfigurer.shutdown(dataSource, databaseName);
dataSource = null;
}
}
diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType.java
index 30085eb3eb..052e53d722 100644
--- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType.java
+++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType.java
@@ -18,7 +18,8 @@ package org.springframework.jdbc.datasource.embedded;
/**
* A supported embedded database type.
* @author Keith Donald
+ * @author Oliver Gierke
*/
public enum EmbeddedDatabaseType {
- HSQL;
+ HSQL, H2;
}
diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java
new file mode 100644
index 0000000000..4c7ed5ecbb
--- /dev/null
+++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/H2EmbeddedDatabaseConfigurer.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2002-2009 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.jdbc.datasource.embedded;
+
+import org.springframework.util.ClassUtils;
+
+/**
+ * Initializes a HSQL embedded database instance.
+ * Call {@link #getInstance()} to get the singleton instance of this class. *
+ * @author Oliver Gierke
+ */
+final class H2EmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigurer {
+
+ private static H2EmbeddedDatabaseConfigurer INSTANCE;
+
+ private H2EmbeddedDatabaseConfigurer() {
+ }
+
+ /**
+ * Get the singleton {@link HsqlEmbeddedDatabaseConfigurer} instance.
+ * @return the configurer
+ * @throws ClassNotFoundException if HSQL is not on the classpath
+ */
+ public static synchronized H2EmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException {
+ if (INSTANCE == null) {
+ ClassUtils.forName("org.h2.Driver", H2EmbeddedDatabaseConfigurer.class.getClassLoader());
+ INSTANCE = new H2EmbeddedDatabaseConfigurer();
+ }
+ return INSTANCE;
+ }
+
+ public void configureConnectionProperties(ConnectionProperties properties, String databaseName) {
+ properties.setDriverClass(org.h2.Driver.class);
+ properties.setUrl(String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1", databaseName));
+ properties.setUsername("sa");
+ properties.setPassword("");
+ }
+}
\ No newline at end of file
diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/HsqlEmbeddedDatabaseConfigurer.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/HsqlEmbeddedDatabaseConfigurer.java
index 874b778731..c650d32093 100644
--- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/HsqlEmbeddedDatabaseConfigurer.java
+++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/HsqlEmbeddedDatabaseConfigurer.java
@@ -15,31 +15,21 @@
*/
package org.springframework.jdbc.datasource.embedded;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.sql.Statement;
-
-import javax.sql.DataSource;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.springframework.util.ClassUtils;
/**
* Initializes a HSQL embedded database instance.
* Call {@link #getInstance()} to get the singleton instance of this class.
- *
* @author Keith Donald
+ * @authoe Oliver Gierke
*/
-final class HsqlEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigurer {
-
- private static final Log logger = LogFactory.getLog(HsqlEmbeddedDatabaseConfigurer.class);
+final class HsqlEmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigurer {
private static HsqlEmbeddedDatabaseConfigurer INSTANCE;
-
- private HsqlEmbeddedDatabaseConfigurer() {
+
+ private HsqlEmbeddedDatabaseConfigurer() {
}
-
+
/**
* Get the singleton {@link HsqlEmbeddedDatabaseConfigurer} instance.
* @return the configurer
@@ -52,27 +42,11 @@ final class HsqlEmbeddedDatabaseConfigurer implements EmbeddedDatabaseConfigurer
}
return INSTANCE;
}
-
+
public void configureConnectionProperties(ConnectionProperties properties, String databaseName) {
properties.setDriverClass(org.hsqldb.jdbcDriver.class);
properties.setUrl("jdbc:hsqldb:mem:" + databaseName);
properties.setUsername("sa");
- properties.setPassword("");
+ properties.setPassword("");
}
-
- public void shutdown(DataSource dataSource) {
- Connection connection = JdbcUtils.getConnection(dataSource);
- Statement stmt = null;
- try {
- stmt = connection.createStatement();
- stmt.execute("SHUTDOWN");
- } catch (SQLException e) {
- if (logger.isWarnEnabled()) {
- logger.warn("Could not shutdown in-memory HSQL database", e);
- }
- } finally {
- JdbcUtils.closeStatement(stmt);
- }
- }
-
}
diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/JdbcUtils.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/JdbcUtils.java
index bd80bf18f5..866ba89c58 100644
--- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/JdbcUtils.java
+++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/JdbcUtils.java
@@ -11,8 +11,9 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
/**
- * Helper JDBC utilities used by other classes in this package. There is some duplication here with JdbcUtils in
- * jdbc.support package. We may want to consider simply using that. Package private for now.
+ * Helper JDBC utilities used by other classes in this package.
+ * Note there is some duplication here with JdbcUtils in jdbc.support package.
+ * We may want to consider simply using that at some point.
* @author Keith Donald
*/
final class JdbcUtils {
diff --git a/org.springframework.jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-3.0.xsd b/org.springframework.jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-3.0.xsd
index d8adc7a453..8b8296f039 100644
--- a/org.springframework.jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-3.0.xsd
+++ b/org.springframework.jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-3.0.xsd
@@ -30,7 +30,7 @@