提交 69646339 编写于 作者: J Juergen Hoeller

reflective loading of driver classes

上级 7dc9ec53
......@@ -13,19 +13,25 @@
* 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 H2 embedded database instance.
* Call {@link #getInstance()} to get the singleton instance of this class. *
* Call {@link #getInstance()} to get the singleton instance of this class.
*
* @author Oliver Gierke
* @since 3.0
*/
final class H2EmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigurer {
private static H2EmbeddedDatabaseConfigurer INSTANCE;
private final Class driverClass;
/**
* Get the singleton {@link H2EmbeddedDatabaseConfigurer} instance.
* @return the configurer
......@@ -33,20 +39,22 @@ final class H2EmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigu
*/
public static synchronized H2EmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException {
if (INSTANCE == null) {
ClassUtils.forName("org.h2.Driver", H2EmbeddedDatabaseConfigurer.class.getClassLoader());
INSTANCE = new H2EmbeddedDatabaseConfigurer();
INSTANCE = new H2EmbeddedDatabaseConfigurer(
ClassUtils.forName("org.h2.Driver", H2EmbeddedDatabaseConfigurer.class.getClassLoader()));
}
return INSTANCE;
}
public H2EmbeddedDatabaseConfigurer(Class driverClass) {
this.driverClass = driverClass;
}
public void configureConnectionProperties(ConnectionProperties properties, String databaseName) {
properties.setDriverClass(org.h2.Driver.class);
properties.setDriverClass(this.driverClass);
properties.setUrl(String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1", databaseName));
properties.setUsername("sa");
properties.setPassword("");
}
private H2EmbeddedDatabaseConfigurer() {
}
}
\ No newline at end of file
}
......@@ -13,6 +13,7 @@
* 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;
......@@ -20,13 +21,18 @@ 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
* @author Oliver Gierke
* @since 3.0
*/
final class HsqlEmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfigurer {
private static HsqlEmbeddedDatabaseConfigurer INSTANCE;
private final Class driverClass;
/**
* Get the singleton {@link HsqlEmbeddedDatabaseConfigurer} instance.
* @return the configurer
......@@ -34,20 +40,22 @@ final class HsqlEmbeddedDatabaseConfigurer extends AbstractEmbeddedDatabaseConfi
*/
public static synchronized HsqlEmbeddedDatabaseConfigurer getInstance() throws ClassNotFoundException {
if (INSTANCE == null) {
ClassUtils.forName("org.hsqldb.jdbcDriver", HsqlEmbeddedDatabaseConfigurer.class.getClassLoader());
INSTANCE = new HsqlEmbeddedDatabaseConfigurer();
INSTANCE = new HsqlEmbeddedDatabaseConfigurer(
ClassUtils.forName("org.hsqldb.jdbcDriver", HsqlEmbeddedDatabaseConfigurer.class.getClassLoader()));
}
return INSTANCE;
}
private HsqlEmbeddedDatabaseConfigurer(Class driverClass) {
this.driverClass = driverClass;
}
public void configureConnectionProperties(ConnectionProperties properties, String databaseName) {
properties.setDriverClass(org.hsqldb.jdbcDriver.class);
properties.setDriverClass(this.driverClass);
properties.setUrl("jdbc:hsqldb:mem:" + databaseName);
properties.setUsername("sa");
properties.setPassword("");
}
private HsqlEmbeddedDatabaseConfigurer() {
}
}
\ No newline at end of file
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册