Skip to content

Commit

Permalink
perf($MyBatisPlus): update MyBatis-Plus version to 3.4.3.2
Browse files Browse the repository at this point in the history
dynamic-datasource-spring-boot-starter 3.4.1
hutool-all 5.7.9

BREAKING CHANGE: unregister bean after processing Druid connection pool
size
  • Loading branch information
johnnymillergh committed Aug 26, 2021
1 parent 2dcb047 commit 95af9e0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
<spring-boot-admin-starter-server.version>2.4.3</spring-boot-admin-starter-server.version>
<spring-boot-admin-starter-client.version>2.4.3</spring-boot-admin-starter-client.version>
<druid-spring-boot-starter.version>1.2.6</druid-spring-boot-starter.version>
<mybatis-plus-boot-starter.version>3.4.3.1</mybatis-plus-boot-starter.version>
<dynamic-datasource-spring-boot-starter.version>3.4.0</dynamic-datasource-spring-boot-starter.version>
<hutool-all.version>5.7.7</hutool-all.version>
<mybatis-plus-boot-starter.version>3.4.3.2</mybatis-plus-boot-starter.version>
<dynamic-datasource-spring-boot-starter.version>3.4.1</dynamic-datasource-spring-boot-starter.version>
<hutool-all.version>5.7.9</hutool-all.version>
<guava.version>30.1.1-jre</guava.version>
<knife4j.version>2.0.9</knife4j.version>
<jjwt.version>0.11.2</jjwt.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.boot.autoconfigure.quartz.QuartzTransactionManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
Expand All @@ -30,9 +31,9 @@
@ConditionalOnClass({MybatisPlusAutoConfiguration.class})
public class DataSourceConfiguration {
@Bean
public DruidDataSourceCreatorPostProcessor druidDataSourceCreatorPostProcessor() {
public DruidDataSourceCreatorPostProcessor druidDataSourceCreatorPostProcessor(ApplicationContext applicationContext, DynamicDataSourceProperties dynamicDataSourceProperties) {
log.warn("Initial bean: '{}'", DruidDataSourceCreatorPostProcessor.class.getSimpleName());
return new DruidDataSourceCreatorPostProcessor();
return new DruidDataSourceCreatorPostProcessor(applicationContext, dynamicDataSourceProperties);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.jmsoftware.maf.springcloudstarter.database;

import com.baomidou.dynamic.datasource.creator.DruidDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

Expand All @@ -14,12 +19,16 @@
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 8/25/2021 11:27 AM
**/
@Slf4j
public class DruidDataSourceCreatorPostProcessor implements BeanPostProcessor {
@RequiredArgsConstructor
public class DruidDataSourceCreatorPostProcessor implements BeanPostProcessor, DisposableBean {
private final ApplicationContext applicationContext;
private final DynamicDataSourceProperties dynamicDataSourceProperties;

@Nullable
@Override
public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException {
public Object postProcessBeforeInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException {
if (bean instanceof DruidDataSourceCreator) {
this.postProcessDynamicDataSourceProperties((DruidDataSourceCreator) bean);
this.enhanceConnectionPoolSize();
}
return bean;
}
Expand All @@ -44,22 +53,29 @@ public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull Stri
* connections you enable in your connection pool. That way there are always a few slots available for direct
* connections for system maintenance and monitoring.</p>
*
* @param bean the bean
* @see
* <a href='https://wiki.postgresql.org/wiki/Number_Of_Database_Connections#How_to_Find_the_Optimal_Database_Connection_Pool_Size'>How to Find the Optimal Database Connection Pool Size</a>
* @see
* <a href='https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-j2ee-concepts-connection-pooling.html#idm46216069663472'>Sizing the Connection Pool</a>
*/
private void postProcessDynamicDataSourceProperties(DruidDataSourceCreator bean) {
private void enhanceConnectionPoolSize() {
val cpuCoreCount = Runtime.getRuntime().availableProcessors();
val minConnectionPoolSize = cpuCoreCount * 2 + 1;
val maxConnectionPoolSize = cpuCoreCount * 3;
bean.getGConfig()
this.dynamicDataSourceProperties.getDruid()
.setInitialSize(minConnectionPoolSize)
.setMinIdle(minConnectionPoolSize)
.setMaxActive(maxConnectionPoolSize);
log.warn("Druid connection pool enhanced by current cpuCoreCount: {}, initial size: {}, min idle: {}" +
", max active: {}",
cpuCoreCount, minConnectionPoolSize, minConnectionPoolSize, maxConnectionPoolSize);
val defaultListableBeanFactory =
(DefaultListableBeanFactory) this.applicationContext.getAutowireCapableBeanFactory();
defaultListableBeanFactory.destroyBean(this);
}

@Override
public void destroy() {
log.warn("Destroyed bean {}", this.getClass().getSimpleName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Interceptor masterSlaveAutoRoutingPlugin() {
}

@Bean
public CommonMetaObjectHandler myBatisPlusConfiguration() {
public CommonMetaObjectHandler commonMetaObjectHandler() {
log.warn("Initial bean: '{}'", CommonMetaObjectHandler.class.getSimpleName());
return new CommonMetaObjectHandler();
}
Expand Down

0 comments on commit 95af9e0

Please sign in to comment.