Integrate @ConfigurationProperties @Beans with DataSource configuration
We now have a much simpler DataSourceAutoConfiguration that binds to whatever DataSource concrete type it finds at runtime. To be able to quickly switch between Hikari and the other types of DataSource there's a minute shim for translating the common properties (username, password, url, driverClassName), but actually only url is different. The shim and also DataSource initialization is supported through DataSourceProperties, but the other native properties get bound directly through the concrete runtime type of the DataSource. The /configprops endpoint works (and is exposed in the actuator sample). Fixes gh-840, fixes gh-477, see also gh-808.pull/874/head
parent
660b73b5c6
commit
5249f54c5a
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright 2013-2014 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.boot.actuate.endpoint;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.context.properties.ConfigurationBeanFactoryMetaData;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class ConfigurationPropertiesReportEndpointSerializationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testNaming() throws Exception {
|
||||
this.context.register(Config.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "foo.name:foo");
|
||||
this.context.refresh();
|
||||
ConfigurationPropertiesReportEndpoint report = this.context
|
||||
.getBean(ConfigurationPropertiesReportEndpoint.class);
|
||||
Map<String, Object> properties = report.invoke();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
.get("foo");
|
||||
assertNotNull(nestedProperties);
|
||||
assertEquals("foo", nestedProperties.get("prefix"));
|
||||
Map<String, Object> map = (Map<String, Object>) nestedProperties
|
||||
.get("properties");
|
||||
assertNotNull(map);
|
||||
assertEquals(1, map.size());
|
||||
assertEquals("foo", map.get("name"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
public static class Config {
|
||||
|
||||
@Bean
|
||||
public ConfigurationPropertiesReportEndpoint endpoint(
|
||||
ConfigurationBeanFactoryMetaData beanFactoryMetaData) {
|
||||
ConfigurationPropertiesReportEndpoint endpoint = new ConfigurationPropertiesReportEndpoint();
|
||||
endpoint.setConfigurationBeanFactoryMetaData(beanFactoryMetaData);
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "foo")
|
||||
public Foo foo() {
|
||||
return new Foo();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Foo {
|
||||
|
||||
private String name = "654321";
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// No setter so it doesn't appear in the report
|
||||
public String getSummary() {
|
||||
return "Name: " + this.name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2014 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.boot.autoconfigure.jdbc;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
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
|
||||
* recommended in high volume environments (the Tomcat DataSource is more reliable).
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @see DataSourceAutoConfiguration
|
||||
*/
|
||||
@Configuration
|
||||
public class CommonsDataSourceConfiguration extends AbstractDataSourceConfiguration {
|
||||
|
||||
private static Log logger = LogFactory.getLog(CommonsDataSourceConfiguration.class);
|
||||
|
||||
private BasicDataSource pool;
|
||||
|
||||
public CommonsDataSourceConfiguration() {
|
||||
// Ensure to set the correct default value for Commons DBCP
|
||||
setInitialSize(0);
|
||||
}
|
||||
|
||||
@Bean(destroyMethod = "close")
|
||||
public DataSource dataSource() {
|
||||
logger.info("Hint: using Commons DBCP BasicDataSource. It's going to work, "
|
||||
+ "but the Tomcat DataSource is more reliable.");
|
||||
this.pool = createAndConfigurePool();
|
||||
return this.pool;
|
||||
}
|
||||
|
||||
private BasicDataSource createAndConfigurePool() {
|
||||
BasicDataSource pool = new BasicDataSource();
|
||||
pool.setDriverClassName(getDriverClassName());
|
||||
pool.setUrl(getUrl());
|
||||
if (getUsername() != null) {
|
||||
pool.setUsername(getUsername());
|
||||
}
|
||||
if (getPassword() != null) {
|
||||
pool.setPassword(getPassword());
|
||||
}
|
||||
pool.setInitialSize(getInitialSize());
|
||||
pool.setMaxActive(getMaxActive());
|
||||
pool.setMaxIdle(getMaxIdle());
|
||||
pool.setMinIdle(getMinIdle());
|
||||
pool.setTestOnBorrow(isTestOnBorrow());
|
||||
pool.setTestOnReturn(isTestOnReturn());
|
||||
pool.setTestWhileIdle(isTestWhileIdle());
|
||||
pool.setValidationQuery(getValidationQuery());
|
||||
if (getTimeBetweenEvictionRunsMillis() != null) {
|
||||
pool.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
|
||||
}
|
||||
if (getMinEvictableIdleTimeMillis() != null) {
|
||||
pool.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
|
||||
}
|
||||
if (getMaxWaitMillis() != null) {
|
||||
pool.setMaxWait(getMaxWaitMillis());
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void close() {
|
||||
if (this.pool != null) {
|
||||
try {
|
||||
this.pool.close();
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
throw new DataAccessResourceFailureException(
|
||||
"Could not close data source", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* 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.boot.autoconfigure.jdbc;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.PropertyValues;
|
||||
import org.springframework.boot.bind.RelaxedDataBinder;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Convenience class for building a {@link DataSource} with common implementations and
|
||||
* properties. If Tomcat, HikariCP or Commons DBCP are on the classpath one of them will
|
||||
* be selected (in that order with Tomcat first). In the interest of a uniform interface,
|
||||
* and so that there can be a fallback to an embedded database if one can be detected on
|
||||
* the classpath, only a small set of common configuration properties are supported. To
|
||||
* inject additional properties into the result you can downcast it, or use
|
||||
* <code>@ConfigurationProperties</code>.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class DataSourceFactory {
|
||||
|
||||
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] {
|
||||
"org.apache.tomcat.jdbc.pool.DataSource",
|
||||
"com.zaxxer.hikari.HikariDataSource",
|
||||
"org.apache.commons.dbcp.BasicDataSource" };
|
||||
|
||||
private Class<? extends DataSource> type;
|
||||
|
||||
private ClassLoader classLoader;
|
||||
|
||||
private Map<String, String> properties = new HashMap<String, String>();
|
||||
|
||||
public static DataSourceFactory create() {
|
||||
return new DataSourceFactory(null);
|
||||
}
|
||||
|
||||
public static DataSourceFactory create(ClassLoader classLoader) {
|
||||
return new DataSourceFactory(classLoader);
|
||||
}
|
||||
|
||||
public DataSourceFactory(ClassLoader classLoader) {
|
||||
this.classLoader = classLoader;
|
||||
}
|
||||
|
||||
public DataSource build() {
|
||||
Class<? extends DataSource> type = getType();
|
||||
DataSource result = BeanUtils.instantiate(type);
|
||||
bind(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void bind(DataSource result) {
|
||||
new RelaxedDataBinder(result).bind(getPropertyValues());
|
||||
}
|
||||
|
||||
private PropertyValues getPropertyValues() {
|
||||
if (getType().getName().contains("Hikari") && this.properties.containsKey("url")) {
|
||||
this.properties.put("jdbcUrl", this.properties.get("url"));
|
||||
this.properties.remove("url");
|
||||
}
|
||||
return new MutablePropertyValues(this.properties);
|
||||
}
|
||||
|
||||
public DataSourceFactory type(Class<? extends DataSource> type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataSourceFactory url(String url) {
|
||||
this.properties.put("url", url);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataSourceFactory driverClassName(String driverClassName) {
|
||||
this.properties.put("driverClassName", driverClassName);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataSourceFactory username(String username) {
|
||||
this.properties.put("username", username);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataSourceFactory password(String password) {
|
||||
this.properties.put("password", password);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Class<? extends DataSource> findType() {
|
||||
if (this.type != null) {
|
||||
return this.type;
|
||||
}
|
||||
for (String name : DATA_SOURCE_TYPE_NAMES) {
|
||||
if (ClassUtils.isPresent(name, this.classLoader)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<DataSource> resolved = (Class<DataSource>) ClassUtils
|
||||
.resolveClassName(name, this.classLoader);
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Class<? extends DataSource> getType() {
|
||||
Class<? extends DataSource> type = findType();
|
||||
if (type != null) {
|
||||
return type;
|
||||
}
|
||||
throw new IllegalStateException("No supported DataSource type found");
|
||||
}
|
||||
|
||||
}
|
@ -1,125 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2014 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.boot.autoconfigure.jdbc;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
/**
|
||||
* Configuration for a HikariCP database pool. The HikariCP pool is a popular data source
|
||||
* implementation that provides high performance as well as some useful opinionated
|
||||
* defaults. For compatibility with other DataSource implementations accepts configuration
|
||||
* via properties in "spring.datasource.*", e.g. "url", "driverClassName", "username",
|
||||
* "password" (and some others but the full list supported by the Tomcat pool is not
|
||||
* applicable). Note that the Hikari team recommends using a "dataSourceClassName" and a
|
||||
* Properties instance (specified here as "spring.datasource.hikari.*"). This makes the
|
||||
* binding potentially vendor specific, but gives you full control of all the native
|
||||
* features in the vendor's DataSource.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @see DataSourceAutoConfiguration
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class HikariDataSourceConfiguration extends AbstractDataSourceConfiguration {
|
||||
|
||||
private String dataSourceClassName;
|
||||
|
||||
private String username;
|
||||
|
||||
private HikariDataSource pool;
|
||||
|
||||
private Properties hikari = new Properties();
|
||||
|
||||
@Bean(destroyMethod = "shutdown")
|
||||
public DataSource dataSource() {
|
||||
this.pool = new HikariDataSource();
|
||||
if (this.dataSourceClassName == null) {
|
||||
this.pool.setDriverClassName(getDriverClassName());
|
||||
}
|
||||
else {
|
||||
this.pool.setDataSourceClassName(this.dataSourceClassName);
|
||||
this.pool.setDataSourceProperties(this.hikari);
|
||||
}
|
||||
this.pool.setJdbcUrl(getUrl());
|
||||
if (getUsername() != null) {
|
||||
this.pool.setUsername(getUsername());
|
||||
}
|
||||
if (getPassword() != null) {
|
||||
this.pool.setPassword(getPassword());
|
||||
}
|
||||
this.pool.setMaximumPoolSize(getMaxActive());
|
||||
this.pool.setMinimumIdle(getMinIdle());
|
||||
if (isTestOnBorrow()) {
|
||||
this.pool.setConnectionInitSql(getValidationQuery());
|
||||
}
|
||||
else {
|
||||
this.pool.setConnectionTestQuery(getValidationQuery());
|
||||
}
|
||||
if (getMaxWaitMillis() != null) {
|
||||
this.pool.setMaxLifetime(getMaxWaitMillis());
|
||||
}
|
||||
return this.pool;
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void close() {
|
||||
if (this.pool != null) {
|
||||
this.pool.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dataSourceClassName the dataSourceClassName to set
|
||||
*/
|
||||
public void setDataSourceClassName(String dataSourceClassName) {
|
||||
this.dataSourceClassName = dataSourceClassName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the hikari data source properties
|
||||
*/
|
||||
public Properties getHikari() {
|
||||
return this.hikari;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getUsername() {
|
||||
if (StringUtils.hasText(this.username)) {
|
||||
return this.username;
|
||||
}
|
||||
if (this.dataSourceClassName == null
|
||||
&& EmbeddedDatabaseConnection.isEmbedded(getDriverClassName())) {
|
||||
return "sa";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-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.
|
||||
* 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.boot.autoconfigure.jdbc;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.boot.bind.PropertySourcesPropertyValues;
|
||||
import org.springframework.boot.bind.RelaxedDataBinder;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class RelaxedDataSourceFactory {
|
||||
|
||||
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] {
|
||||
"com.zaxxer.hikari.HikariDataSource",
|
||||
"org.apache.tomcat.jdbc.pool.DataSource",
|
||||
"org.apache.commons.dbcp.BasicDataSource" };
|
||||
|
||||
private Class<? extends DataSource> type;
|
||||
|
||||
private ConfigurableEnvironment environment;
|
||||
|
||||
public static RelaxedDataSourceFactory create(ConfigurableEnvironment environment) {
|
||||
return new RelaxedDataSourceFactory(environment);
|
||||
}
|
||||
|
||||
public RelaxedDataSourceFactory(ConfigurableEnvironment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
public DataSource build(String prefix) {
|
||||
Class<? extends DataSource> type = getType();
|
||||
DataSource result = BeanUtils.instantiate(type);
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(result, prefix);
|
||||
binder.bind(new PropertySourcesPropertyValues(this.environment
|
||||
.getPropertySources()));
|
||||
return result;
|
||||
}
|
||||
|
||||
public RelaxedDataSourceFactory type(Class<? extends DataSource> type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
private Class<? extends DataSource> getType() {
|
||||
if (this.type != null) {
|
||||
return this.type;
|
||||
}
|
||||
for (String name : DATA_SOURCE_TYPE_NAMES) {
|
||||
if (ClassUtils.isPresent(name, null)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<DataSource> resolved = (Class<DataSource>) ClassUtils
|
||||
.resolveClassName(name, null);
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("No supported DataSource type found");
|
||||
}
|
||||
|
||||
}
|
@ -1,89 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2014 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.boot.autoconfigure.jdbc;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Configuration for a Tomcat database pool. The Tomcat pool provides superior performance
|
||||
* and tends not to deadlock in high volume environments.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @see DataSourceAutoConfiguration
|
||||
*/
|
||||
@Configuration
|
||||
public class TomcatDataSourceConfiguration extends AbstractDataSourceConfiguration {
|
||||
|
||||
private String jdbcInterceptors;
|
||||
private long validationInterval = 30000;
|
||||
private org.apache.tomcat.jdbc.pool.DataSource pool;
|
||||
|
||||
@Bean(destroyMethod = "close")
|
||||
public DataSource dataSource() {
|
||||
this.pool = new org.apache.tomcat.jdbc.pool.DataSource();
|
||||
this.pool.setDriverClassName(getDriverClassName());
|
||||
this.pool.setUrl(getUrl());
|
||||
if (getUsername() != null) {
|
||||
this.pool.setUsername(getUsername());
|
||||
}
|
||||
if (getPassword() != null) {
|
||||
this.pool.setPassword(getPassword());
|
||||
}
|
||||
this.pool.setInitialSize(getInitialSize());
|
||||
this.pool.setMaxActive(getMaxActive());
|
||||
this.pool.setMaxIdle(getMaxIdle());
|
||||
this.pool.setMinIdle(getMinIdle());
|
||||
this.pool.setTestOnBorrow(isTestOnBorrow());
|
||||
this.pool.setTestOnReturn(isTestOnReturn());
|
||||
this.pool.setTestWhileIdle(isTestWhileIdle());
|
||||
if (getTimeBetweenEvictionRunsMillis() != null) {
|
||||
this.pool
|
||||
.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
|
||||
}
|
||||
if (getMinEvictableIdleTimeMillis() != null) {
|
||||
this.pool.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
|
||||
}
|
||||
this.pool.setValidationQuery(getValidationQuery());
|
||||
this.pool.setValidationInterval(this.validationInterval);
|
||||
if (getMaxWaitMillis() != null) {
|
||||
this.pool.setMaxWait(getMaxWaitMillis());
|
||||
}
|
||||
if (this.jdbcInterceptors != null) {
|
||||
this.pool.setJdbcInterceptors(this.jdbcInterceptors);
|
||||
}
|
||||
return this.pool;
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void close() {
|
||||
if (this.pool != null) {
|
||||
this.pool.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void setJdbcInterceptors(String jdbcInterceptors) {
|
||||
this.jdbcInterceptors = jdbcInterceptors;
|
||||
}
|
||||
|
||||
public void setValidationInterval(long validationInterval) {
|
||||
this.validationInterval = validationInterval;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue