SpringBoot發現最新版Druid重大問題

馮曉東技術部落格18649325921發表於2020-09-24

發現Druid問題

最近做專案,遇到大量插入的地方,經過大量的除錯,最終發現是Druid連線池的問題,(以前一個大專案就遇到過Druid的坑),果斷換成c3p0之後,壓力測試嘩嘩上去了。

下面是更換c3p0方法。

1.修改pom.xml

匯入c3p0依賴:

<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.5</version>
</dependency>

2.修改application.yml

spring:
  application:
    name: nh-tst
  http:
    encoding:
      charset: UTF-8
      enabled: true
      force: true
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
  jpa:
    hibernate:
      ddl-auto: none
    show-sql: true
c3p0:
  jdbcUrl: jdbc:oracle:thin:@xxxxx:1522/prodpdb1
  user: xxxxxx
  password: xxxxxx
  driverClass: oracle.jdbc.driver.OracleDriver
  minPoolSize: 3
  maxPoolSize: 30
  maxIdleTime: 1800000
  acquireIncrement: 120
  maxStatements: 100000
  initialPoolSize: 5
  idleConnectionTestPeriod: 60
  acquireRetryAttempts: 30
  acquireRetryDelay: 10000
  breakAfterAcquireFailure: false
  testConnectionOnCheckout: false

3.增加DataSourceConfiguration.java類

package com.nh.fk.tst.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.mchange.v2.c3p0.ComboPooledDataSource;

@Configuration
public class DataSourceConfiguration {
	// c3p0 連線池
	@Bean(name = "dataSource")
	@Qualifier(value = "dataSource")
	@Primary
	@ConfigurationProperties(prefix = "c3p0")
	public DataSource dataSource() {
		return DataSourceBuilder.create().type(ComboPooledDataSource.class).build();
	}
}

打包,執行:世界又恢復了和平!!

相關文章