【问题标题】:Not able to set spring.datasource.type无法设置 spring.datasource.type
【发布时间】:2015-12-26 07:46:17
【问题描述】:

我正在尝试在我的 Spring Boot 服务器上设置 c3p0。这是我现在的配置

spring.datasource.url=jdbc:mysql://url/db
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.test-on-borrow=true
#spring.datasource.test-while-idle=true
spring.datasource.validation-query=SELECT 1
#spring.datasource.time-between-eviction-runs-millis=10000
#spring.datasource.min-evictable-idle-time-millis=30000

spring.jpa.show-sql=true

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
spring.jpa.properties.hibernate.connection.driver_class=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.connection.url=jdbc:mysql://url/db
spring.jpa.properties.hibernate.connection.username=username
spring.jpa.properties.hibernate.connection.password=password
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.show_sql=true
#spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop


spring.jpa.properties.hibernate.c3p0.max_size=30
spring.jpa.properties.hibernate.c3p0.min_size=7
spring.jpa.properties.hibernate.c3p0.acquire_increment=1
spring.jpa.properties.hibernate.c3p0.idle_test_period=100
spring.jpa.properties.hibernate.c3p0.max_statements=0
spring.jpa.properties.hibernate.c3p0.max_idle_time=200
spring.jpa.properties.hibernate.c3p0.url=jdbc:mysql://url/db
spring.jpa.properties.hibernate.c3p0.username=username
spring.jpa.properties.hibernate.c3p0.password=password
spring.jpa.properties.hibernate.c3p0.driverClassName=com.mysql.jdbc.Driver

我的问题是我不知道如何告诉 spring.datasource 使用

com.mchange.v2.c3p0.ComboPooledDataSource

我看到的所有 XML 定义都使用了类似的东西

<bean id="dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">

不能在application.properties中设置数据源类型/类吗?

据此

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

spring.datasource.type= # fully qualified name of the connection pool implementation to use

但是按照这个

http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

(和我的 STS).type 选项不存在。这是一个错误还是我应该以不同的方式使用它?

我们将不胜感激您的帮助!

干杯!

【问题讨论】:

  • 你的spring.jpa.properties.hibernate.connectionspring.jpa.properties.hibernate.c3p0 没用。 Spring Boot 将注入一个数据源,从而禁用这些属性的使用,因此它们只会为您的application.properties 添加空间。我不建议使用 C3P0,它已经很老了,而且有一些缺陷,而是使用曾经支持的默认值之一,例如 tomcat-jdbcHikariCP。这样,只需添加 jar,Spring Boot 就会为您配置它。
  • 你要找的属性是Spring Boot 1.3早期版本没有这个。

标签: spring spring-boot datasource c3p0


【解决方案1】:

spring.datasource.type 已在 1.3 行中引入,因此您需要 Spring Boot 1.3.0.M5 才能使用该属性(IDE 中的内容帮助应该会给您正确的提示)。

在 1.2.x 上,您需要自己创建 DataSource bean 来强制类型,类似于

@Bean
@ConfigurationProperties("spring.datasource")
public DataSource dataSource() {
   return DataSourceBuilder.create().type(ComboPooledDataSource.class)
            .build();
}

【讨论】:

    【解决方案2】:

    你们真是一针见血。今天早上我进行了手动设置配置并连接了 c3p0,但是对于我的一个控制器,我使用的是 jdbcTemplate,结果发现 c3p0 数据源不能与 jdbcTemplates 一起使用,所以我决定看看替代方案。

    我做了一些阅读,结果证明 tomcat-jdbc 池将是我的最佳选择。因此,为了进行设置,我从原始帖子中列出的 application.properties 中删除了所有属性,并添加了以下自定义属性

    tomcat.jdbc.pool.url=jdbc:mysql://url/db_name
    tomcat.jdbc.pool.username=username
    tomcat.jdbc.pool.password=password
    tomcat.jdbc.pool.initial-size=10
    tomcat.jdbc.pool.test-on-borrow=true
    tomcat.jdbc.pool.test-while-idle=true
    tomcat.jdbc.pool.validation-query=SELECT 1
    tomcat.jdbc.pool.driver-class-name=com.mysql.jdbc.Driver
    tomcat.jdbc.pool.max_size=30
    tomcat.jdbc.pool.min_size=7
    

    然后我创建了以下配置类来将我的主数据源设置为 org.apache.tomcat.jdbc.pool.DataSource

    import org.apache.tomcat.jdbc.pool.DataSource;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    
    @EnableTransactionManagement
    @Configuration
    public class DataSourceConfiguration {
    
        @Value("${tomcat.jdbc.pool.max_size}")
        private int maxSize;
    
        @Value("${tomcat.jdbc.pool.min_size}")
        private int minSize;
    
        @Value("${tomcat.jdbc.pool.initial-size}")
        private int initialSize;
    
        @Value("${tomcat.jdbc.pool.test-on-borrow}")
        private boolean testOnBorrow;
    
        @Value("${tomcat.jdbc.pool.test-while-idle}")
        private boolean testWhileIdle;
    
        @Value("${tomcat.jdbc.pool.username}")
        private String username;
    
        @Value("${tomcat.jdbc.pool.password}")
        private String password;
    
        @Value("${tomcat.jdbc.pool.url}")
        private String url;
    
        @Value("${tomcat.jdbc.pool.driver-class-name}")
        private String driverClassName;
    
        @Value("${tomcat.jdbc.pool.validation-query}")
        private String validationQuery;
    
        @Bean
        @Primary
        public DataSource dataSource() {
            DataSource dataSource = new DataSource();
            dataSource.setUrl(url);
            dataSource.setPassword(password);
            dataSource.setUsername(username);
            dataSource.setDriverClassName(driverClassName);
            dataSource.setValidationQuery(validationQuery);
            dataSource.setInitialSize(initialSize);
            dataSource.setMaxIdle(maxSize);
            dataSource.setMinIdle(minSize);
            dataSource.setTestOnBorrow(testOnBorrow);
            dataSource.setTestWhileIdle(testWhileIdle);
            return dataSource;
        }
    }
    

    瞧,我现在有了一个生产就绪的连接池。

    我读到 HikariCP 在某些情况下可能会胜过 tomcat-jdbc 但我的应用程序是来自企业的自定义请求,它一次只能由 5-10-20 人使用,因此配置和设置的简单性肯定超过了微不足道的性能提升。

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-12-26
      • 2020-09-03
      • 1970-01-01
      • 2014-04-27
      • 2018-12-21
      • 2018-08-02
      • 2011-12-06
      • 2011-03-13
      • 2019-10-07
      相关资源
      最近更新 更多