如何配置tomcat数据源?

时间:2019-11-06 19:54:58

标签: spring spring-boot

在build.gradle中,我有:

compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-web-services'
compile 'org.springframework.boot:spring-boot-starter-amqp'
compile("org.springframework.boot:spring-boot-starter-jdbc") {
    exclude module: "spring-boot-starter-hikari"
}
compile group: 'org.apache.tomcat', name: 'tomcat-jdbc', version: '9.0.21'

我尝试在application.yml(位于tomcat /../ resource_directory中)中配置数据源

spring:
datasource:
    jdbc-url: "jdbc:oracle:thin: ...."
    username: "admin"
    password: "admin"
    driver-class-name: "oracle.jdbc.driver.OracleDriver"
    type: "org.apache.tomcat.jdbc.pool.DataSource" 
    tomcat:
        initialSize: 55
        max-wait: 10000
        max-active: 50
        max-idle: 15
        min-idle: 8
        default-autoCommit: false

我在代码中使用@Autowired。但是,当我调试代码时,我会下一步:

log.info("dataSource: " + dataSource.toString());1
log.info("dataSourceClass: " + dataSource.getClass());2
dataSource: HikariDataSource (null) 1
dataSourceClass: class com.zaxxer.hikari.HikariDataSource 2

在日志文件中,我接下来看到了

Loaded 1 document from YAML resource // with correct path to file.

并更正加载的数据:

spring={datasource={jdbc-url=jdbc...., username=admin, password=admin, driver-class-name=oracle.jdbc.driver.OracleDriver, 
type=org.apache.tomcat.jdbc.pool.DataSource, tomcat={initialSize=55, max- 
wait=10000, max-active=50, max-idle=15, min-idle=8, default- 
autoCommit=false}}}

当我将config放入application.propertes中的src / resources中时,我得到了正确的tomcat dataSource。

dataSource: org.apache.tomcat.jdbc.pool.DataSourc //1
dataSourceClass: class org.apache.tomcat.jdbc.pool.DataSource//2

我做错了什么??帮我解决这个困惑。

1 个答案:

答案 0 :(得分:0)

您实际上应该排除:

com.zaxxer:HikariCP

编辑:另外,我认为this answer可能也有帮助