[bs-51] Add support for h2 and Derby embedded database
Also included support for database shutdown in @PreDestroy [Fixes #48387903]
This commit is contained in:
parent
1a0902b32a
commit
a12f0dcd12
|
@ -15,6 +15,9 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.bootstrap.autoconfigure.jdbc;
|
package org.springframework.bootstrap.autoconfigure.jdbc;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import javax.annotation.PreDestroy;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.apache.commons.dbcp.BasicDataSource;
|
import org.apache.commons.dbcp.BasicDataSource;
|
||||||
|
@ -22,6 +25,7 @@ import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.dao.DataAccessResourceFailureException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration for a Commons DBCP database pool. The DBCP pool is popular but not
|
* Configuration for a Commons DBCP database pool. The DBCP pool is popular but not
|
||||||
|
@ -34,16 +38,29 @@ import org.springframework.context.annotation.Configuration;
|
||||||
public class BasicDataSourceConfiguration extends AbstractDataSourceConfiguration {
|
public class BasicDataSourceConfiguration extends AbstractDataSourceConfiguration {
|
||||||
|
|
||||||
private static Log logger = LogFactory.getLog(BasicDataSourceConfiguration.class);
|
private static Log logger = LogFactory.getLog(BasicDataSourceConfiguration.class);
|
||||||
|
private BasicDataSource pool;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource dataSource() {
|
public DataSource dataSource() {
|
||||||
logger.info("Hint: using Commons DBCP BasicDataSource. It's going to work, but the Tomcat DataSource is more reliable.");
|
logger.info("Hint: using Commons DBCP BasicDataSource. It's going to work, but the Tomcat DataSource is more reliable.");
|
||||||
BasicDataSource pool = new BasicDataSource();
|
this.pool = new BasicDataSource();
|
||||||
pool.setDriverClassName(getDriverClassName());
|
this.pool.setDriverClassName(getDriverClassName());
|
||||||
pool.setUrl(getUrl());
|
this.pool.setUrl(getUrl());
|
||||||
pool.setUsername(getUsername());
|
this.pool.setUsername(getUsername());
|
||||||
pool.setPassword(getPassword());
|
this.pool.setPassword(getPassword());
|
||||||
return pool;
|
return this.pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
public void close() {
|
||||||
|
if (this.pool != null) {
|
||||||
|
try {
|
||||||
|
this.pool.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DataAccessResourceFailureException(
|
||||||
|
"Could not close data source", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,11 +19,13 @@ package org.springframework.bootstrap.autoconfigure.jdbc;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.annotation.PreDestroy;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
@ -36,25 +38,42 @@ import org.springframework.util.ClassUtils;
|
||||||
@Configuration
|
@Configuration
|
||||||
public class EmbeddedDatabaseConfiguration {
|
public class EmbeddedDatabaseConfiguration {
|
||||||
|
|
||||||
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_TYPE_CLASSES;
|
|
||||||
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_DRIVER_CLASSES;
|
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_DRIVER_CLASSES;
|
||||||
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_URLS;
|
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_URLS;
|
||||||
|
|
||||||
|
private EmbeddedDatabase database;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
EMBEDDED_DATABASE_TYPE_CLASSES = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
|
||||||
EMBEDDED_DATABASE_TYPE_CLASSES.put(EmbeddedDatabaseType.HSQL,
|
|
||||||
"org.hsqldb.Database");
|
|
||||||
EMBEDDED_DATABASE_DRIVER_CLASSES = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
EMBEDDED_DATABASE_DRIVER_CLASSES = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
||||||
|
EMBEDDED_DATABASE_DRIVER_CLASSES.put(EmbeddedDatabaseType.H2, "org.h2.Driver");
|
||||||
|
EMBEDDED_DATABASE_DRIVER_CLASSES.put(EmbeddedDatabaseType.DERBY,
|
||||||
|
"org.apache.derby.jdbc.EmbeddedDriver");
|
||||||
EMBEDDED_DATABASE_DRIVER_CLASSES.put(EmbeddedDatabaseType.HSQL,
|
EMBEDDED_DATABASE_DRIVER_CLASSES.put(EmbeddedDatabaseType.HSQL,
|
||||||
"org.hsqldb.jdbcDriver");
|
"org.hsqldb.jdbcDriver");
|
||||||
|
|
||||||
EMBEDDED_DATABASE_URLS = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
EMBEDDED_DATABASE_URLS = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
||||||
|
EMBEDDED_DATABASE_URLS.put(EmbeddedDatabaseType.H2,
|
||||||
|
"jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
|
||||||
|
EMBEDDED_DATABASE_URLS.put(EmbeddedDatabaseType.DERBY,
|
||||||
|
"jdbc:derby:memory:testdb;create=true");
|
||||||
EMBEDDED_DATABASE_URLS.put(EmbeddedDatabaseType.HSQL, "jdbc:hsqldb:mem:testdb");
|
EMBEDDED_DATABASE_URLS.put(EmbeddedDatabaseType.HSQL, "jdbc:hsqldb:mem:testdb");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource dataSource() {
|
public DataSource dataSource() {
|
||||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
|
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
|
||||||
.setType(getEmbeddedDatabaseType());
|
.setType(getEmbeddedDatabaseType());
|
||||||
return builder.build();
|
this.database = builder.build();
|
||||||
|
return this.database;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
public void close() {
|
||||||
|
if (this.database != null) {
|
||||||
|
this.database.shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getEmbeddedDatabaseDriverClass(
|
public static String getEmbeddedDatabaseDriverClass(
|
||||||
|
@ -67,7 +86,7 @@ public class EmbeddedDatabaseConfiguration {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static EmbeddedDatabaseType getEmbeddedDatabaseType() {
|
public static EmbeddedDatabaseType getEmbeddedDatabaseType() {
|
||||||
for (Map.Entry<EmbeddedDatabaseType, String> entry : EMBEDDED_DATABASE_TYPE_CLASSES
|
for (Map.Entry<EmbeddedDatabaseType, String> entry : EMBEDDED_DATABASE_DRIVER_CLASSES
|
||||||
.entrySet()) {
|
.entrySet()) {
|
||||||
if (ClassUtils.isPresent(entry.getValue(),
|
if (ClassUtils.isPresent(entry.getValue(),
|
||||||
EmbeddedDatabaseConfiguration.class.getClassLoader())) {
|
EmbeddedDatabaseConfiguration.class.getClassLoader())) {
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.bootstrap.autoconfigure.jdbc;
|
package org.springframework.bootstrap.autoconfigure.jdbc;
|
||||||
|
|
||||||
|
import javax.annotation.PreDestroy;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
@ -30,14 +31,23 @@ import org.springframework.context.annotation.Configuration;
|
||||||
@Configuration
|
@Configuration
|
||||||
public class TomcatDataSourceConfiguration extends AbstractDataSourceConfiguration {
|
public class TomcatDataSourceConfiguration extends AbstractDataSourceConfiguration {
|
||||||
|
|
||||||
|
private org.apache.tomcat.jdbc.pool.DataSource pool;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource dataSource() {
|
public DataSource dataSource() {
|
||||||
org.apache.tomcat.jdbc.pool.DataSource pool = new org.apache.tomcat.jdbc.pool.DataSource();
|
this.pool = new org.apache.tomcat.jdbc.pool.DataSource();
|
||||||
pool.setDriverClassName(getDriverClassName());
|
this.pool.setDriverClassName(getDriverClassName());
|
||||||
pool.setUrl(getUrl());
|
this.pool.setUrl(getUrl());
|
||||||
pool.setUsername(getUsername());
|
this.pool.setUsername(getUsername());
|
||||||
pool.setPassword(getPassword());
|
this.pool.setPassword(getPassword());
|
||||||
return pool;
|
return this.pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
public void close() {
|
||||||
|
if (this.pool != null) {
|
||||||
|
this.pool.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue